欢迎来到日本小春精选,了解日本先从@日本生活基础课程开始!
欢迎 欢迎     登录 | 注册      消息
当前位置: 首页小组技术交流nginx 502 bad gateway

[Linux]nginx 502 bad gateway

<div>我们在使用nginx的时候有不少问题出现,首先我们就来解决下nginx 502错误的问题</div><p><br /></p><div>经 过分析将nginx的error log打开,发现”pstream sent too big header while reading   response header from   upstream”这样的错误提示,查阅了一下资料,大意是nginx缓冲区有一个bug造成的,我们网站的页面消耗占用缓冲区可能过大</div><p>&nbsp;</p><p>&nbsp;</p><div><p>我们是nginx反向代理<br /> proxy是nginx作为client转发时使用的,如果header过大,超出了默认的1k,就会引发上述的upstream sent too big header (说白了就是nginx把外部请求给后端apache ,apache返回的header&nbsp;<wbr> 太大nginx处理不过来就导致了。</wbr></p> <p>&nbsp;<wbr></wbr></p> <p>&nbsp;<wbr> server {<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> listen&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 80;<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> server_name&nbsp;<wbr> *.xywy.com ;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> large_client_header_buffers 4 16k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> #charset koi8-r;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> # access_log off;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> location / {</wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>#添加这3行 ,<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="color:red;">&nbsp;</span><wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="color:red;"> proxy_buffer_size 64k;</span><br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="color:red;"> proxy_buffers&nbsp;</span><wbr>&nbsp;<wbr><span style="color:red;"> 32 32k;</span><br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr><span style="color:red;"> proxy_busy_buffers_size 128k;</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_set_header Host $host;<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_set_header X-Real-IP&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> $remote_addr;<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_set_header X-Forwarded-For&nbsp;<wbr> $proxy_add_x_forwarded_for;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> set $baiduspider '';</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> if ( $http_user_agent ~ Baiduspider) {<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> set $baiduspider Baidu;<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> }</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>............</p> <p>&nbsp;<wbr></wbr></p> <p>&nbsp;<wbr>如果是 nginx+PHPcgi 就该&nbsp;<wbr></wbr></wbr></p> <p>fastcgi_connect_timeout 60;<br /> fastcgi_send_timeout 180;<br /> fastcgi_read_timeout 180;<br /> fastcgi_buffer_size 128k;<br /> fastcgi_buffers 4 256k;<br /> fastcgi_busy_buffers_size 256k;<br /> fastcgi_temp_file_write_size 256k;<br /> fastcgi_intercept_errors on<br /> 011/01/07 11:12:57 [error] 10770#0: *38585340 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 116.22.131.154, server: *.xywy.com, request: &quot;GET /ysmp/index.php?did=124994 HTTP/1.0&quot;, upstream: &quot;http://127.0.0.1:8080/ysmp/index.php?did=124994&quot;, host: &quot;xywy.yn16.com&quot;<br /> &nbsp;</p> <p>后来原来那错误没了出了新错误了 upstream timed out 超时?</p> <p>server {<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> listen&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> 80;<br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> server_name&nbsp;<wbr> *.xywy.com ;<br /> <span style="color:#ff0000;">&nbsp;<wbr> large_client_header_buffers 4 16k;</wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> client_max_body_size 300m;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> client_body_buffer_size 128k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_connect_timeout 600;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_read_timeout 600;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_send_timeout 600;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_buffer_size 64k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_buffers&nbsp;<wbr>&nbsp;<wbr> 4 32k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_busy_buffers_size 64k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br style="color: #ff0000;" /> <span style="color:#ff0000;">&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> proxy_temp_file_write_size 64k;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></span><br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> #charset koi8-r;<br /> <br /> &nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr>&nbsp;<wbr> # access_log off;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></p> <p>后来参数我又改了下 就好了。。。<br /> &nbsp;</p> <p>&nbsp;<wbr>可以参考:</wbr></p> <p><br /> &nbsp;</p> <p><a><u>http://wiki.nginx.org/NginxHttpProxyModule</u></a></p> <a><u>http://blog.sina.com.cn/s/blog_5dc960cd0100i4mt.html</u></a></div><br />
2012-08-09 17:11:59 来自:球球
用户评论(1)
正序阅读
  • 球球

    2012-08-09 18:51:44 球球 1#

    <p>http://www.vpser.net/build/nginx-lnmp-ipv6.html</p><p>http://lnmp.org/faq.html</p><p>http://iambin.blogbus.com/logs/62588556.html</p><p>http://wiki.nginx.org/HttpGeoipModule<br /></p>

你的回应
登录 | 注册