nginx.conf之listen与server_name的意义

server_name 为虚拟服务器的识别标志,匹配到特定的server块,转发到对应的应用服务器中去。

先上一段配置

server {
        listen       ip:端口;
        # 当listen出现了ip时,server_name就失去了意义。所以不配置也罢了。
        #server_name  域名;
 
        access_log   日志地址1;
        error_log    日志地址2;
 
        location / {
            root   /data/www/151;
            index  index.html index.htm;
        }
    }

客户端通过域名访问服务器时会将域名与被解析的ip一同放在请求中。当请求到了nginx中时。nginx会先去匹配ip,如果listen中没有找到对应的ip,就会通过域名进行匹配,匹配成功以后,再匹配端口。当这三步完成,就会找到对应的server的location对应的资源。

验证如下:

第一步:在客户端配置host,用于域名解析

[root@client html]# cat /etc/hosts
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com 

第二步:给虚拟机(服务端,以下这两个词会混用)配置另外三个ip,用于模拟多台服务器;

# ifconfig
 
打印出的结果
eth0      Link encap:Ethernet  HWaddr 00:0C:29:BB:AC:FB  
          inet addr:192.168.2.19  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:febb:acfb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:123455 errors:0 dropped:0 overruns:0 frame:0
          TX packets:123953 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:34612737 (33.0 MiB)  TX bytes:81191887 (77.4 MiB)
注意   eth0  下面的eth0是对应的,你的虚拟机可能不同
# ifconfig eth0:1 192.168.2.151/24 up
# ifconfig eth0:2 192.168.2.152/24 up
# ifconfig eth0:3 192.168.2.153/24 up
 
再次执行
# ifconfig
 
可以看到多了三个ip

 第三步:

[root@nginx]# cat /etc/hosts
192.168.2.151 www.test151.com
192.168.2.152 www.test152.com
192.168.2.153 www.test153.com 

第四步:建立相关的nginx的文件

1.建立虚拟主机存放网页的根目录,并创建首页文件index.html

# mkdir -p /usr/local/nginx/test/data/www
# mkdir /usr/local/nginx/test/data/logs/
# cd /usr/local/nginx/test/data/www
# mkdir 151
# mkdir 152
# mkdir 153
# echo "192.168.2.151" > 151/index.html; echo "192.168.2.152" > 152/index.html;echo "192.168.2.153" > 153/index.html
[root@localhost www]# ls
151  152  153

2.修改nginx.conf,将虚拟主机配置文件包含进主文件

server {
        listen       192.168.2.151:80;
        #server_name  www.test151.com;
 
        access_log   /usr/local/nginx/test/data/logs/www.test151.com.log main;
        error_log    /usr/local/nginx/test/data/logs/www.test151.com.error.log;
 
        location / {
            root   /usr/local/nginx/test/data/www/151;
            index  index.html index.htm;
        }
   }
 
 server {
        listen       192.168.2.152:80;
        #server_name  www.test152.com;
 
        access_log   /usr/local/nginx/test/data/logs/www.test152.com.log main;
        error_log    /usr/local/nginx/test/data/logs/www.test152.com.error.log;
 
        location / {
            root   /usr/local/nginx/test/data/www/152;
            index  index.html index.htm;
        }
   }
 
 server {
        listen       192.168.2.153:80;
        #server_name  www.test153.com;
 
        access_log   /usr/local/nginx/test/data/logs/www.test153.com.log main;
        error_log    /usr/local/nginx/test/data/logs/www.test153.com.error.log;
 
        location / {
            root  /usr/local/nginx/test/data/www/153;
            index  index.html index.htm;
        }
   }

客户端访问结果如下:

[root@client html]# curl 192.168.2.151
192.168.2.151
[root@client html]# curl 192.168.2.152
192.168.2.152
[root@client html]# curl 192.168.2.153
192.168.2.153
[root@client html]# curl www.test151.com
192.168.2.151
[root@client html]# curl www.test152.com
192.168.2.152
[root@client html]# curl www.test153.com
192.168.2.153
[root@client html]# 

可以使用ip和域名访问。但是是使用的ip进行匹配。

3.把server中的ip都删除,把server_name的注释都解开。重启nginx。再访问,发现用域名能访问到对应的资源,但是使用ip就只会出现192.168.2.151的资源。

这是因为通过域名和ip都能将请求发给nginx服务器,但是通过ip访问的时候nginx匹配不到任何内容,这个时候就会使用第一个server;但是通过域名则可以匹配到指定的后端服务器。

4.将 server_name = www.test153.com 的server的listen加上ip 192.168.2.152:80 ,重启nginx。

这个时候使用www.test152.com,或者192.168.2.152进行访问就可以得到 server_name = www.test153.com 的资源。

这是因为listen配上后server_name就失效了(优先匹配listen中的ip地址),当用域名访问www.test152.com的时候,客户端先将域名解析成ip地址,将请求发给nginx的时候,优先进行ip地址的匹配,当客户端通过ip地址访问的时候,直接匹配了ip地址。

如果server中配置了ip,那么我们就使用客户端带来的ip进行匹配,这个时候server_name失效。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值