<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>荷包蛋博客 &#187; nginx</title>
	<atom:link href="http://www.hebaodans.com/tag/nginx/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hebaodans.com</link>
	<description>好的心态是成功的开始</description>
	<lastBuildDate>Tue, 20 Jul 2010 17:32:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>nginx 502 bad故障原因及解决方法收集</title>
		<link>http://www.hebaodans.com/2009/05/n-g-i-n-x-5-0-2-b-a-d-gu-zhang-yuan-yin-ji-jie-jue-fang-fa-shou-ji/</link>
		<comments>http://www.hebaodans.com/2009/05/n-g-i-n-x-5-0-2-b-a-d-gu-zhang-yuan-yin-ji-jie-jue-fang-fa-shou-ji/#comments</comments>
		<pubDate>Fri, 15 May 2009 00:44:07 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[nginx]]></category>
		<category><![CDATA[502 bad gateway]]></category>
		<category><![CDATA[nginx 502]]></category>

		<guid isPermaLink="false">http://www.hebaodans.com/?p=418</guid>
		<description><![CDATA[如题，最近网站频繁出现502错误，简直无法正常运转，出现这种情况大多是php-cgi超时没有返回信息,或进程僵死等情况造成的，参考张宴的这篇关于502错误的解决办法(http://blog.s135.com/read.php?361)，并咨询系统管理员高手，我们的nginx已经配置到极致这些都已经老早做过修改了，但现在又出然出现。 经过分析将nginx的error log打开，发现&#8221;pstream sent too big header while reading response header from upstream&#8221;这样的错误提示，查阅了一下资料，大意是nginx缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大。参考老外写的修改办法增加了缓冲区容量大小设置,502问题彻底解决，后来系统管理员又对参数做了调整只保留了2个设置参数：client head buffer，fastcgi buffer size。 参考: http://www.sudone.com/nginx/nginx_400_bad_request.html http://blog.rackcorp.com/?p=14 二、昨天装上nginx后在高负载的时候，论坛上传图片或者执行较长时间脚本的时候就不停的出现502 Bad Gateway ，网上搜了，大多数都是张大师的那篇解决方案，他的解决方案是 http { &#8230;&#8230; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; &#8230;&#8230; } 增加了fastcgi的相应请求时间。但是我在实际中碰到了这个问题，设置到500，还是会出现，只是比我设置120的时候要少一些。后来发现主要是在一些post或者数据库操作的时候出现这种情况，静态页面是不会出现的。 反复的查问题，调试，也加大了CGI的进程数。 128 256再加上去可能会变得很慢。占用内存大了。 在php-fpm.conf设置中还有一项，可能当时没注意到，无意中改了这个值。 request_terminate_timeout 这个值是max_execution_time，就是fast-cgi的执行脚本时间。 0s 0s为关闭，就是无限执行下去。（当时装的时候没仔细看就改了一个数字） 发现，问题解决了，执行很长时间也不会出错了。 优化fastcgi中，还可以改改这个值5s 。看看效果 终于发现502的错误其实不是nginx的问题，哈哈 php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉，都会出现502错误 三、 一台服务器上运行着nginx php(fpm) xcache，访问量日均 300W pv左右 [...]]]></description>
			<content:encoded><![CDATA[<p>如题，最近网站频繁出现502错误，简直无法正常运转，出现这种情况大多是php-cgi超时没有返回信息,或进程僵死等情况造成的，参考张宴的这篇关于502错误的解决办法(http://blog.s135.com/read.php?361)，并咨询系统管理员高手，我们的<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>已经配置到极致这些都已经老早做过修改了，但现在又出然出现。</p>
<p>经过分析将<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>的error log打开，发现&#8221;pstream sent too big header while reading response header from upstream&#8221;这样的错误提示，查阅了一下资料，大意是<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大。参考老外写的修改办法增加了缓冲区容量大小设置,502问题彻底解决，后来系统管理员又对参数做了调整只保留了2个设置参数：client head buffer，fastcgi buffer size。</p>
<p>参考:</p>
<p>http://www.sudone.com/nginx/nginx_400_bad_request.html</p>
<p>http://blog.rackcorp.com/?p=14</p>
<p>二、昨天装上nginx后在高负载的时候，论坛上传图片或者执行较长时间脚本的时候就不停的出现502 Bad Gateway ，网上搜了，大多数都是张大师的那篇解决方案，他的解决方案是</p>
<p>http<br />
{<br />
&#8230;&#8230;<br />
fastcgi_connect_timeout 300;<br />
fastcgi_send_timeout 300;<br />
fastcgi_read_timeout 300;<br />
&#8230;&#8230;<br />
}</p>
<p>增加了fastcgi的相应请求时间。但是我在实际中碰到了这个问题，设置到500，还是会出现，只是比我设置120的时候要少一些。后来发现主要是在一些post或者数据库操作的时候出现这种情况，静态页面是不会出现的。</p>
<p>反复的查问题，调试，也加大了CGI的进程数。</p>
<p><value name="max_children">128</value></p>
<p>256再加上去可能会变得很慢。占用内存大了。</p>
<p>在php-fpm.conf设置中还有一项，可能当时没注意到，无意中改了这个值。</p>
<p>request_terminate_timeout</p>
<p>这个值是max_execution_time，就是fast-cgi的执行脚本时间。</p>
<p><value name="request_terminate_timeout">0s</value></p>
<p>0s为关闭，就是无限执行下去。（当时装的时候没仔细看就改了一个数字）</p>
<p>发现，问题解决了，执行很长时间也不会出错了。</p>
<p>优化fastcgi中，还可以改改这个值<value name="process_control_timeout">5s</value> 。看看效果</p>
<p>终于发现502的错误其实不是nginx的问题，哈哈</p>
<p>php-cgi进程数不够用、php执行时间长、或者是php-cgi进程死掉，都会出现502错误</p>
<p>三、</p>
<p>一台服务器上运行着nginx php(fpm) xcache，访问量日均 300W pv左右<br />
最近经常会出现这样的情况： php页面打开很慢，cpu使用率突然降至很低，系统负载突然升至很高，查看网卡的流量，也会发现突然降到了很低。这种情况只持续数秒钟就恢复了<br />
检查php-fpm的日志文件发现了一些线索<br />
Sep 30 08:32:23.289973 [NOTICE] fpm_unix_init_main(), line 271: getrlimit(nofile): max:51200, cur:51200<br />
Sep 30 08:32:23.290212 [NOTICE] fpm_sockets_init_main(), line 371: using inherited socket fd=10, &#8220;127.0.0.1:9000&#8243;<br />
Sep 30 08:32:23.290342 [NOTICE] fpm_event_init_main(), line 109: libevent: using epoll<br />
Sep 30 08:32:23.296426 [NOTICE] fpm_init(), line 47: fpm is running, pid 30587<br />
在这几句的前面，是1000多行的关闭children和开启children的日志<br />
原来，php-fpm有一个参数 max_requests ，该参数指明了，每个children最多处理多少个请求后便会被关闭，默认的设置是500。因为php是把请求轮询给每个children，在大流量下，每个childre到达max_requests所用的时间都差不多，这样就造成所有的children基本上在同一时间被关闭。<br />
在这期间，nginx无法将php文件转交给php-fpm处理，所以cpu会降至很低(不用处理php，更不用执行sql)，而负载会升至很高(关闭和开启children、nginx等待php-fpm)，网卡流量也降至很低(nginx无法生成数据传输给客户端)</p>
<p>解决问题很简单，增加children的数量，并且将 max_requests 设置未 0 或者一个比较大的值，重启php-fpm</p>
<p>四、</p>
<p><span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx-502/" title="View all posts in nginx 502" target="_blank">nginx 502</a></span>错误的原因比较多，是因为在代理模式下后端服务器出现问题引起的。这些错误一般都不是nginx本身的问题，一定要从后端找原因！但nginx把这些出错都揽在自己身上了，着实让nginx的推广者备受置疑，毕竟从字眼上理解，bad gateway？不就是bad nginx吗？让不了解的人看到，会直接把责任推在nginx身上，希望nginx下一个版本会把出错提示写稍微友好一些，至少不会是现在简单的一句502 Bad Gateway，另外还不忘附上自己的大名。</p>
<p>502错误最通常的出现情况就是后端主机当机，当然还有。在upstream配置里有这么一项配置：proxy_next_upstream，这个配置指定了nginx在从一个后端主机取数据遇到何种错误时会转到下一个后端主机，里头写上的就是会出现502的所有情况拉，默认是error timeout，error就是当机、断线之类的，timeout就是读取堵塞超时，比较容易理解。我一般是全写上的：</p>
<p>proxy_next_upstream error timeout invalid_header http_500 http_503;</p>
<p>不过现在可能我要去掉http_500这一项了，http_500指定后端返回500错误时会转一个主机，后端的jsp出错的话，本来会打印一堆stacktrace的错误信息，现在被502取代了。但公司的程序员可不这么认为，他们认定是nginx出现了错误，我实在没空跟他们解释502的原理了……</p>
<p>invalid_header我也没认真查清到底指的什么，我也很想先把它弄下来。</p>
<p>503错误就可以保留，因为后端通常是apache resin，如果apache死机就是error，但resin死机，仅仅是503，所以还是有必要保留的。</p>
<p>对nginx fastcgi使用的情况，我现在用得不多，不熟就不乱说了。</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.hebaodans.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.hebaodans.com/2009/05/n-g-i-n-x-5-0-2-b-a-d-gu-zhang-yuan-yin-ji-jie-jue-fang-fa-shou-ji/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>nginx+php 502 bad gateway解决方法</title>
		<link>http://www.hebaodans.com/2009/03/n-g-i-n-x-p-h-p-5-0-2-b-a-d-g-a-t-e-w-a-y-jie-jue-fang-fa/</link>
		<comments>http://www.hebaodans.com/2009/03/n-g-i-n-x-p-h-p-5-0-2-b-a-d-g-a-t-e-w-a-y-jie-jue-fang-fa/#comments</comments>
		<pubDate>Sat, 21 Mar 2009 09:32:16 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[nginx]]></category>
		<category><![CDATA[502]]></category>
		<category><![CDATA[bad gateway]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://www.hebaodans.com/?p=330</guid>
		<description><![CDATA[打开 /usr/local/php/etc/php-fpm.conf       调大以下两个参数(根据服务器实际情况，过大也不行）     &#60;value name=&#8221;max_children&#8221;&#62;1280&#60;/value&#62;       &#60;value name=&#8221;max_requests&#8221;&#62;1000&#60;/value&#62; 可同时参看 nginx 502 bad故障原因及解决方法收集 ]]></description>
			<content:encoded><![CDATA[<p>打开 /usr/local/php/etc/php-fpm.conf</p>
<p>      调大以下两个参数(根据服务器实际情况，过大也不行）</p>
<p>    &lt;value name=&#8221;max_children&#8221;&gt;1280&lt;/value&gt;</p>
<p>      &lt;value name=&#8221;max_requests&#8221;&gt;1000&lt;/value&gt;</p>
<p>可同时参看 <a href="http://www.hebaodans.com/index.php/2009/05/n-g-i-n-x-5-0-2-b-a-d-gu-zhang-yuan-yin-ji-jie-jue-fang-fa-shou-ji/" target="_self">nginx 502 bad故障原因及解决方法收集 </a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.hebaodans.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.hebaodans.com/2009/03/n-g-i-n-x-p-h-p-5-0-2-b-a-d-g-a-t-e-w-a-y-jie-jue-fang-fa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nginx php调用kill命令平滑重启nginx</title>
		<link>http://www.hebaodans.com/2009/02/n-g-i-n-x-p-h-p-diao-yong-k-i-l-l-ming-ling-ping-hua-zhong-qi-n-g-i-n-x/</link>
		<comments>http://www.hebaodans.com/2009/02/n-g-i-n-x-p-h-p-diao-yong-k-i-l-l-ming-ling-ping-hua-zhong-qi-n-g-i-n-x/#comments</comments>
		<pubDate>Sat, 14 Feb 2009 10:06:44 +0000</pubDate>
		<dc:creator></dc:creator>
				<category><![CDATA[Php]]></category>
		<category><![CDATA[centos]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[phpz执行root命令]]></category>
		<category><![CDATA[php执行系统命令]]></category>
		<category><![CDATA[sudo]]></category>

		<guid isPermaLink="false">http://www.hebaodans.com/?p=225</guid>
		<description><![CDATA[1.新建php文件，输入以下代码 ：   &#60;?php     passthru(escapeshellcmd(&#8220;sudo kill -HUP `cat /usr/local/nginx/nginx.pid`&#8221;)) ?&#62; 2.root权限登陆centos，执行 visudo ,最好用visudo编辑，不容易出现莫名其妙的预防错误。在打开的sudo文件末尾加入以下一行： www ALL=(root) NOPASSWD: /bin/kill, /usr/bin/kill （nginx以www用户运行) 3.如果www用户执行sudo会提示“sorry,sudo must be setuid root.”，登陆root，输入一下命令：    ls -l  /usr/bin/sudo chown root:root /usr/bin/sudo chmod 4755 /usr/bin/sudo reboot(好像可以不重启，如果不行再reboot) （有网友提到 直接chmod 4111 /usr/bin/sudo即可）]]></description>
			<content:encoded><![CDATA[<p>1.新建php文件，输入以下代码 ：</p>
<p>  &lt;?php</p>
<p>    passthru(escapeshellcmd(&#8220;<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/sudo/" title="View all posts in sudo" target="_blank">sudo</a></span> <span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/kill/" title="View all posts in kill" target="_blank">kill</a></span> -HUP `cat /usr/local/<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>/<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>.pid`&#8221;))</p>
<p>?&gt;</p>
<p>2.root权限登陆centos，执行 visudo ,最好用visudo编辑，不容易出现莫名其妙的预防错误。在打开的<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/sudo/" title="View all posts in sudo" target="_blank">sudo</a></span>文件末尾加入以下一行：</p>
<p>www ALL=(root) NOPASSWD: /bin/<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/kill/" title="View all posts in kill" target="_blank">kill</a></span>, /usr/bin/kill</p>
<p>（<span class='wp_keywordlink_affiliate'><a href="http://www.hebaodans.com/tag/nginx/" title="View all posts in nginx" target="_blank">nginx</a></span>以www用户运行)</p>
<p>3.如果www用户执行sudo会提示“sorry,sudo must be setuid root.”，登陆root，输入一下命令：</p>
<p>   ls -l  /usr/bin/sudo</p>
<p>chown root:root /usr/bin/sudo</p>
<p>chmod 4755 /usr/bin/sudo</p>
<p>reboot(好像可以不重启，如果不行再reboot)</p>
<p>（有网友提到 直接chmod 4111 /usr/bin/sudo即可）</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.hebaodans.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.hebaodans.com/2009/02/n-g-i-n-x-p-h-p-diao-yong-k-i-l-l-ming-ling-ping-hua-zhong-qi-n-g-i-n-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
