服务器 发布日期:2024/12/27 浏览次数:1
对于Nginx而言,每一个虚拟主机相当于一个在同一台服务器中却相互独立的站点,从而实现一台主机对外提供多个 web 服务,每个虚拟主机之间是独立的,互不影响的。
通过 Nginx 可以实现虚拟主机的配置,Nginx 支持三种类型的虚拟主机配置:
[root@nginx ~]# ifconfig eth0:0 172.24.8.70 broadcast 172.24.8.255 netmask 255.255.255.0 [root@nginx ~]# ip addr | grep 172 inet 172.24.8.71/24 brd 172.24.8.255 scope global noprefixroute eth0 inet 172.24.8.72/24 brd 172.24.8.255 scope global secondary eth0:0
提示:如上在同一台主机添加多个IP地址。
[root@nginx ~]# mkdir /usr/share/nginx/ipvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/ipvhost02/ [root@nginx ~]# echo '<h1>Ipvhost01</h1>' > /usr/share/nginx/ipvhost01/index.html [root@nginx ~]# echo '<h1>Ipvhost02</h1>' > /usr/share/nginx/ipvhost02/index.html
[root@nginx ~]# vi /etc/nginx/conf.d/ipvhost.conf server { listen ; #监听端口 server_name ipvhost.odocker.com ...; #配置虚拟主机名和IP location / { root /usr/share/nginx/ipvhost; #请求匹配路径 index index.html; #指定主页 access_log /var/log/nginx/ipvhost.access.log main; error_log /var/log/nginx/ipvhost.error.log warn; } } server { listen ; server_name ipvhost.odocker.com ...; location / { root /usr/share/nginx/ipvhost; index index.html; access_log /var/log/nginx/ipvhost.access.log main; error_log /var/log/nginx/ipvhost.error.log warn; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 [root@nginx ~]# nginx -s reload #重载配置文件
浏览器访问:ipvhost01.odocker.com。
clipboard
浏览器访问:ipvhost02.odocker.com。
clipboard
[root@nginx ~]# mkdir /usr/share/nginx/webvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/webvhost02/ [root@nginx ~]# echo '<h1>Webvhost01</h1>' > /usr/share/nginx/webvhost01/index.html [root@nginx ~]# echo '<h1>Webvhost02</h1>' > /usr/share/nginx/webvhost02/index.html
[root@nginx ~]# vi /etc/nginx/conf.d/webvhost.conf server { listen ; server_name webvhost.odocker.com; location / { root /usr/share/nginx/webvhost; index index.html; access_log /var/log/nginx/webvhost.access.log main; error_log /var/log/nginx/webvhost.error.log warn; } } server { listen ; server_name webvhost.odocker.com; location / { root /usr/share/nginx/webvhost; index index.html; access_log /var/log/nginx/webvhost.access.log main; error_log /var/log/nginx/webvhost.error.log warn; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 [root@nginx ~]# nginx -s reload #重载配置文件
浏览器访问:webvhost01.odocker.com。
clipboard
浏览器访问:webvhost02.odocker.com。
clipboard
[root@nginx ~]# mkdir /usr/share/nginx/portvhost01/ [root@nginx ~]# mkdir /usr/share/nginx/portvhost02/ [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost01/index.html [root@nginx ~]# echo '<h1>Portvhost01</h1>' > /usr/share/nginx/portvhost02/index.html
[root@nginx ~]# vi /etc/nginx/conf.d/portvhost.conf server { listen ; server_name portvhost.odocker.com; location / { root /usr/share/nginx/portvhost; index index.html; access_log /var/log/nginx/portvhost.access.log main; error_log /var/log/nginx/portvhost.error.log warn; } } server { listen ; server_name portvhost.odocker.com; location / { root /usr/share/nginx/portvhost; index index.html; access_log /var/log/nginx/access_portvhost.log main; } }
[root@nginx ~]# nginx -t -c /etc/nginx/nginx.conf #检查配置文件 [root@nginx ~]# nginx -s reload #重载配置文件
浏览器访问:portvhost01.odocker.com:8080
clipboard
浏览器访问:portvhost02.odocker.com:8081
clipboard