lnmp 強制跳轉(zhuǎn)https
1,訪問時出現(xiàn) 502 Bad Gateway 的解決辦法Nginx 502 Bad Gateway的含義是請求的php-cgi 已經(jīng)執(zhí)行,但是由于某種原因(一般是讀取資源的問題)沒有執(zhí)行完畢而導(dǎo)致p
1,訪問時出現(xiàn) 502 Bad Gateway 的解決辦法
Nginx 502 Bad Gateway的含義是請求的php-cgi 已經(jīng)執(zhí)行,但是由于某種原因(一般是讀取資源的問題)沒有執(zhí)行完畢而導(dǎo)致php-cgi 進程終止。一般并發(fā)數(shù)太高的網(wǎng)站都容易出現(xiàn)此錯誤。出現(xiàn)502 Bad Gateway的原因有很多(更多原因見這里),但是大部分人修改下面的參數(shù)即可解決。
打開 /usr/local/php/etc/php-fpm.conf 文件,修改如下幾個參數(shù):
max_children表示php-cgi 的處理進程。如果max_children設(shè)置的較小,比如5-10個,那么php-cgi 就會“很累”,處理速度也很慢,等待的時間也較長。如果長時間沒有得到處理的請求就會出現(xiàn)504 Gateway Time-out錯誤。設(shè)置max_children也需要根據(jù)服務(wù)器的性能進行設(shè)定,增大進程數(shù),內(nèi)存占用也會相應(yīng)增大,正常情況下每個php-cgi 所耗費的內(nèi)存在20M 左右,這里我設(shè)置的是80。
request_terminate_timeout指的是fast-cgi 的執(zhí)行腳本時間,它默認是0s 。0s 的含義是讓php-cgi 一直執(zhí)行下去而沒有時間限制。如果你在此設(shè)成0s ,那么當出現(xiàn)502 Bad Gateway的時候,這個502的狀態(tài)將一直持續(xù)下去不會改變。但是如果你設(shè)置成5s ,那么當php-cgi 假死5s 以后會自動恢復(fù)。這個值可以根據(jù)你服務(wù)器的性能進行設(shè)定,這里我設(shè)置的是20s 。
2,強制開啟SSL (強制http 轉(zhuǎn)向https )
編輯 /usr/local/nginx/conf/nginx.conf 文件,修改如下代碼:
server
{ listen 80; server_name 域名; rewrite ^/(.*) https://域名/$1 permanent;
,}
server
ssl on; { listen 443; server_name 域名; index index.html index.htm index.php; root /home/wwwroot;
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 就能解決,但是我在試驗的過程中,發(fā)現(xiàn)有兩個問題需要注意:
1,兩個server 里不能同時出現(xiàn)“l(fā)og_format access”這一段,否則重啟nginx 會提示配置文件有誤。
2,必須在443端口加上“fastcgi_param HTTPS on;”,否則在https 進行提交表單操作時會轉(zhuǎn)向http 。
整個配置如下:
server
location ~ .*.(php|php5)?$ { } fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; { listen 80; server_name 域名; index index.html index.htm index.php; root /home/wwwroot;
,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;
location /status {
stub_status on; } location ~ .*.(php|php5)?$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fcgi.conf; fastcgi_param HTTPS on; server_name 域名; index index.html index.htm index.php; root /home/wwwroot; ssl_prefer_server_ciphers 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;
}