1,访问时出现 502 Bad Gateway 的解决办法
Nginx 502 Bad Gateway的含义是请求的php-cgi已经执行,但是由于某种原因(一般是读取资源的问题)没有执行完毕而导致php-cgi进程终止。一般并发数太高的网站都容易出现此错误。出现502 Bad Gateway的原因有很多(更多原因见这里),但是大部分人修改下面的参数即可解决。
打开 /usr/local/php/etc/php-fpm.conf 文件,修改如下几个参数:


<value name="max_children">5</value>
<value name="request_terminate_timeout">0s</value>
<value name="process_control_timeout">5s</value>

max_children表示php-cgi的处理进程。如果max_children设置的较小,比如5-10个,那么php-cgi就会“很累”,处理速度也很慢,等待的时间也较长。如果长时间没有得到处理的请求就会出现504 Gateway Time-out错误。设置max_children也需要根据服务器的性能进行设定,增大进程数,内存占用也会相应增大,正常情况下每个php-cgi所耗费的内存在20M左右,这里我设置的是80。

request_terminate_timeout指的是fast-cgi的执行脚本时间,它默认是0s。0s的含义是让php-cgi一直执行下去而没有时间限制。如果你在此设成0s,那么当出现502 Bad Gateway的时候,这个502的状态将一直持续下去不会改变。但是如果你设置成5s,那么当php-cgi假死5s以后会自动恢复。这个值可以根据你服务器的性能进行设定,这里我设置的是20s。

2,强制开启SSL(强制http转向https)
编辑 /usr/local/nginx/conf/nginx.conf 文件,修改如下代码:

 

server
 {
  listen       80;
  server_name  域名;
  rewrite ^/(.*) https://域名/$1 permanent;
 }
 
server
 {
  listen       443;
  server_name  域名;
  index index.html index.htm index.php;
  root  /home/wwwroot;
 
  ssl    on;
                ssl_certificate    /home/wwwroot/xxx.crt;
                ssl_certificate_key     /home/wwwroot/xxx.key;
                ssl_session_timeout 5m;
 
        ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
 
  location ~ .*\.(php|php5)?$
   {
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fcgi.conf;
    fastcgi_param  HTTPS on;
   }

3,同时使http和https都能正常访问
这个问题浪费了我巨大的精力,其实新建两个server就能解决,但是我在试验的过程中,发现有两个问题需要注意:
1,两个server里不能同时出现“log_format access”这一段,否则重启nginx会提示配置文件有误。
2,必须在443端口加上“fastcgi_param HTTPS on;”,否则在https进行提交表单操作时会转向http。
整个配置如下:

 

80;
  server_name  域名;
  index index.html index.htm index.php;
  root  /home/wwwroot;
 
  location ~ .*\.(php|php5)?$
   {
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fcgi.conf;
   }
 
                location /status {
                        stub_status on;
                        access_log   off;
                }
 
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                        {
                                expires      30d;
                        }
 
                location ~ .*\.(js|css)?$
                        {
                                expires      12h;
                        }
 
 }
 
server
 {
  listen       443;
 
  ssl    on;
  ssl_certificate    /home/wwwroot/xxx.crt;
  ssl_certificate_key     /home/wwwroot/xxx.key;
  ssl_session_timeout 5m;
 
  ssl_protocols SSLv2 SSLv3 TLSv1;
  ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  ssl_prefer_server_ciphers   on;
 
 
  server_name  域名;
  index index.html index.htm index.php;
  root  /home/wwwroot;
 
 
  location ~ .*\.(php|php5)?$
   {
    fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    include fcgi.conf;
    fastcgi_param  HTTPS on;
 
   }
 
                location /status {
                        stub_status on;
                        access_log   off;
                }
 
                location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
                        {
                                expires      30d;
                        }
 
                location ~ .*\.(js|css)?$
                        {
                                expires      12h;
                        }
 
                log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
                access_log  /home/wwwlogs/access.log  access;
        }