LoginSignup
72
53

More than 5 years have passed since last update.

504 Gateway Time-out が出た際は Elastic Load Balancing の設定にも注意しよう

Posted at

前書き

nginx と php の設定をすれば速攻対応できるはずだった 504 Gateway Time-out に悩まされたということと、
毎回設定の仕方をググるのも面倒なので nginx と php の設定についても備忘録を残します。

nginx と php の設定

必要な設定は以下の3ファイルになります。出力されたエラーログに応じて設定します。

※ 数値は任意の値です。今回は全て 180 ( 3 分 ) としています。

nginx.conf

エラーログ

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

設定

ディレイクティブ デフォルト コンテキスト 意味
fastcgi_read_timeout 60s http server location FastCGIプロセスへデータを送信するための待ち時間

コンテキストである http server location ディレイクティブのブロック内に fastcgi_read_timeout ディレイクティブを適用します。

/etc/nginx/nginx.conf
fastcgi_read_timeout 180

php.ini

エラーログ

Fatal error: Maximum execution time of 60 seconds exceeded

設定

項目 デフォルト 意味
max_execution_time 0 PHP スクリプトの最大実行時間
/etc/php5/fpm/php.ini
max_execution_time = 180

php-fpm.conf

エラーログ

recv() failed (104: Connection reset by peer) while reading response header from upstream

設定

項目 デフォルト 意味
request_terminate_timeout 0 リクエストを終了するまでの待ち時間
/etc/php5/fpm/php-fpm.conf
request_terminate_timeout 180

再読み込み

$ sudo service php-fpm reload
$ sudo service nginx reload

これで 504 Gateway Time-out を回避!出来ない…。

Elastic Load Balancing ( 以下、 ELB ) に最近繋いだことを思い出し、下記にたどり着きました。

ELB の設定

ロードバランサーに対するアイドル接続のタイムアウトの設定 - Elastic Load Balancing

デフォルトでは、Elastic Load Balancing は両方の接続についてアイドルタイムアウトを 60 秒に設定しています。

とのことで、

  1. https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)にある Amazon EC2 コンソールを開きます。
  2. ナビゲーションペインの [LOAD BALANCING] で [ Load Balancers] をクリックします。

  3. ロードバランサーを選択します。

  4. 下部のペインで、[Description] タブを選択します。

  5. [Connection Settings] を見つけ、[(Edit)] をクリックします。

  6. [Configure Connection Settings] ダイアログボックスで、[Idle Timeout] の値を入力します。アイドルタイムアウトの範囲は 1 ~ 3,600 秒です。

  7. [Save] をクリックします。

に書いてある通りにELBのアイドルタイムアウトを設定し、やっと 504 Gateway Time-out を回避できました!

参考文献

72
53
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
72
53