[Nginx] upstream timed out (110: Connection timed out) while reading response header from upstream

Tadashi Shigeoka ·  Sat, March 28, 2015

当サイト codenote.net が 504 Gateway Time-out で接続しにくい状態になっていました。

まず、Nginx のエラーログを確認すると upstream timed out (110: Connection timed out) while reading response header from upstream というエラーが出てました。

Nginx
$ tail -F /var/log/nginx/codenote.net/error.log 
2015/03/28 23:45:04 [error] 20407#0: *537270 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 223.135.69.50, server: codenote.net, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/www.sock", host: "codenote.net"

ひとまず PHP-FPM のプロセスを再起動して一旦は直ったものの、またすぐに繋がらなくなりました。

$ service php-fpm restart
Stopping php-fpm:                                          [  OK  ]
Starting php-fpm:                                          [  OK  ]

調査してみると PHP-FPM の設定に問題があるようで、確認してみたら process manager の設定がほぼデフォルトのままの状態でした。ちゃんと設定していたはずなんですけど…

/etc/php-fpm.d/www.conf

以下、pm (process manager) のデフォルト設定です。

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either 'static' or 'dynamic'
; Note: This value is mandatory.
pm.max_children = 9

; The number of child processes created on startup.
; Note: Used only when pm is set to 'dynamic'
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 3

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 2

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 4

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 200

下記の設定に変更しました。

pm = static
pm.max_children = 30
pm.max_requests = 500

pm = static は最初から小プロセスの起動数を固定する設定です。dynamic だと動的ですが、最初から確保できる static の方が利用するリソースが把握しやすいので static にします。

max_children は起動する小プロセスの最大数です。

max_requests は子プロセスが再起動するまでに実行するリクエスト数です。

PHP-FPM の process manager の設定を変更後、504 Gateway Time-out は表示されず快適にアクセスできるようになりました。

参考情報