nginx虚拟主机配置

一、虚拟主机概念和类型介绍

1、虚拟主机概念
虚拟主机在web服务器里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立的对外提供服务供用户访问。一个web服务里可以同时支持多个虚拟主机站点

2、虚拟主机类型
常见的虚拟主机类型有如下几种。

(1)基于域名的虚拟主机

所谓基于域名的虚拟主机,意思就是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机,例如:www.etiantian.org

(2)基于端口的虚拟主机

同理,所谓基于端口的虚拟主机,意思就是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里要带有端口,例如:http://www.etiantian.org:9000

(3)基于IP的虚拟主机

同理,所谓基于IP的虚拟主机,意思就是通过不同的IP区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见。一般不同的业务需要使用多IP的场景都会在负载均衡器上进行VIP绑定,而不是在Web上绑定I来区分不同的虚拟机。

三种虚拟主机类型均可独立使用,也可以混合使用。

二、基于域名的虚拟主机配置

1、配置基于域名的虚拟主机

[root@nginx ~]# cd /application/nginx/conf/
[root@nginx conf]# diff nginx.conf.default nginx.conf    #初始时这两个配置文件是一致的;
[root@nginx conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf        #过滤包含#号和空行,生成新文件nginx.conf

或者直接创建新的配置文件nginx.conf,然后编辑
[root@nginx conf]# vim nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.dmtest1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

这样,一个基于域名的站点就创建好了。

 2、创建域名对应的站点目录及文件

[root@nginx conf]# mkdir ../html/www -p        #创建www目录;
[root@nginx conf]# echo "http://www.dmtest1.com.org" >../html/www/index.html      #生成一个默认首页文件;
[root@nginx conf]# cat ../html/www/index.html   #查看生成的默认首页文件;
http://www.dmtest1.com.org    

 3、检查语法并重载nginx

[root@nginx conf]# ../sbin/nginx -t    #检查nginx配置文件语法是否正确;
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@nginx conf]# systemctl reload nginx  #平滑重启nginx;

[root@nginx conf]# ps -ef |grep nginx    #查看nginx启动进程;
root       1100      1  0 19:01 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
www        1428   1100  0 19:24 ?        00:00:00 nginx: worker process
root       1430   1293  0 19:24 pts/0    00:00:00 grep --color=auto nginx

[root@nginx conf]# netstat -lntp | grep 80    #查看是否监听在80 端口
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1100/nginx: master  

========================================================================================================================

测试

Linux测试:   
[root@nginx conf]# echo "192.168.200.102 www.dmtest1.com" >>/etc/hosts  
[root@nginx conf]# tail -1 /etc/hosts
192.168.200.102 www.dmtest1.com
[root@nginx conf]# curl www.dmtest1.com
http://www.dmtest1.com.org

Windows测试:
Windows下需要配置hosts解析,hosts文件在C:WindowsSystem32driversetc目录下
在hosts文件中的添加方法和Linux一样。

 4、配置多个基于域名的虚拟主机

多个域名虚拟主机的配置的区别就是server_name和root参数的配置不同,下面是基于三个域名的虚拟主机配置:

[root@nginx conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.dmtest1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     server {
         listen       80;
         server_name  www.dmtest2.com;
         location / {
             root   html/dmtest2;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }

     server {
         listen       80;
         server_name  www.dmtest3.com;
         location / {
             root   html/dmtest3;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
}

 创建虚拟主机站点对应的目录和文件

[root@nginx conf]# mkdir ../html/dmtest2 ../html/dmtest3 -p
[root@nginx conf]# echo "http://dmtest2.com" >../html/dmtest2/index.html
[root@nginx conf]# echo "http://dmtest3.com" >../html/dmtest3/index.html

[root@nginx conf]# cat ../html/dmtest2/index.html 
http://dmtest2.com
[root@nginx conf]# cat ../html/dmtest3/index.html 
http://dmtest3.com

 目录结构如下:

root@nginx conf]# tree ../html/
../html/
├── 50x.html
├── dmtest2            #dmtest2站点目录;
│   └── index.html
├── dmtest3        #dmtest3站点目录;
│   └── index.html
├── index.html  
└── www          #www站点目录
    └── index.html

3 directories, 5 files

 检查并重载nginx配置文件

root@nginx conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@nginx conf]# systemctl reload nginx

 测试

[root@nginx conf]# echo "192.168.200.102 www.dmtest2.com" >>/etc/hosts
[root@nginx conf]# echo "192.168.200.102 www.dmtest3.com" >>/etc/hosts

[root@nginx conf]# curl www.dmtest2.com
http://dmtest2.com
[root@nginx conf]# curl www.dmtest3.com
http://dmtest3.com
[root@nginx conf]# curl www.dmtest1.com
http://www.dmtest1.com.org

 三、基于端口的虚拟主机配置

如果要配置基于端口的虚拟主机,就需要为每个虚拟主机配置不同的端口。编辑nginx.conf主配置文件,把每个虚拟主机的“listen 80”,这个配置行的80端口修改掉,server_name域名位置可以不用改变。

root@nginx conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.dmtest1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     server {
         listen       88;
         server_name  www.dmtest2.com;
         location / {
             root   html/dmtest2;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }

     server {
         listen       888;
         server_name  www.dmtest3.com;
         location / {
             root   html/dmtest3;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
}

 检查语法并重载nginx

[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@nginx conf]# systemctl reload nginx

[root@nginx conf]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1100/nginx: master  
tcp        0      0 0.0.0.0:888             0.0.0.0:*               LISTEN      1100/nginx: master  
tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      1100/nginx: master 

 防火墙开启88和888端口

[root@nginx conf]# firewall-cmd --zone=public --add-port=88/tcp --permanent 
success
[root@nginx conf]# firewall-cmd --zone=public --add-port=888/tcp --permanent 
success
[root@nginx conf]# firewall-cmd --reload
success
[root@nginx conf]# firewall-cmd --list-port
80/tcp 88/tcp 888/tcp

 测试

[root@nginx conf]# curl 192.168.200.102:88
http://dmtest2.com
[root@nginx conf]# curl 192.168.200.102:888
http://dmtest3.com
[root@nginx conf]# curl 192.168.200.102:80
http://www.dmtest1.com.org

 四、基于IP的虚拟主机配置

在服务器网卡上增加多个IP

[root@nginx conf]# ip addr add 192.168.200.103/24 dev ens34
[root@nginx conf]# ip addr add 192.168.200.104/24 dev ens34

[root@nginx conf]# ip add | grep 192.168.200    #查看添加的IP;
    inet 192.168.200.102/24 brd 192.168.200.255 scope global noprefixroute ens34
    inet 192.168.200.103/24 scope global secondary ens34
    inet 192.168.200.104/24 scope global secondary ens34

[root@nginx conf]# ping -c 3 192.168.200.104    #测试添加到的IP是否正常;
PING 192.168.200.104 (192.168.200.104) 56(84) bytes of data.
64 bytes from 192.168.200.104: icmp_seq=1 ttl=64 time=0.024 ms
64 bytes from 192.168.200.104: icmp_seq=2 ttl=64 time=0.035 ms
64 bytes from 192.168.200.104: icmp_seq=3 ttl=64 time=0.032 ms

--- 192.168.200.104 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.024/0.030/0.035/0.006 ms
[root@nginx conf]# ping -c 3 192.168.200.103    #测试添加到的IP是否正常;
PING 192.168.200.103 (192.168.200.103) 56(84) bytes of data.
64 bytes from 192.168.200.103: icmp_seq=1 ttl=64 time=0.023 ms
64 bytes from 192.168.200.103: icmp_seq=2 ttl=64 time=0.034 ms
64 bytes from 192.168.200.103: icmp_seq=3 ttl=64 time=0.033 ms

--- 192.168.200.103 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.023/0.030/0.034/0.005 ms

 修改nginx配置文件如下:

root@nginx conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen      192.168.200.102:80;
        server_name  www.dmtest1.com;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
     server {
         listen       192.168.200.103:88;
         server_name  www.dmtest2.com;
         location / {
             root   html/dmtest2;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }

     server {
         listen       192.168.200.104:888;
         server_name  www.dmtest3.com;
         location / {
             root   html/dmtest3;
             index  index.html index.htm;
         }
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
     }
}


#这是一个端口和IP混合的虚拟主机配置,可以根据需要自行修改,使其仅仅基于IP,即把每个虚拟主机的server_name字段都换成IP地址。

 检查配置文件并重载

[root@nginx conf]# ../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful
[root@nginx conf]# systemctl reload nginx

 测试

root@nginx conf]# curl 192.168.200.102:80
http://www.dmtest1.com.org
[root@nginx conf]# curl 192.168.200.103:88
http://dmtest2.com
[root@nginx conf]# curl 192.168.200.104:888
http://dmtest3.com
[root@nginx conf]# curl www.dmtest1.com:80
http://www.dmtest1.com.org
[root@nginx conf]# curl www.dmtest2.com:88
http://dmtest2.com
[root@nginx conf]# curl www.dmtest3.com:888
http://dmtest3.com

 五、nginx虚拟主机配置步骤

总结如下:

1)增加一个完整的server标签段到结尾处。注意,要放在http的结束大括号前也就是将server标签段放入http标签。

2)更改server_name及对应网页的root根目录,如果需要其他参数,可以增加或修改。

3)创建server_name域名对应网页的根目录,并且建立测试文件,如果没有index首页,访问会出现403错误。

4)检查 Nginx配置文件语法,平滑重启Nginx服务,快速检查启动结果。

5)在客户端对server_name处配置的域名做host解析或DNS配置,并检查(ping域名看返回的IP是否正确)。

6)在浏览器中输入地址访问,或者在Linux客户端做hosts解析,用wget或curl接地址访问。

Nginx虚拟主机的官方帮助网址为http://ngInx.org/en/docs/http/request_processing.html。

原文地址:https://www.cnblogs.com/Mr-Ding/p/9532824.html