[文章作者:张宴 本文版本:v1.0 最后修改:2008.11.28 转载请注明原文链接:http://blog.zyan.cc/post/382/]

  今天在配置Nginx + PHP + MediaWiki中,发现一个问题:MediaWiki所在的Nginx虚拟主机绑定了多个域名,但是不管通过什么域名访问MediaWiki首页,都会被跳转到其中的一个域名上。Nginx配置文件中没有相关的rewrite跳转规则,那么就应该是MediaWiki的PHP程序做的跳转,但是,遍历了MediaWiki目录下的所有文件以及查询了MySQL数据库中的每个表,都没有发现记录有这个域名。后来,通过查看源代码发现MediaWiki是根据$_SERVER['SERVER_NAME']做的跳转,顺藤摸瓜,发现了下列问题:

  在一个Nginx虚拟主机中,可以绑定多个server_name,例如:
  点击在新窗口中浏览此图片

  而server_name的先后顺序的不同,对PHP程序中使用$_SERVER["SERVER_NAME"]或getenv('SERVER_NAME')获取服务器域名是有影响的:
  点击在新窗口中浏览此图片

  点击在新窗口中浏览此图片

  $_SERVER["SERVER_NAME"]或getenv('SERVER_NAME')获取的始终将是Nginx server_name配置中的第一个域名,这一点在程序开发中需要注意。这第一个域名就相当于Apache虚拟主机配置中的ServerName,后面的域名就相当于Apache的ServerAlias。

  PS:以下是网友Daze的留言,希望对本文读者有所帮助。
引用
在某些情况下(具体可参考 wiki.nginx.org),Nginx 内部重定向规则会被启动,例如,当 URL 指向一个目录并且在最后没有包含“/”时,Nginx 内部会自动的做一个 301 重定向,这时会有两种情况:
1、server_name_in_redirect on(默认),URL 重定向为: server_name 中的第一个域名 + 目录名 + /;
2、server_name_in_redirect off,URL 重定向为: 原 URL 中的域名 + 目录名 + /。

当你有多个域名要指向同一个虚拟主机,并且你自己写 301 重定向规则把它们合并到某一个域名时,情况就更复杂了:
首先,nginx 检查 URL,如果符合条件,就用该规则(你写的)做第一遍重定向,接着,检查新生成的 URL,如果符合内部自动重定向之条件,就用前面提到的规则再做一次重定向。

至于 PHP 的 $_SERVER["SERVER_NAME"],在 nginx 中默认是由 nginx 的变量 $server_name 提供,这时它和重定向没有关系,始终是 server_name 设置中的第一个域名,但这是可以被改变的,在你的 nginx 配置中找到 fastcgi_param 部分,修改
fastcgi_param  SERVER_NAME    $server_name;

fastcgi_param  SERVER_NAME    $host;
但现在就要注意了,此时的 $_SERVER["SERVER_NAME"] 会受你写的和 nginx 自己的重定向规则所影响而变化。

现在就清楚了,如果 MediaWiki 是通过 $_SERVER["SERVER_NAME"] 来自己处理 URL 的话,那么在 nginx + php 的默认环境下,它获得的将始终是 server_name 设置中的第一个域名,所以造成了“不管通过什么域名访问 MediaWiki 首页,都会被跳转到其中的一个域名上。”,这不是 nginx 的重定向造成的,虽然默认 server_name_in_redirect 是 on,但这个指令的影响范围仅仅只是 nginx 自己内部的重定向规则,所以,当你在 nginx + php 的环境中使用多域名虚拟主机,并且你的 php 库、框架、代码大量使用 $_SERVER["SERVER_NAME"] 时,你也许应该:
1、设置 fastcgi_param  SERVER_NAME    $host;
2、设置 server_name_in_redirect off; 让 nginx 在处理自己内部重定向时不默认使用  server_name 设置中的第一个域名;
3、不要使用 nginx 的 rewrite 规则来重定向、合并多个域名。
当然,后俩条是完全可选的,前提是你清楚你在做什么并且小心处理这时的  $_SERVER["SERVER_NAME"],也许更好的做法是保持 fastcgi_param  SERVER_NAME    $server_name; ,然后合理使用 $_SERVER["SERVER_NAME"] 和 $_SERVER["HTTP_HOST"]。

这个问题确实很微妙,也许我的理解还是不完全,好在还有 curl ,慢慢研究了。 :-)

P.S. nginx 0.7.x 之前的版本还有一个指令 optimize_server_names 会影响内部重定向规则。


Tags: ,



技术大类 » Web服务器 | 评论(60) | 引用(3) | 阅读(125373)
pikbee Email Homepage
2018-12-15 16:25
Thank you so much. I'm glad to know another cool instagram search website to use.
918kiss Apk hack Email Homepage
2019-3-4 18:09
It is perfect time to make some plans for <a href="https://onegold888.com/918kiss.php">918kiss Apk hack</a>  the future and it is time to be happy. I've read this publish and if I may just I desire to suggest you few interesting issues or tips. Maybe you can write next articles referring to this article. I want to learn even more things approximately it!
砂缸过滤 Email Homepage
2019-5-13 10:00
name不弄好会导致什么后果
slope 2 Email Homepage
2022-3-29 11:19
I felt very happy while reading this site. This was really very informative site for me. I really liked it. This was really a cordial post     slope 2
miniokj
2022-4-19 12:43
Here I am and can only say thank you for a great post. Let's continue to do well.among us 2
jaypal Email Homepage
2022-5-26 13:39
https://www.emergenresearch.com/
shz Email
2022-7-17 02:15
This is really very nice post you shared, i like the post, thanks for sharing..  online dealings
shz Email
2022-7-17 02:18
This website and I conceive this internet site is really informative ! Keep on putting up!  online dealings
shzz Email
2022-7-17 02:18
I really enjoy reading and also appreciate your work.  online dealings
shz Email
2022-7-17 02:21
I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.  online dealings
shzz Email
2022-7-17 02:21
I read that Post and got it fine and informative. Please share more like that...  online dealings
shz Email
2022-7-17 02:24
Thanks a lot for one’s intriguing write-up. It’s actually exceptional. Searching ahead for this sort of revisions.  online dealings
shzz Email
2022-7-17 02:25
This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.  online dealings
shz Email
2022-7-17 02:27
Thanks for sharing this information. I really like your blog post very much. You have really shared a informative and interesting blog post .  online dealings
shzz Email
2022-7-17 02:29
Your site is truly cool and this is an extraordinary moving article.  online dealings
shz Email
2022-7-17 02:30
Thanks for providing recent updates regarding the concern, I look forward to read more.  online dealings
paul Email
2022-11-7 19:20
Excel your brand identity with utmost designs and completely responsive Shopify storefronts that provide a seamless online shopping experience to your potential visitors.  Shopify app development
seo queen
2023-2-22 00:17
maaf numpang permisi para agan semua1# <a href="https://web.facebook.com/hajarjahanamcikarang">hajar jahanam cikarang</a> 2# <a href=https://web.facebook.com/hajarjahanamcikarang>hajar jahanam cikarang</a> 3# https://web.facebook.com/hajarjahanamcikarang"" target="_blank">hajar jahanam cikarang 4# hajar jahanam cikarang 5# hajar jahanam cikarang
Lennon Homepage
2023-6-3 10:56
Depending on how a love dolls is used, it can give you the sensation of having sex with a real person and help you achieve orgasm.
ruberedwin9752 Email Homepage
2023-9-6 11:31
Where are cheap sex doll brothels located?cheap sex doll
分页: 3/4 第一页 上页 1 2 3 4 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]