Press "Enter" to skip to content

关于lnmp一键安装包的那些事2

本文继续讲述我在使用CentOS+Licess的lnmp一键安装包时遇到的问题。

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

打开 /usr/local/php/etc/php-fpm.conf 文件,修改几个参数:
[code lang=”plain”]
5
0s
5s

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

request_terminate_timeout指的是fast-cgi的执行脚本时间,它默认是0s。0s的含义是让php-cgi一直执行下去而没有时间限制。如果你在此设成0s,那么当出现502 Bad Gateway的时候,这个502的状态将一直持续下去不会改变。如果在此设置成30s,那么30秒以后fast-cgi就被terminate,如果此时用户的请求没有执行完成,那么会立马变成一个502 Bad Gateway。因此该值不建议设置为0s,设置大了会导致fast-cgi一直无法释放,设置小了会性能较弱的服务器频繁出现502 Bad Gateway。建议设置成60s。注意,如果你需要进行一个耗时很长的php任务,比如导入大容量数据库等,建议先把此值设置为0s,执行完再改成60s就OK了。

打开 /usr/local/php/etc/php.ini 文件,修改如下参数:
[code lang=”plain”]
max_execution_time = 30

注意,这个也是指的php脚本的执行时间,这个时间比上面的request_terminate_timeout优先级弱,即系统会优先考虑request_terminate_timeout的值,然后再考虑此值。因此建议此值大于request_terminate_timeout值即可,也可以保持默认的300s。

这里有一个一键重启脚本,能够在发生Nginx 502 Bad Gateway的时候自动重启php。

2,强制开启SSL(强制http转向https)
编辑 /usr/local/nginx/conf/nginx.conf 文件,修改如下代码:
[code lang=”plain”]
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。
整个配置如下:
[code lang=”plain”]
server
{
listen 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;
}

11 Comments

  1. SErHo 2010-10-17

    折腾这个基本上在你这里就可以找到问题答案了。

  2. zhiwei 2010-10-26

    建议在博客里加上屏蔽3×0浏览器的代码。

  3. C.Z 2010-12-12

    我也购买了84的,也装好了,我现在在想要不要做些什么安全措施啊,还是我每天有备份就得了,不知道阁下是否有做些安全措施。

    • bear 2010-12-13

      我什么安全措施也没做 就是把ROOT密码改的复杂了点 呵呵

      • C.Z 2010-12-13

        我在想是不是要做点什么,就像Licess 说的阻止SSH暴力攻击。
        我学你也用wordpress默认主题,哈哈,效果就是不一样,爽。

Leave a Reply to C.Z Cancel reply

Your email address will not be published. Required fields are marked *