<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[张宴的博客]]></title> 
<link>http://zyan.cc/index.php</link> 
<description><![CDATA[Web系统架构与底层研发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[张宴的博客]]></copyright>
<item>
<link>http://zyan.cc/post/382/</link>
<title><![CDATA[Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 28 Nov 2008 03:27:18 +0000</pubDate> 
<guid>http://zyan.cc/post/382/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2008.11.28 转载请注明原文链接：<a href="http://blog.zyan.cc/post/382/" target="_blank">http://blog.zyan.cc/post/382/</a>]<br/><br/>　　今天在配置Nginx + PHP + <a href="http://www.mediawiki.org/" target="_blank">MediaWiki</a>中，发现一个问题：MediaWiki所在的Nginx虚拟主机绑定了多个域名，但是不管通过什么域名访问MediaWiki首页，都会被跳转到其中的一个域名上。Nginx配置文件中没有相关的rewrite跳转规则，那么就应该是MediaWiki的PHP程序做的跳转，但是，遍历了MediaWiki目录下的所有文件以及查询了MySQL数据库中的每个表，都没有发现记录有这个域名。后来，通过查看源代码发现MediaWiki是根据$_SERVER['SERVER_NAME']做的跳转，顺藤摸瓜，发现了下列问题：<br/><br/>　　在一个Nginx虚拟主机中，可以绑定多个server_name，例如：<br/>　　<a href="http://zyan.cc/attachment/200811/nginx_config.gif" target="_blank"><img src="http://zyan.cc/attachment/200811/nginx_config.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　而server_name的先后顺序的不同，对PHP程序中使用$_SERVER["SERVER_NAME"]或getenv('SERVER_NAME')获取服务器域名是有影响的：<br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2094</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>lihaixin &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 28 Nov 2008 14:35:26 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2094</guid> 
<description>
<![CDATA[ 
	关注nginx，关注张宴！
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2105</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>jack &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 29 Nov 2008 17:57:36 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2105</guid> 
<description>
<![CDATA[ 
	关注张宴的博客，关注张宴的技术文章<br/>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2114</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>yak &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Dec 2008 06:05:54 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2114</guid> 
<description>
<![CDATA[ 
	nginx 如何自定义日志 比如图请求,js,css请示不记录
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2241</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>CELL &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 13 Dec 2008 05:56:04 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2241</guid> 
<description>
<![CDATA[ 
	用 $_SERVER[&quot;HTTP_HOST&quot;] 就可以了
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2423</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>sheaven &lt;sheaven@qq.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 16 Jan 2009 05:04:50 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2423</guid> 
<description>
<![CDATA[ 
	这个问题或许可以在server&#123;&#125;里面添加 server_name_in_redirect &nbsp;off; 来解决
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment2888</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>sim &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 26 Mar 2009 07:33:21 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment2888</guid> 
<description>
<![CDATA[ 
	张兄 关于nginx想请教下 我在装了nginx后编译安装php 大多参数能编译上 但是有几条参数怎么也编译不上<br/><br/>Notice: Following unknown configure options were used:<br/>--with-zip<br/>--enable-url-inclubes<br/>--enable-versioning<br/>--enable-trace-vars<br/><br/>是不是nginx不支持这4条php编译参数？ 还是需要用别的方法？ 请指教啊
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment3035</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>VPS主机 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 16 Apr 2009 08:46:40 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment3035</guid> 
<description>
<![CDATA[ 
	关注张宴的博客··
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment3036</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>crystal &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 16 Apr 2009 08:47:30 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment3036</guid> 
<description>
<![CDATA[ 
	张宴只写技术性的文章吗？
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment3994</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>Daze &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 05 Sep 2009 11:04:30 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment3994</guid> 
<description>
<![CDATA[ 
	这都是什么跟什么啊？说那么多都没说到点子上！<br/>“$_SERVER[&quot;SERVER_NAME&quot;]或getenv(&#039;SERVER_NAME&#039;)获取的始终将是Nginx server_name配置中的第一个域名”，那是因为 Nginx 把别的都重定向到这第一个域名，想要 Nginx 提供 requested Host header，用 server_name_in_redirect off 啊！
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment4006</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>奔跑的企鹅 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 09 Sep 2009 04:04:42 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment4006</guid> 
<description>
<![CDATA[ 
	server_name_in_redirect off;<br/>我把它加到server里面了<br/>可是还是不能用啊<br/>各位啊有没有更好的方法呢？<br/>我的 QQ 366204135<br/>用http_host很不爽啊！
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment4083</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>Daze &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Wed, 23 Sep 2009 12:52:12 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment4083</guid> 
<description>
<![CDATA[ 
	对不起，我错了，我上面说的也并不完整。<br/>在某些情况下（具体可参考 wiki.nginx.org），Nginx 内部重定向规则会被启动，例如，当 URL 指向一个目录并且在最后没有包含“/”时，Nginx 内部会自动的做一个 301 重定向，这时会有两种情况：<br/>1、server_name_in_redirect on（默认），URL 重定向为： server_name 中的第一个域名 + 目录名 + /；<br/>2、server_name_in_redirect off，URL 重定向为： 原 URL 中的域名 + 目录名 + /。<br/><br/>当你有多个域名要指向同一个虚拟主机，并且你自己写 301 重定向规则把它们合并到某一个域名时，情况就更复杂了：<br/>首先，nginx 检查 URL，如果符合条件，就用该规则（你写的）做第一遍重定向，接着，检查新生成的 URL，如果符合内部自动重定向之条件，就用前面提到的规则再做一次重定向。<br/><br/>至于 PHP 的 $_SERVER[&quot;SERVER_NAME&quot;]，在 nginx 中默认是由 nginx 的变量 $server_name 提供，这时它和重定向没有关系，始终是 server_name 设置中的第一个域名，但这是可以被改变的，在你的 nginx 配置中找到 fastcgi_param 部分，修改<br/>fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;$server_name;<br/>为<br/>fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;$host;<br/>但现在就要注意了，此时的 $_SERVER[&quot;SERVER_NAME&quot;] 会受你写的和 nginx 自己的重定向规则所影响而变化。<br/><br/>现在就清楚了，如果 MediaWiki 是通过 $_SERVER[&quot;SERVER_NAME&quot;] 来自己处理 URL 的话，那么在 nginx + php 的默认环境下，它获得的将始终是 server_name 设置中的第一个域名，所以造成了“不管通过什么域名访问 MediaWiki 首页，都会被跳转到其中的一个域名上。”，这不是 nginx 的重定向造成的，虽然默认 server_name_in_redirect 是 on，但这个指令的影响范围仅仅只是 nginx 自己内部的重定向规则，所以，当你在 nginx + php 的环境中使用多域名虚拟主机，并且你的 php 库、框架、代码大量使用 $_SERVER[&quot;SERVER_NAME&quot;] 时，你也许应该：<br/>1、设置 fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;$host;<br/>2、设置 server_name_in_redirect off; 让 nginx 在处理自己内部重定向时不默认使用&nbsp;&nbsp;server_name 设置中的第一个域名；<br/>3、不要使用 nginx 的 rewrite 规则来重定向、合并多个域名。<br/>当然，后俩条是完全可选的，前提是你清楚你在做什么并且小心处理这时的&nbsp;&nbsp;$_SERVER[&quot;SERVER_NAME&quot;]，也许更好的做法是保持 fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;$server_name; ，然后合理使用 $_SERVER[&quot;SERVER_NAME&quot;] 和 $_SERVER[&quot;HTTP_HOST&quot;]。<br/><br/>这个问题确实很微妙，也许我的理解还是不完全，好在还有 curl ，慢慢研究了。 :-)<br/><br/>P.S. nginx 0.7.x 之前的版本还有一个指令 optimize_server_names 会影响内部重定向规则。
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment6069</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>ffff &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 01 Jun 2010 04:58:51 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment6069</guid> 
<description>
<![CDATA[ 
	这个blog是用wiki作的吗？
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment6428</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>吕滔 &lt;admin@lvtao.net&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 24 Jul 2010 05:35:04 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment6428</guid> 
<description>
<![CDATA[ 
	张老师，我为什么总是配置出错了。。<br/><br/>尤其是 mysql
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment8017</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>a12333a &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sun, 27 Mar 2011 05:54:31 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment8017</guid> 
<description>
<![CDATA[ 
	谢谢张老师，学习了
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/#blogcomment8748</link>
<title><![CDATA[[评论] Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>dresses &lt;lh286227785@hotmail.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 26 Jul 2011 02:14:16 +0000</pubDate> 
<guid>http://zyan.cc/post/382/#blogcomment8748</guid> 
<description>
<![CDATA[ 
	Here are a variety of the empire &lt;a href=&quot;<a href="http://www.romancearound.com/&quot;&gt;dresses&lt;/a&gt;" target="_blank">http://www.romancearound.com/&quot;&gt;dresses&lt;/a&gt;</a>, I want to introduce yourself. The first shoulder chiffon &lt;a href=&quot;<a href="http://www.romancearound.com/bridesmaid-dresses-c-197.html&quot;&gt;bridesmaid" target="_blank">http://www.romancearound.com/bridesmaid-dresses-c-197.html&quot;&gt;bridesmaid</a> dresses&lt;/a&gt; empire style wedding will &lt;a href=&quot;<a href="http://www.romancearound.com/wedding-dresses-trumpet-wedding-dresses-c-180_253.html&quot;&gt;trumpet" target="_blank">http://www.romancearound.com/wedding-dresses-trumpet-wedding-dresses-c-180_253.html&quot;&gt;trumpet</a> wedding dresses&lt;/a&gt;.This Tia cowries, dressed in her wedding day.Chiffon of the empire the best clothes, because they are the size of the drum as complete motion from the wind the site makes it easy to create a plan impression.One romantic flowers floating above shoulder function and help catch the eyes of the cut of the chest and neck problems, so that more comprehensive coverage of physical illusion bust.Clothing do not like big long torso, short legs, or pear-shaped&lt;a href=&quot;<a href="http://www.romancearound.com/bridesmaid-dresses-ankle-length-bridesmaid-dresses-c-197_198.html&quot;&gt;ankle" target="_blank">http://www.romancearound.com/bridesmaid-dresses-ankle-length-bridesmaid-dresses-c-197_198.html&quot;&gt;ankle</a> length bridesmaid dresses&lt;/a&gt; figure.special is also suitable for pregnant brides since the mobility as a dress can accommodate more Many of the abdomen.
]]>
</description>
</item>
</channel>
</rss>