nginx基于IP多虚拟主机配置

【基于IP多虚拟主机】
环境准备
1.添加别名
ifconfig eth0:1 10.0.0.145 netmask 255.255.255.0  broadcast 10.0.0.255 up
ifconfig eth0:2 10.0.0.146 netmask 255.255.255.0  broadcast 10.0.0.255 up
此时一共准备了3个IP
[ html]# ifconfig |grep -E  "^.*inet 10"
        inet 10.0.0.200  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 10.0.0.145  netmask 255.255.255.0  broadcast 10.0.0.255
        inet 10.0.0.146  netmask 255.255.255.0  broadcast 10.0.0.255


【开始实践】
1.给nginx添加include包含语法,让其他目录下的配置文件,导入到nginx.conf中,这样的写法,
能够让nginx每一个配置文件,看起来更简洁,更清晰

修改nginx.conf,在http{}标签中的最后一行,添加如下参数
include  extra/*.conf;

2.在nginx.conf中修改第一个虚拟主机配置
 server {
        listen      10.0.0.200:80;
        server_name  localhost;
        
        charset utf-8;
        
        location / {
            root   /www/200;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    
    
3.在/opt/tngx232/conf/extra/145.conf中编辑第二个虚拟主机配置
mkdir /opt/tngx232/conf/extra
[ extra]# vim 145.conf
server {
        listen      10.0.0.145:80;
        server_name  localhost;
        
        charset utf-8;
        
        location / {
            root   /www/145;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


4.在在/opt/tngx232/conf/extra/146.conf中编辑第三个虚拟主机配置
server {
        listen      10.0.0.146:80;
        server_name  localhost;
        
        charset utf-8;
        
        location / {
            root   /www/146;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


5.编辑完成后,检查nginx语法
[ extra]# nginx  -t
nginx: the configuration file /opt/tngx232/conf/nginx.conf syntax is ok
nginx: configuration file /opt/tngx232/conf/nginx.conf test is successful


6.重启nginx
[ extra]# nginx -s stop
[ extra]# nginx


7.准备3个基于IP的站点内容

mkdir -p /www/{200.145.146}
[ extra]# echo "我是110,hello man." >  /www/200/index.html
[ extra]# echo "我是200,hello man." >  /www/200/index.html
[ extra]# echo "我是145,hello man." >  /www/145/index.html
[ extra]# echo "我是146,hello man." >  /www/146/index.html


8.测试访问
[ extra]# curl 10.0.0.200
我是200,hello man.
[ extra]# curl 10.0.0.145
我是145,hello man.
[ extra]# curl 10.0.0.146
我是146,hello man.

相关推荐