一 虚拟主机
1.1 虚拟主机概念
对于Nginx而言,每一个虚拟主机相当于一个在同一台服务器中却相互独立的站点,从而实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。
1.2 虚拟主机类型
通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置:
- 基于 IP 的虚拟主机(较少使用)
- 基于域名的虚拟主机
- 基于端口的虚拟主机
二 基于IP虚拟主机
2.1 配置多IP地址
[root@master ~]# ifconfig ens33:0 192.168.1.221 broadcast 192.168.1.255 netmask 255.255.255.0 [root@master ~]# ip add |grep 192 inet 192.168.1.220/24 brd 192.168.1.255 scope global noprefixroute ens33 inet 192.168.1.221/24 brd 192.168.1.255 scope global secondary ens33:0
提示:如上在同一台主机添加多个IP地址。
2.2 创建站点目录
[root@master ~]# mkdir /usr/share/nginx/ipvhost01/ [root@master ~]# mkdir /usr/share/nginx/ipvhost02/ [root@master ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html [root@master ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html
2.3 配置虚拟主机
[root@master ~]# vi /etc/nginx/conf.d/ipvhost01.conf server { listen 80; #监听端口 server_name 192.168.1.220; #配置虚拟主机名和IP location / { root /usr/share/nginx/ipvhost01; #请求匹配路径 index index.html; #指定主页 access_log /var/log/nginx/ipvhost01.access.log main; error_log /var/log/nginx/ipvhost01.error.log warn; } } server { listen 80; server_name 192.168.1.221; location / { root /usr/share/nginx/ipvhost02; index index.html; access_log /var/log/nginx/ipvhost02.access.log main; error_log /var/log/nginx/ipvhost02.error.log warn; } }
[root@master ~]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@master ~]# nginx -s reload
2.4 确认验证
三 基于域名虚拟主机
3.1 创建站点目录
[root@master ~]# mkdir /usr/share/nginx/webvhost01/ [root@master ~]# mkdir /usr/share/nginx/webvhost02/ [root@master ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html [root@master ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html
3.2 配置虚拟主机
[root@master ~]# vi /etc/nginx/conf.d/webvhost.conf server { listen 80; server_name webvhost01.odocker.com; location / { root /usr/share/nginx/webvhost01; index index.html; access_log /var/log/nginx/webvhost01.access.log main; error_log /var/log/nginx/webvhost01.error.log warn; } } server { listen 80; server_name webvhost02.odocker.com; location / { root /usr/share/nginx/webvhost02; index index.html; access_log /var/log/nginx/webvhost02.access.log main; error_log /var/log/nginx/webvhost02.error.log warn; } }
[root@master ~]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@master ~]# nginx -s reload
3.3 确认验证
[root@master ~]# curl webvhost01.odocker.com <h1>Webvhost01</h1> [root@master ~]# curl webvhost02.odocker.com <h1>Webvhost02</h1>
四 基于端口虚拟主机
4.1 创建站点目录
[root@master ~]# mkdir /usr/share/nginx/portvhost01/ [root@master ~]# mkdir /usr/share/nginx/portvhost02/ [root@master ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html [root@master ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html
[root@master ~]# vi /etc/nginx/conf.d/portvhost.conf server { listen 8080; server_name portvhost01.odocker.com; location / { root /usr/share/nginx/portvhost01; index index.html; access_log /var/log/nginx/portvhost01.access.log main; error_log /var/log/nginx/portvhost01.error.log warn; } } server { listen 8081; server_name portvhost02.odocker.com; location / { root /usr/share/nginx/portvhost02; index index.html; access_log /var/log/nginx/access_portvhost02.log main; } } ~
[root@master ~]# nginx -t -c /etc/nginx/nginx.conf nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@master ~]# nginx -s reload
3.3 确认验证
[root@master ~]# curl portvhost01.odocker.com:8080 <h1>Portvhost01</h1> [root@master ~]# curl portvhost02.odocker.com:8081 <h1>Portvhost01</h1>