502是nginx返回的错误

  • 502是nginx返回的错误
  • 配置nginx.conf
  • nginx.conf 配置代码


502 Bad Gateway是nginx服务器上面经常遇到的问题,但是在网上查找资料的时候,95%的资料都是在讨论nginx+php-fpm的环境下如何如何处理。而我的环境是nginx + apache,为了把这个问题搞清楚,我希望通过这篇文章,能深入的对这个问题的解决思路做一个梳理。

502是nginx返回的错误

所有的502都是nginx返回的,在错误页面也可以看到只有nginx作为标识,没有看到apache。这说明502是在nginx这个环节出现了问题。所以,我们需要来再梳理一下,在nginx+apache这个环境中数据包的处理逻辑:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mUlXa6Fe-1606705726113)(https://static.tangshuang.net/wp-content/uploads/2016/01/nginx-httpd-502.fw_.png)]

配置nginx.conf

504 Gateway Time-out就字面意思,我们可以理解为网页请求超时,也就是浏览网站网页所发出的请求没有反应或者未响应,在网站程序层面来说,就是请求未能够执行相应的PHP-CGI程序,或者PHP-CGI程序未能做出相应的处理,又或者是CGI程序的响应处理结果未能够反馈到浏览器或者未能及时反馈到浏览器。
  
  504 Gateway Time-out错误多是存在于Nginx网站服务器环境下,多与nginx.conf与php-fpm.conf设置是否正确合理有关。504GatewayTime-out错误的解决方法就是根据网站服务器性能及网站流量等诸多因素整合考虑,正确合理的设置niginx.conf和php-fpm.conf配置。
  
  进行正确合理nginx.conf配置,我们需要先了解和清楚我们网站服务器的配置性能,包括CPU、内存等,并对网站服务器进行必要的性能测试(可参考:vps主机性能测试方法详解),从而准确的掌握网站服务器自身性能状况;
  
  其次,是通过网站统计代码的添加,分析段时间内网站流量状况,比如:百度统计、CNZZ站长统计等,掌握网站日流量及PHP-CGI请求的大概情况;
  
  最后,根据我们掌握的服务器性能状况及网络流量情况,合理的对nginx.conf中的下列字句进行合理正确的设置。

fastcgi_connect_timeout 300s;
  
fastcgi_send_timeout 300s;
  
fastcgi_read_timeout 300s;
  
fastcgi_buffer_size 128k;
  
fastcgi_buffers 8128k;
  
fastcgi_busy_buffers_size 256k;
  
fastcgi_temp_file_write_size 256k;
  
fastcgi_intercept_errorson;

这些字句的设置规定了PHP-CGI的连接、发送和读取时间限制,需根据服务器性能及网络流量综合考虑设置,时间设置过长会给网站服务器造成压力,导致网站响应缓慢,甚至服务器宕机;时间设置过短,则就会出现504 Gateway Time-out或者其他CGI无响应错误。
  
  还有就是php-fpm.conf中max_children与request_terminate_timeout两个重要参数的设置。这两个参数的设置需要我们根据PHP程序情况及服务器带宽状况综合考虑并计算出合理准确的值,才能够避免504 Gateway Time-out或者其他CGI无响应错误的出现。通常情况下,般网站,可将request_terminate_timeou设置在900s左右,而max_children值根据服务器内存大小和CGI请求数目设置为合理的数值,般设置为800M左右。

nginx.conf 配置代码

根据自己服务器性能进行具体参数的配置

#user  nobody;
worker_processes  4;
 
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  logs/access.log  main;
 
    sendfile        on;
    #tcp_nopush     on;
 
    #keepalive_timeout  0;
    keepalive_timeout  65;
 
    fastcgi_connect_timeout 300; 
    fastcgi_send_timeout 300; 
    fastcgi_read_timeout 300; 
    fastcgi_buffer_size 64k; 
    fastcgi_buffers 4 64k; 
    fastcgi_busy_buffers_size 128k; 
    fastcgi_temp_file_write_size 128k; 
    gzip  on;
 
    client_header_buffer_size   32k;
    #配置两台tomcat的负载均衡
    upstream mysite{  
      ip_hash;
      server 172.29.xx.x9:8280;  
      server 172.29.xx.x0:8280;  
    }  
 
	
        
    server {
        listen       80;
        server_name  172.29.xx.xx;
 
        charset utf-8;
        #charset koi8-r;
 
        access_log  logs/host.access.log  main;
        
        location /zyk {
            proxy_pass http://mysite; #配置两台tomcat的负载均衡
	  		proxy_set_header Host $host;
	        proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	        proxy_connect_timeout   18000;   
            proxy_send_timeout      18000;  
            proxy_read_timeout      18000;  
        }
        #error_page  404              /404.html;
 
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
 
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
 

 
 
   
 
}