LoginSignup
26
28

More than 5 years have passed since last update.

Nginxリバースプロキシのタイムアウト対策

Last updated at Posted at 2016-01-28

ネットワークアプライアンス的なもんのGUIにnginxでたてたリバースプロキシを経由してアクセスしてたところ、更新後に接続がぶった切られる現象で作業がはかどらないとの報告があり、以下の感じにtimeout関連のパラメータをざっくり変えて様子をみることに。

$ sudo vi nginx.conf
$ sudo service nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ diff nginx.conf{,.`date +%Y%m%d`}
25,31c25
<     keepalive_timeout  130;
<     send_timeout 130;
<     client_body_timeout 130;
<     client_header_timeout 130;
<     proxy_send_timeout 130;
<     proxy_read_timeout 130;
< 
---
>     keepalive_timeout  65;
$ sudo service nginx restart

参考書籍によるとデフォルト値はだいたい60秒でした。
ほかにもfastcgi_{read,send,connect}_timeoutとかあったけどローカルにコンテンツはなくfastcgi使ってないし、
proxy_connect_timeoutはコネクション確立で切られることはないから不要そう、
短にtimeoutは終了待ちっぽいので終わってからそんなに待ってもしょうがないのでとりあえずいいやと。
ssl_session_timeoutとかはセッションキャッシュ設定いれてないと意味なさそげだしセッション確立のCPU処理コストがちょっと減るかもくらいの問題で待ちとは関係ないのかなと思ってスルーしました。

ちなみにログ的にはこんなかんじでした。これ自体はinfoだしクライアント側から接続切ってるだけとかかもしれず。

2016/01/25 03:25:06 [info] 7049#0: *26464 client 10.127.xxx.xxx closed keepalive connection (104: Connection reset by peer)
2016/01/25 06:08:30 [info] 7049#0: *26475 client closed connection while waiting for request, client: 10.127.xxx.xxx, server: 0.0.0.0:443

パラメータ変更後、更新メンテを経てerror.logをみたところなんだかwarnがでていたので追記です。

2016/02/02 00:21:06 [warn] 13855#0: *3429 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000002, client: 10.127.xxx.xxx, server: fuga01.hoge.net, request: "POST /cgi-mod/index.cgi HTTP/1.1", host: "fuga01.hoge.net", referrer: "https://fuga01.hoge.net/cgi-mod/index.cgi?xxxxx=xxxx&xxxx=xxxxx..." 

メモリからあふれてtempファイルに書いてるからI/Oがかかってるっぽいです。
nginxのリクエストボディのバッファリングに関する問題とその改善策 - Qiita
nginx で a client request body is buffered to a temporary file の警告に対応してみる | レンタルサーバー・自宅サーバー設定・構築のヒント
nginxをリバースプロキシにした時にPOSTがこける - tumblr
この↑へんをみると、client_body_buffer_sizeを増やすといいのかなーと思いました。
まあPOSTにちょっと時間かかっただけで失敗した話は聞いてないんで緊急度は低めのようですね。
(大量のユーザがアクセスするコンテンツとかではなくメンテする人がさわるだけの環境なため)

参考:
マスタリングNginx」のディレクティブ一覧を念入りに眺めてみました。超いい本です。
Nginx をリバースプロキシ(キャッシュ) として使ってみた | レンタルサーバー・自宅サーバー設定・構築のヒント
Module ngx_http_proxy_module
【nginx】レスポンスタイムアウトの設定メモ|ばちブロ
フロント/バックのreverse proxy構成で、指定秒数以内に必ずレスポンスを返す方法 - (ひ)メモ
Nginx設定のまとめ - Qiita

26
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
28