nginx 域名转发_nginx配置文件nginx.conf之server及server_name的意义详解

参考连接:

nginx配置文件nginx.conf之server及server_name的意义详解_我在关山口开摇摇车的博客-CSDN博客

本人在学习nginx的时候被server_name的意义困扰了很久。又是查资料,又是请教人。最后还是自己测试出来的。

你搜到这篇文章说明你已经经过了基本的配置,但是还不懂其中的含义。

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

本文主要是解释server_name的意义,文章最后会解释server在整个访问请求的流程;请注意文中红色文字;

先上一段配置


  1. server {

  2. listen ip:端口;
  3. # 当listen出现了ip时,server_name就失去了意义。所以不配置也罢了。
  4. #server_name 域名;

  5. access_log 日志地址1;

  6. error_log 日志地址2;

  7. location / {

  8. root /data/www/151;

  9. index index.html index.htm;

  10. }

  11. }

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

验证如下:

第一步:在客户端配置host,用于域名jie'xi


  1. #虚拟机

  2. 192.168.231.102 http://backend.xpe.com

  3. 192.168.231.151 http://www.test151.com

  4. 192.168.231.152 http://www.test152.com

  5. 192.168.231.153 http://www.test153.com

  6. 192.168.231.151 http://www.test154.com

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


  1. # ifconfig

  2. 打印出的结果

  3. eth0 Link encap:Ethernet HWaddr 00:0C:29:BB:AC:FB

  4. inet addr:192.168.231.102 Bcast:192.168.231.255 Mask:255.255.255.0

  5. inet6 addr: fe80::20c:29ff:febb:acfb/64 Scope:Link

  6. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

  7. RX packets:123455 errors:0 dropped:0 overruns:0 frame:0

  8. TX packets:123953 errors:0 dropped:0 overruns:0 carrier:0

  9. collisions:0 txqueuelen:1000

  10. RX bytes:34612737 (33.0 MiB) TX bytes:81191887 (77.4 MiB)

  11. 注意 eth0 下面的eth0是对应的,你的虚拟机可能不同

  12. # ifconfig eth0:1 192.168.231.151/24 up

  13. # ifconfig eth0:2 192.168.231.152/24 up

  14. # ifconfig eth0:3 192.168.231.153/24 up

  15. 再次执行

  16. # ifconfig

  17. 可以看到多了三个ip

第三步:(非必须)

3个IP对应的域名如下,配置服务端的host文件便于测试
# vim /etc/hosts
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.231.151 http://www.test151.com
192.168.231.152 http://www.test152.com
192.168.231.153 http://www.test153.com

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

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


  1. # mkdir -p /usr/local/nginx/test/data/www

  2. # cd /usr/local/nginx/test/data/www

  3. # mkdir 151

  4. # mkdir 152

  5. # mkdir 153

  6. # echo "192.168.2.151" > 151/index.html; echo "192.168.2.152" > 152/index.html;echo "192.168.2.153" > 153/index.html

  7. [root@localhost www]# ls

  8. 151 152 153


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

  1. #通过ip配置

  2. server {

  3. listen 192.168.231.151:80;
  4. #server_name www.test151.com;

  5. access_log /usr/local/nginx/test/data/logs/www.test151.com.log main;

  6. error_log /usr/local/nginx/test/data/logs/www.test151.com.error.log;

  7. location / {

  8. root /usr/local/nginx/test/data/www/151;

  9. index index.html index.htm;

  10. }

  11. }

  12. server {

  13. listen 192.168.231.152:80;
  14. #server_name www.test152.com;

  15. access_log /usr/local/nginx/test/data/logs/www.test152.com.log main;

  16. error_log /usr/local/nginx/test/data/logs/www.test152.com.error.log;

  17. location / {

  18. root /usr/local/nginx/test/data/www/152;

  19. index index.html index.htm;

  20. }

  21. }

  22. server {

  23. listen 192.168.231.153:80;
  24. #server_name www.test153.com;

  25. access_log /usr/local/nginx/test/data/logs/www.test153.com.log main;

  26. error_log /usr/local/nginx/test/data/logs/www.test153.com.error.log;

  27. location / {

  28. root /usr/local/nginx/test/data/www/153;

  29. index index.html index.htm;

  30. }

  31. }

客户端访问结果如下

bcf3df7820162e91fe1476220ac15783.png

804e2ce8337e405d9f9620fede8f48d8.png

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

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

e498351915fd88dac91fb920432f6c6e.png

但是使用ip就只会出现192.168.231.151的资源

e5cba7ceed59eb8c83d153ca729e16df.png

这是因为通过其他ip也能请求到nginx,但是匹配不到相应的server,这个时候就会使用第一个server。

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

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

c758b5e660a4ed394236e139cb7aebde.png

00af7935c1961d97a6c3a75e46b31759.png

但是使用http://www.test153.com访问的到的是151的资源

3b7f4a62df303fb60029648f28f5b2eb.png

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

————————————————————————————————————————————

分割线

——————————————————————————————————————————————

看到评论中还有很多疑惑我来解释下吧;

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

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

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

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

5、举例说明


  1. server {

  2. listen 90;

  3. server_name www.test151.com;

  4. access_log /usr/local/nginx/test/data/logs/www.test151.com.log main;

  5. error_log /usr/local/nginx/test/data/logs/www.test151.com.error.log;

  6. location / {

  7. root /usr/local/nginx/test/data/www/151;

  8. index index.html index.htm;

  9. }

  10. }

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值