1. 程式人生 > >Nginx 虛擬主機配置的三種方式(三)(基於域名)

Nginx 虛擬主機配置的三種方式(三)(基於域名)

3、Nginx基於域名的虛擬主機配置

使用基於域名的虛擬主機配置是比較流行的方式,可以在同一個IP上配置多個域名並且都通過80埠訪問。

3.1 假設伺服器有個IP地址為192.168.2.155

[[email protected] ~]# ifconfig ens33:5 192.168.2.155/24 up
[[email protected] ~]# ifconfig
ens33:5: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.2.155  netmask 255.255.255.0  broadcast 192.168.2.255
        ether 00:0c:29:16:90:ae  txqueuelen 1000  (Ethernet)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.2 192.168.2.155對應的域名如下,配置主機的host檔案便於測試

[[email protected] ~]# vim /etc/hosts
[[email protected] ~]# cat /etc/hosts|grep 192.168.2.155
192.168.2.155 www.oa.com
192.168.2.155 www.bbs.com
192.168.2.155 www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5

3.3 建立虛擬主機存放網頁的根目錄,並建立首頁檔案index.html

[[email protected]
~]# cd /data/www/ [[email protected] www]# mkdir www.oa.com [[email protected] www]# mkdir www.bbs.com [[email protected] www]# mkdir www.test.com [[email protected] www]# echo www.oa.com > www.oa.com/index.html [[email protected] www]# echo www.bbs.com > www.bbs.com/index.html [
[email protected]
www]# echo www.test.com > www.test.com/index.html
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.4 修改nginx.conf,將虛擬主機配置檔案包含進主檔案

[[email protected] /]# cd /usr/local/nginx/conf/
[[email protected] conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[[email protected] conf]# vim nginx.conf
  • 1
  • 2
  • 3
  • 4
  • 5

在nginx.conf檔案末尾加入以下配置

# 在http段中找到以下內容並刪除每行前面的“#”
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

# 配置檔案結尾的最後一個“}”之前加入以下語句,如下所示
include vhost/*.conf
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.5 編輯每個域名的配置檔案(每個虛擬主機的配置檔案)

[[email protected] conf]# cd vhost/
[[email protected] vhost]# cat www.oa.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.oa.com;

        access_log   /data/logs/www.oa.com.log main;
        error_log    /data/logs/www.oa.com.error.log;

        location / {
            root   /data/www/www.oa.com;
            index  index.html index.htm;
        }
    }

[[email protected] vhost]# cat www.bbs.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.bbs.com;

        access_log   /data/logs/www.bbs.com.log main;
        error_log    /data/logs/www.bbs.com.error.log;

        location / {
            root   /data/www/www.bbs.com;
            index  index.html index.htm;
        }
    }

[[email protected] vhost]# cat www.test.com.conf
    server {
        listen       192.168.2.155:80;
        server_name  www.test.com;

        access_log   /data/logs/www.test.com.log main;
        error_log    /data/logs/www.test.com.error.log;

        location / {
            root   /data/www/www.test.com;
            index  index.html index.htm;
        }
    }

[[email protected] vhost]# cat /data/www/www.oa.com/index.html
www.oa.com
[[email protected] vhost]# cat /data/www/www.bbs.com/index.html
www.bbs.com
[[email protected] vhost]# cat /data/www/www.test.com/index.html
www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

3.6 建立日誌檔案,否則無法啟動nginx

[[email protected] /]# mkdir -p /data/logs
[[email protected] /]# touch /data/logs/www.oa.com.log
[[email protected] /]# touch /data/logs/www.oa.com.error.log
[[email protected] /]# touch /data/logs/www.bbs.com.log
[[email protected] /]# touch /data/logs/www.bbs.com.error.log
[[email protected] /]# touch /data/logs/www.test.com.log
[[email protected] /]# touch /data/logs/www.test.com.error.log
[[email protected] /]# ls /data/logs/
www.oa.com.error.log  www.bbs.com.error.log  www.test.com.error.log
www.oa.com.log        www.bbs.com.log        www.test.com.log
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

3.7 先測試配置檔案然後再啟動nginx

[[email protected] /]# cd /usr/local/nginx/sbin/
[[email protected] sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 啟動nginx
[[email protected] sbin]# ./nginx
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.8 測試檔案

[[email protected] vhost]# curl http://www.oa.com
www.oa.com
[[email protected] vhost]# curl http://www.bbs.com
www.bbs.com
[[email protected] vhost]# curl http://www.test.com
www.test.com
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

附:配置過程中的問題

1、最後測試時發生的問題

[[email protected] ~]# curl http://www.oa.com
curl: (7) Failed connect to www.oa.com:80; 拒絕連線
  • 1
  • 2

決解方法: 檢視Nginx是否在監聽相應的埠。

[[email protected] ~]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN
tcp        0      0 192.168.2.155:80        0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN
tcp6       0      0 :::111                  :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN
tcp6       0      0 :::23                   :::*                    LISTEN
tcp6       0      0 ::1:25                  :::*                    LISTEN
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1、配置虛擬主機檔案時要加上監聽的IP地址,每個虛擬主機配置檔案都一樣。

 listen       192.168.2.155:80;
  • 1

2、配置完成後要重啟伺服器

--------------------- 作者:B8613A 來源:CSDN 原文:https://blog.csdn.net/liupeifeng3514/article/details/79007051?utm_source=copy 版權宣告:本文為博主原創文章,轉載請附上博文連結!