007.Nginx虚拟主机
一 虚拟主机
1.1 虚拟主机概念
1.2 虚拟主机类型
- 基于 IP 的虚拟主机(较少使用)
- 基于域名的虚拟主机
- 基于端口的虚拟主机
二 基于IP虚拟主机
2.1 配置多IP地址
1 [root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0 2 [root@nginx ~]# ip addr | grep 172 3 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0 4 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
2.2 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/ 3 [root@nginx ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html 4 [root@nginx ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html
2.3 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/ipvhost01.conf 2 server { 3 listen 80; #监听端口 4 server_name ipvhost01.odocker.com 172.24.8.71; #配置虚拟主机名和IP 5 location / { 6 root /usr/share/nginx/ipvhost01; #请求匹配路径 7 index index.html; #指定主页 8 access_log /var/log/nginx/ipvhost01.access.log main; 9 error_log /var/log/nginx/ipvhost01.error.log warn; 10 } 11 } 12 server { 13 listen 80; 14 server_name ipvhost02.odocker.com 172.24.8.72; 15 location / { 16 root /usr/share/nginx/ipvhost02; 17 index index.html; 18 access_log /var/log/nginx/ipvhost02.access.log main; 19 error_log /var/log/nginx/ipvhost02.error.log warn; 20 } 21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
2.4 确认验证
三 基于域名虚拟主机
3.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/webvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/ 3 [root@nginx ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html 4 [root@nginx ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf 2 server { 3 listen 80; 4 server_name webvhost01.odocker.com; 5 location / { 6 root /usr/share/nginx/webvhost01; 7 index index.html; 8 access_log /var/log/nginx/webvhost01.access.log main; 9 error_log /var/log/nginx/webvhost01.error.log warn; 10 } 11 } 12 server { 13 listen 80; 14 server_name webvhost02.odocker.com; 15 location / { 16 root /usr/share/nginx/webvhost02; 17 index index.html; 18 access_log /var/log/nginx/webvhost02.access.log main; 19 error_log /var/log/nginx/webvhost02.error.log warn; 20 } 21 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证
四 基于端口虚拟主机
4.1 创建站点目录
1 [root@nginx ~]# mkdir /usr/share/nginx/portvhost01/ 2 [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/ 3 [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html 4 [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html
3.2 配置虚拟主机
1 [root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf 2 server { 3 listen 8080; 4 server_name portvhost01.odocker.com; 5 location / { 6 root /usr/share/nginx/portvhost01; 7 index index.html; 8 access_log /var/log/nginx/portvhost01.access.log main; 9 error_log /var/log/nginx/portvhost01.error.log warn; 10 } 11 } 12 server { 13 listen 8081; 14 server_name portvhost02.odocker.com; 15 location / { 16 root /usr/share/nginx/portvhost02; 17 index index.html; 18 access_log /var/log/nginx/access_portvhost02.log main; 19 } 20 }
1 [root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 2 [root@nginx ~]# nginx -s reload #重载配置文件
3.3 确认验证