<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[刘新修]]></title> 
<link>http://pic1.liuxinxiu.com:80/index.php</link> 
<description><![CDATA[刘新修的个人博客 (Liuxinxiu'S Blog)]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[刘新修]]></copyright>
<item>
<link>http://pic1.liuxinxiu.com:80/php-fpm-502/</link>
<title><![CDATA[nginx加php-fpm出现502 bad gateway错误的5种解决方法]]></title> 
<author>刘新修 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[web服务器]]></category>
<pubDate>Wed, 23 Nov 2016 04:59:52 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/php-fpm-502/</guid> 
<description>
<![CDATA[ 
	<p><span style="font-family: tahoma, arial, 宋体;">nginx出现502有很多原因，但大部分原因可以归结为资源数量不够用,也就是说后端php-fpm处理有问题，nginx将正确的客户端请求发给了后端的php-fpm进程，但是因为php-fpm进程的问题导致不能正确解析php代码，最终返回给了客户端502错误。</span></p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">服务器出现502的原因是连接超时 我们向服务器发送请求 由于服务器当前链接太多，导致服务器方面无法给于正常的响应,产生此类报错</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">因此如果你服务器并发量非常大，那只能先增加机器，然后按以下方式优化会取得更好效果;但如果你并发不大却出现502，一般都可以归结为配置问题，脚本超时问题。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">一、php-fpm进程数不够用</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">使用 netstat -napo &#124;grep &quot;php-fpm&quot; &#124; wc -l 查看一下当前fastcgi进程个数，如果个数接近conf里配置的上限，就需要调高进程数。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">但也不能无休止调高，可以根据服务器内存情况，可以把php-fpm子进程数调到100或以上，在4G内存的服务器上200就可以。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;"><br />二、调高调高linux内核打开文件数量</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">可以使用这些命令(必须是root帐号)</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>echo </span><span class="string">'ulimit -HSn 65536'</span><span> &gt;&gt; /etc/profile&nbsp;&nbsp;</span></span></li><li><span>echo <span class="string">'ulimit -HSn 65536'</span><span> &gt;&gt; /etc/rc.local&nbsp;&nbsp;</span></span></li><li class="alt"><span>source /etc/profile&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">三、脚本执行时间超时</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">&nbsp;</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">如果脚本因为某种原因长时间等待不返回 ，导致新来的请求不能得到处理，可以适当调小如下配置。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">nginx.conf里面主要是如下</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>fastcgi_connect_timeout 300;&nbsp;&nbsp;</span></span></li><li><span>fastcgi_send_timeout 300;&nbsp;&nbsp;</span></li><li class="alt"><span>fastcgi_read_timeout 300;&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">php-fpm.conf里如要是如下</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>request_terminate_timeout = 10s&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">四、缓存设置比较小</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">&nbsp;</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">修改或增加配置到nginx.conf</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>proxy_buffer_size 64k;&nbsp;&nbsp;</span></span></li><li><span>proxy_buffers&nbsp;&nbsp;512k;&nbsp;&nbsp;</span></li><li class="alt"><span>proxy_busy_buffers_size 128k;&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">五、 recv() failed (104: Connection reset by peer) while reading response header from upstream</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">&nbsp;</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">可能的原因机房网络丢包或者机房有硬件防火墙禁止访问该域名</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">但最重要的是程序里要设置好超时，不要使用php-fpm的request_terminate_timeout，</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">最好设成request_terminate_timeout=0;</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">因为这个参数会直接杀掉php进程，然后重启php进程，这样前端nginx就会返回104: Connection reset by peer。这个过程是很慢，总体感觉就是网站很卡。</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>May 01 10:50:58.044162 [WARNING] [pool www] child 4074, script </span><span class="string">'/usr/local/nginx/html/quancha/sameip/detail.php'</span><span> execution timed </span><span class="keyword">out</span><span> (15.129933 sec), terminating&nbsp;&nbsp;</span></span></li><li><span>May 01 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal 15 SIGTERM after 90.227060 seconds from start&nbsp;&nbsp;</span></li><li class="alt"><span>May 01 10:50:58.046818 [NOTICE] [pool www] child 4082 started&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">说一千道一万最重要的就是程序里控制好超时，gethostbyname、curl、file_get_contents等函数的都要设置超时时间。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">&nbsp;</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">另一个就是多说，这个东西是增加了网站的交互性，但是使用的多了反应就慢了，如果你网站超时且使用了多说是，可以关闭它。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px;"><span style="color: rgb(0, 0, 255);"><strong><font face="tahoma, arial, 宋体">php-fpm.conf max_children 和 max_requests配置：</font></strong></span></p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px;"><span style="color: rgb(255, 0, 0);">pm = static</span></p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px;">假如使用静态 pm.max_children这个参数会起作用，其余不会。动态反之。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px;">2G内存pm.max_children大概开启50左右，按照实际情况来调优，这个是很必要的。</p><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px;">========================================================</p><p style="margin: 5px auto; overflow-x: auto; color: rgb(17, 17, 17); font-family: &quot;HanHei SC&quot;, &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, STHeitiSC-Light, Arial, sans-serif;">max_children是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。 假设你打算给10G内存给当前配置的PHP-FPM Pool，一般一个PHP请求占用内存10M-40M，我们按站点每个PHP请求占用内存25M，这样max_children = 10G/25M = 409。所以，这个值可以根据情况算出来</p><p style="margin: 5px auto; overflow-x: auto; color: rgb(17, 17, 17); font-family: &quot;HanHei SC&quot;, &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, STHeitiSC-Light, Arial, sans-serif;">max_requests是每个子进程重生之前处理的请求数， 默认值为unlimited（默认为1024），可以设置小一点（如500左右），这样可以避免内存泄露带来的问题</p><p style="margin: 5px auto; overflow-x: auto; color: rgb(17, 17, 17); font-family: &quot;HanHei SC&quot;, &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, STHeitiSC-Light, Arial, sans-serif;">Nginx代理过程，将业务服务器请求数据缓存到本地文件，再将文件数据转发给请求客户端。高并发的客户端请求，必然要求服务器文件句柄的并发打开限制。使用ulimit命令（ulimit -n），查看Linux系统文件句柄并发限制，默认是1024，我们可以改为65535（2 的 16 次方，这是系统端口的极限）。修改的方法为：修改系统文件/etc/security/limits.conf，添加如下信息，并重新启动系统生效。</p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>*&nbsp;soft&nbsp;&nbsp;&nbsp;nofile&nbsp;&nbsp;65535&nbsp;&nbsp;</span></span></li><li><span>&nbsp;&nbsp;</span></li><li class="alt"><span>*&nbsp;hard&nbsp;&nbsp;&nbsp;nofile&nbsp;&nbsp;65535&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;"><span style="color: rgb(17, 17, 17); font-family: &quot;HanHei SC&quot;, &quot;PingFang SC&quot;, &quot;Helvetica Neue&quot;, Helvetica, STHeitiSC-Light, Arial, sans-serif;">然后在Nginx配置文件中，把文件限制及连接数信息改为65535：</span></p><div class="codeText"><div class="codeHead">C#代码</div><ol start="1" class="dp-c"><li class="alt"><span><span>worker_rlimit_nofile&nbsp;65535;&nbsp;&nbsp;</span></span></li><li><span>events&nbsp;&#123;&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;epoll;&nbsp;&nbsp;</span></li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;worker_connections&nbsp;&nbsp;65535;&nbsp;&nbsp;</span></li><li class="alt"><span>&#125;&nbsp;&nbsp;</span></li></ol></div><p style="margin-bottom: 0px; padding-top: 5px; padding-bottom: 5px; font-family: tahoma, arial, 宋体;">&nbsp;</p>
]]>
</description>
</item><item>
<link>http://pic1.liuxinxiu.com:80/php-fpm-502/#blogcomment</link>
<title><![CDATA[[评论] nginx加php-fpm出现502 bad gateway错误的5种解决方法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://pic1.liuxinxiu.com:80/php-fpm-502/#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>