<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[[实践OK]一边制造，一边讲解http状态码502504499500]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 30 Nov 2018 16:02:17 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	实践来自：https://mp.weixin.qq.com/s/TFRv2wfaNpje_5rrELBEgA<br/><br/>定义<br/>==============================================================================================<br/>502，Bad Gateway，网关错误，它往往表示网关从上游服务器中接收到的响应是无效的，主要是PHP执行时间大于PHP超时，于是给Nginx返回为空引起的。<br/>502并不是指网关本身出了问题，而是<br/>1）从上游接收响应出了问题，比如由于上游服务自身超时导致不能产生响应数据，<br/>2）或者上游不按照协议约定来返回数据导致网关不能正常解析。<br/><br/><br/>情况1：关掉PHP-FPM进程：<br/>&quot;123.115.119.90&quot; &quot;up.levoo.com&quot; &quot;-&quot; &quot;[30/Nov/2018:22:51:52 +0800]&quot; &quot;GET /hello.php HTTP/1.1&quot; &quot;502&quot; &quot;552&quot; &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36&quot; &quot;0.000&quot; &quot;0.000&quot;<br/>2018/11/30 22:51:52 [crit] 29084#0: *124 connect() to unix:/dev/shm/php-fcgi.sock failed (2: No such file or directory) while connecting to upstream, client: 123.115.119.90, server: up.levoo.com, request: &quot;GET /hello.php HTTP/1.1&quot;, upstream: &quot;fastcgi://unix:/dev/shm/php-fcgi.sock:&quot;, host: &quot;up.levoo.com&quot;<br/><br/>情况2：<br/><textarea name="code" class="php" rows="15" cols="100">
&lt;?php

sleep(7); 

echo &#039;hello world&#039;;

?&gt;
</textarea><br/>php-fpm.conf配置：<br/><br/>request_terminate_timeout=5<br/>nginx配置：<br/>fastcgi_read_timeout 10;<br/><br/>php-fpm.conf设置的最大执行时间是5s，但是php脚本需要的执行时间大于7s，所以php-fpm进程执行5s时就回退出，此时php脚本没有正常执行完，返回给网关Nginx的数据为空，于是导致502。<br/>502日志：<br/>&quot;123.115.119.90&quot; &quot;up.levoo.com&quot; &quot;-&quot; &quot;[30/Nov/2018:22:53:14 +0800]&quot; &quot;GET /hello.php HTTP/1.1&quot; &quot;502&quot; &quot;552&quot; &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36&quot; &quot;5.287&quot; &quot;5.288&quot;<br/>2018/11/30 22:53:14 [error] 29084#0: *188 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 123.115.119.90, server: up.levoo.com, request: &quot;GET /hello.php HTTP/1.1&quot;, upstream: &quot;fastcgi://unix:/dev/shm/php-fcgi.sock:&quot;, host: &quot;up.levoo.com&quot;<br/><br/>在实际的实践中发现当在EasySwoole里传大于设置的图片也会出现 Nginx的代理机上出现104: Connection reset by peer的情况。<br/><br/>==============================================================================================<br/><br/>504:nginx则以为上游php-fpm没有按照设置时间超过Nginx的等待时间，返回响应数据就会返回504。<br/>504，Gateway Timeout，网关超时。<br/><br/>它表示网关没有从上游及时获取响应数据。注意它和502在超时场景下的区别，502是指上游php-fpm因为超过自身允许的执行时间而不能正常生成响应数据，而504是指在php-fpm还未执行完成的某一时刻，由于超过了nginx自身的超时时间，nginx则以为上游php-fpm没有按照设置时间返回响应数据就会返回504， 此时对于php-fpm而言还会继续执行下去，直到执行完成。<br/><br/>&lt;?php<br/><br/>sleep(7); <br/><br/>echo &#039;hello world&#039;;<br/><br/>error_log(&quot;hello&quot;, 3, &quot;/tmp/hello.log&quot;);<br/><br/>?&gt;<br/><br/>vi php-fpm.d/www.conf<br/>request_terminate_timeout = 30s<br/><br/>vi nginx.conf<br/>fastcgi_read_timeout&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5s;<br/><br/><br/>504 Gateway Time-out<br/>nginx<br/><br/>&quot;123.115.119.90&quot; &quot;up.levoo.com&quot; &quot;-&quot; &quot;[30/Nov/2018:23:52:04 +0800]&quot; &quot;GET /hello.php HTTP/1.1&quot; &quot;504&quot; &quot;562&quot; &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36&quot; &quot;5.005&quot; &quot;5.005&quot;<br/>2018/11/30 23:52:0<br/><br/><br/>==============================================================================================<br/>499, Client Closed Request， 客户端主动断开连接。<br/>是指一次http请求在客户端指定的时间内没有返回响应，此时，客户端会主动断开连接，此时表象为客户端无响应返回，而nginx的日志中会status code 为499。<br/><br/>此状态码在浏览器请求时几乎不可见，因为浏览器默认的超时时间会很长。多见于服务之间的调用，在业务架构中常常会分层设计，拆分为不同的子系统或者微服务，这样系统之间就会常常通过http方式来请求，并且会设置每次请求的超时时间，当请求在请求时间内所调用的上游服务无返回，则会主动关闭连接，上游服务日志中会记录一条499。<br/><br/>php代码<br/><br/>&lt;?php<br/><br/>sleep(7); <br/><br/>echo &#039;hello world&#039;;<br/><br/>error_log(&quot;hello&quot;, 3, &quot;/tmp/hello.log&quot;);<br/><br/>?&gt;<br/>php-fpm.conf配置：<br/><br/>request_terminate_timeout=30<br/>nginx配置：<br/><br/>fastcgi_read_timeout 5;<br/>我们在linux终端使用curl命令来请求，-m 表示超时时间，单位为秒<br/><br/>curl -i -m 3 http://127.0.0.1/hello.php <br/>返回为：<br/><br/>curl: (28) Operation timed out after 3004 milliseconds with 0 bytes received<br/>nginx的access日志的code为499，如下：<br/><br/>&quot;HEAD /hello.php HTTP/1.1&quot; 499 0 <br/><br/><br/>#curl -I -m 3 http://up.levoo.com/hello.php <br/>curl: (28) Operation timed out after 3022 milliseconds with 0 bytes received<br/>Nginx日志：<br/>&quot;47.94.88.237&quot; &quot;up.levoo.com&quot; &quot;-&quot; &quot;[30/Nov/2018:23:55:38 +0800]&quot; &quot;GET /hello.php HTTP/1.1&quot; &quot;499&quot; &quot;0&quot; &quot;-&quot; &quot;curl/7.55.1&quot; &quot;2.888&quot; &quot;-&quot;<br/><br/>==============================================================================================<br/>500<br/>500, Internal Server Error , 服务器内部错误，服务器遇到了一个未曾预料的状况，导致了它无法完成对请求的处理。<br/><br/>日常开发中500错误几乎都是由于php脚本语法出现错误导致php-fpm无法正常执行。<br/><br/>复现路径<br/>php代码:<br/><br/>&lt;?php<br/>echo &#039;hello &#039;<br/>echo &#039; world&#039;;<br/>?&gt;<br/>由于php代码语法错误，php-fpm执行失败，然后告诉nginx这一结果，nginx则返回500。<br/>该网页无法正常运作 up.levoo.com 目前无法处理此请求。<br/>HTTP ERROR 500<br/><br/>Nginx错误日志：<br/>&quot;123.115.119.90&quot; &quot;up.levoo.com&quot; &quot;-&quot; &quot;[30/Nov/2018:23:57:42 +0800]&quot; &quot;GET /hello.php HTTP/1.1&quot; &quot;500&quot; &quot;5&quot; &quot;-&quot; &quot;Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36&quot; &quot;0.022&quot; &quot;0.022&quot;<br/><br/>php错误日志：<br/>hello.php on line 3
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]一边制造，一边讲解http状态码502504499500]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>