一、虚拟主机概念
虚拟主机在web服务器里就是一个独立的网站站点,这个站点对应独立的域名(也多是IP或端口),具备独立的程序及资源目录,能够独立的对外提供服务供用户访问。一个web服务里能够同时支持多个虚拟主机站点html
二、虚拟主机类型
常见的虚拟主机类型有以下几种。nginx
(1)基于域名的虚拟主机web
所谓基于域名的虚拟主机,意思就是经过不一样的域名区分不一样的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型,几乎全部对外提供服务的网站使用的都是基于域名的虚拟主机,例如:www.etiantian.orgvim
(2)基于端口的虚拟主机浏览器
同理,所谓基于端口的虚拟主机,意思就是经过不一样的端口来区分不一样的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不但愿直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里要带有端口,例如:http://www.etiantian.org:9000服务器
(3)基于IP的虚拟主机app
同理,所谓基于IP的虚拟主机,意思就是经过不一样的IP区分不一样的虚拟主机,此类虚拟主机对应的企业应用很是少见。通常不一样的业务须要使用多IP的场景都会在负载均衡器上进行VIP绑定,而不是在Web上绑定I来区分不一样的虚拟机。负载均衡
三种虚拟主机类型都可独立使用,也能够混合使用。curl
一、配置基于域名的虚拟主机tcp
[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; } } } 这样,一个基于域名的站点就建立好了。
二、建立域名对应的站点目录及文件
[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
三、检查语法并重载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:\Windows\System32\drivers\etc\目录下 在hosts文件中的添加方法和Linux同样。
四、配置多个基于域名的虚拟主机
多个域名虚拟主机的配置的区别就是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
[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
5、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。