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失效。

总结

1、一台服务器可以配置多个ip

2、一个ip可以有多个域名;

3、文中一台虚拟机配置了多个ip是为了模拟局域网中其他服务器;而不是为了秀一个服务器可以配置多个ip,这样干在实际中没啥意义;

4、实际操作中一般将 listen 后面接端口而不接ip,因为接了ip后就只能通过ip匹配到相对应的server;

5、举例说明

server {
        listen       90;
        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;
        }
   }

首先我们在其他电脑上输入 www.test151.com:90访问nginx时,这个时候找到的就是当前这个server代码;nginx所在服务器上会有配置host文件,里面会写上www.test151.com指向的是局域网中的那台机器,这个时候nginx就会将请求转发到host中所配置的服务器的相应端口中;

相关推荐

  1. nginx.conflistenserver_name意义

    2024-02-20 16:22:03       30 阅读
  2. vue笔记$listeners

    2024-02-20 16:22:03       34 阅读
  3. 汇编中$+6意义理解

    2024-02-20 16:22:03       32 阅读
  4. 整体意义构成构建

    2024-02-20 16:22:03       14 阅读
  5. 揭秘:抽象类接口之间区别意义

    2024-02-20 16:22:03       10 阅读
  6. 安全合规漏洞扫描重要意义

    2024-02-20 16:22:03       38 阅读
  7. Postgresql源码(122)Listen / Notify事务联动机制

    2024-02-20 16:22:03       21 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-20 16:22:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-20 16:22:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-20 16:22:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-20 16:22:03       18 阅读

热门阅读

  1. 奇门遁甲 API接口

    2024-02-20 16:22:03       44 阅读
  2. Element UI组件的安装及使用

    2024-02-20 16:22:03       26 阅读
  3. kube-ovn自定义vpc

    2024-02-20 16:22:03       23 阅读
  4. 开源BLHELI-S 代码详细解读(四)

    2024-02-20 16:22:03       22 阅读
  5. Vue3中watch与watchEffect的区别

    2024-02-20 16:22:03       24 阅读
  6. OSS业务存储适配器模式

    2024-02-20 16:22:03       21 阅读
  7. python数据分析numpy基础之var求数组方差

    2024-02-20 16:22:03       26 阅读
  8. 缓存使用常见思路及问题

    2024-02-20 16:22:03       18 阅读
  9. BUG:required a single bean, but 2 were found:

    2024-02-20 16:22:03       22 阅读