相同server_name多个虚拟主机优先级访问

1、在/etc/nginx/conf.d/添加两个虚拟主机server1.conf、server2.conf
server1.conf:
server {
    listen       443;
    server_name 127.0.0.1;
 
 
     location / {
        root /home/testzq/app/code1;
        index index.html;
    }
}
server2.conf:
server {
    listen       443;
    server_name 127.0.0.1;
 
 
     location / {
        root /home/testzq/app/code2;
        index index.html;
    }
}
 两配置文件的区别:
[root@zq conf.d]# diff server1.conf  server2.conf
6c6
<         root /home/testzq/app/code1;
---
>         root /home/testzq/app/code2;
访问页面编写:
[root@zq app]# cat /home/testzq/app/code1/index.html
<html>
<head>
    <meta charset="utf-8">
    <title> server 1</title>
</head>
<body>
    <h1>server 1</h1>
</body>
</html>
[root@zq app]# cat /home/testzq/app/code2/index.html
<html>
<head>
    <meta charset="utf-8">
    <title> server 2</title>
</head>
<body>
    <h1>server 2</h1>
</body>
</html>
2、测试nginx配置文件及加载配置
[root@zq conf.d]# nginx -tc /etc/nginx/nginx.conf
nginx: [warn] conflicting server name "127.0.0.1" on 0.0.0.0:443, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
 
[root@zq conf.d]# nginx -s reload -c /etc/nginx/nginx.conf
nginx: [warn] conflicting server name "127.0.0.1" on 0.0.0.0:443, ignored
3、访问https://ip/,看到的是 server 1,优先级取的是server1.conf
把server1.conf改名server3.conf,重加载nginx -s reload -c /etc/nginx/nginx.conf,再访问看到的是server 2,此时访问的是server2.conf
4、结论
Nginx读取配置文件的时候是按照文件名顺序进行读取的,server1.conf比server2.conf靠前。
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值