计算机网络/计算机科学与应用/系统/运维/开发

13、Nginx 基于多IP的虚拟主机

Nginx 基于IP的多虚拟主机


1、准备好ip地址

ifconfig ens33:1 192.168.178.145 netmask 255.255.255.0 broadcast 192.168.178.255 up
ifconfig  ens33:1  192.168.178.146  netmask  255.255.255.0  broadcast  192.168.178.255 up
ifconfig  ens33:1  192.168.178.147  netmask  255.255.255.0  broadcast  192.168.178.255 up

添加nginx的配置,添加多个server{} 标签 让nginx支持基于ip的多虚拟主机,返回多个站点内容


2、给nginx添加include包含语法,让其他目录下的配置文件参数,导入到nginx.conf 中 这样的写法

修改nginx.conf  在http{}标签中的最后一行,添加如下参数,extra 文件夹和nginx.conf 文件夹为相对

include extra/*.conf;


3、在extra目录下,添加基于多个基于ip的虚拟主机配置

 第一个基于ip的虚拟主机,写在conf/nginx.conf 部分代码如下,当192.168.178.110请求到来,让nginx去/www/110文件夹下寻找资源

 location  /  {
     root /www/110;
     index index.html index.htm;
 }

4、添加192.168.178.145 虚拟主机配

在nginx.conf 主配置文件最后添加  include extra/*.conf

vim extra/145.conf

server {
    listen 192.168.178.145:80;
    server_name _;
    location / {
            root /www/145;
            index index.html;
     }
}


5、检查语法 

nginx -t


6、重载配置,在允许重启nginx的情况下

nginx -s stop
# 防止出现nginx有缓存的情况
nginx


7、准备3个基于ip的站点内容

mkdir -p /www/{110,145,146}
echo "I'm 110,hello man."> /www/110/index.html
echo "I'm 145,hello man."> /www/145/index.html
echo "I'm 146,hello man."> /www/146/index.html


8、curl测试端口是否通

curl 192.168.178.110
curl  127.0.0.1:81 端口测试

注意:这里如何浏览器可以访问,但是ping不通,查看安全组是否开放


9、使用浏览器查看效果

# 如果没改变,可以使用带参数形式访问
192.168.178.110/?name=zhangsan


再长的路,一步步也能走完,再短的路,不迈开双脚也无法到达

评论

^