LoginSignup
3
4

More than 3 years have passed since last update.

Elastic Beanstalk にファイルアップロードができない (nginxで413エラー)

Last updated at Posted at 2019-05-14

背景

EBにrailsアプリケーションをデプロイしたらファイルのアップロードがうまくいかず

調査

ローカル
$ eb ssh

でサーバに入り

サーバ内
$ view /var/log/nginx/access.log

でログを調査
413エラーが出ていました

nginxで"client intended to send too large body"が発生した時の対策方法

を見てみると
どうやらファイルサイズがnginxの設定値より大きくてエラーになるらしく、デフォルトでは1MBまでしか受け取れないらしい

設定を変更してみる

試行1

Elastic beanstalk で大きいデータを POST したら nginx が 413 Request Entity Too Large
reasing client_max_body_size in Nginx conf on AWS Elastic Beanstalk
を参考にebextensionsを書いてデプロイしてみる

.ebextensions/01-nginx-max-body.config
files:
  "/etc/nginx/conf.d/client-max-body-size.conf":
    mode: "000755"
    owner: root
    group: root
    content: "client_max_body_size 1024M;"

そしてデプロイをするも変わらずエラー

試行2

Nginx httpディレクティブで指定したclient_max_body_size が無視される
を見て
サーバ内部の/etc/nginx/nginx.confを見てみると

include /etc/nginx/conf.d/*.conf;

がhttpディレクティブに書かれており、serverディレクティブには

include /etc/nginx/default.d/*.conf;

とあったので.ebextensions/01-nginx-max-body.config
/etc/nginx/conf.d/client-max-body-size.conf
/etc/nginx/default.d/client-max-body-size.confに書き換えてデプロイするもエラー

試行3

設定が反映されていないのでは?と思ったがnginxの設定値を確認するコマンドがわからず
とりあえずサーバ内で

# nginx -s reload

をしてみると、動いた!!!
なるほど、設定が反映されていないんですね〜

ということで最終的に

.ebextensions/01-nginx-max-body.config
files:
  "/etc/nginx/conf.d/client-max-body-size.conf":
    mode: "000755"
    owner: root
    group: root
    content: "client_max_body_size 1024M;"

container_commands:
  01-nginx_reload:
    command: sudo nginx -s reload

でデプロイしたら成功、予想通り!

デプロイポリシーがAll at onceだったのでreloadが必要だったのかもしれない
immutableだと新しいインスタンスが立ち上がるからreload必要ないのかもしれない
ここらへんは要検証

3
4
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
3
4