nginx常见500错误解决办法

On 2011年05月16日, in linux, tips, by netoearth

Nginx 出現 500 Error, 錯誤訊息只能從 Log 查到, 有遇到下述兩種狀況:

  1. socket() failed (24: Too many open files) while connecting to upstream
  2. 512 worker_connections are not enough while connecting to upstream

在此紀錄解決方法.

 

Nginx “Too many open files” 修復

錯誤訊息

2011/05/01 23:00:49 [alert] 7387#0: *6259768 socket() failed (24: Too many open files) while connecting to upstream, client: 123.123.123.123, server: www.example.com, request: “GET [[/]] HTTP/1.1″, upstream: “fastcgi://127.0.0.1:1234″, host: “www.example.com”

解法1

這個解法參考看看就好, 最後沒有採用此解法.

  1. $ ulimit -n # 看目前系統設定的限制 (ulimit -a # 可查看全部參數)
    1024
  2. vim /etc/security/limits.conf # 由此檔案設定 nofile (nofile – max number of open files) 的大小
  3. ulimit -n 8024 # 直接將設定值改大一點.
解法2

最後查起來, 應該是 Nginx 的 connection 太多, 但是 php-cgi 不夠用.

php-cgi was started with phpfcgid_children=”10″ and phpfcgid_requests=”500″
ab was run on another server, connect via a switch using GBit ethernet

參考此篇設定: PHP performance III — Running nginx

  1. vim /etc/nginx/nginx.conf

    worker_connections  1024;
    worker_rlimit_nofile

  2. vim /etc/init.d/php-fcgi

    PHP_FCGI_CHILDREN=15
    PHP_FCGI_MAX_REQUESTS=1000
    改成
    PHP_FCGI_CHILDREN=15
    PHP_FCGI_MAX_REQUESTS=1024

  3. 上述文章的 phpfcgid_stop(), 寫得還不錯, 有需要可以用看看.

    phpfcgid_stop() {
    echo "Stopping $name."
    pids=`pgrep php-cgi`
    pkill php-cgi
    wait_for_pids $pids
    }

Nginx “512 worker_connections are not enough” 修復

錯誤訊息

2011/05/01 23:21:21 [alert] 19973#0: *6325881 512 worker_connections are not enough while connecting to upstream, client: 123.123.123.123, server: www.example.com, request: “GET / HTTP/1.1″, upstream: “fastcgi://127.0.0.1:1234″, host: “www.example.com”

解法
  1. /etc/nginx/nginx.conf

    worker_connections  10240;

  2. 參考 Nginx CoreModule

    worker_processes 2;
    worker_rlimit_nofile 10240;
    events {
    # worker_connections 10240;
    }

Tagged with:  

Comments are closed.