LoginSignup
0
1

More than 3 years have passed since last update.

備忘録:AWSのElastic Beanstalk でnginxの設定拡張 client_max_body_size

Last updated at Posted at 2020-11-04

環境

  • Spring Boot + React
  • Elastic BeanStalk
    • Corretto 11
    • Amazon Linux 2

バグ

練習のため、Spring Boot + ReactのCRUDアプリをAWSのElastic BeanStalkにデプロイしたところ、ファイル容量の大きい画像投稿ができない。

spring.servlet.multipart.max-file-size=10MBの設定をSpring Boot側ではしており、ローカルでは投稿できるサイズの画像を投稿できず、413 Request Entity Too Largeエラーが出ている。

nginxのエラーログ(var/log/nginx/error.log)

2020/11/03 10:42:27 [error] 31794#0: *108 client intended to send too large body: 3275907 bytes,...

エラー内容をググると、nginxはデフォルトで、リクエストボディのサイズに1MBまでという上限が設定されていることが分かった。この上限を変えるには、client_max_body_sizeを変更しなければいけないらしい。

ソース
https://stackoverflow.com/questions/44741514/nginx-error-client-intended-to-send-too-large-body

Elastic Beanstalkでのnginx設定拡張方法

どうやらソースバンドルのルートに、以下のディレクトリ構造で.confファイルを作成し、client_max_body_size 10M;を記述すれば、ディプロイ時に設定を追加してくれるらしい。

├── .platform
│   └── nginx
│       └── conf.d
│           └── myconf.conf
└── myApp-0.0.1-SNAPSHOT.jar

ソース

  1. https://stackoverflow.com/questions/18908426/increasing-client-max-body-size-in-nginx-conf-on-aws-elastic-beanstalk
  2. https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

注:上の記事では、 .ebextensions/nginx/conf.d/以下となっているが、Amazon Linux2の場合、上記の.platform/nginx/conf.d/に追加するのが正しいようです(https://stackoverflow.com/questions/63430968/application-source-bundle-doesnt-work-when-uploaded-to-aws-elastic-beanstalk )。

試してみた

フォルダーを作成し、その中にjarとconfファイルを含んだ.platformフォルダをzip化→アップロードするも、deployエラーが出る。。。下記、eb-engine.logのエラー。confファイルの書き方を変えてみたり色々したけど、デプロイできない。。

[ERROR] An error occurred during execution of command [app-deploy] - [CheckProcfileForJavaApplication]. 
Stop running the command. Error: there is no Procfile and no .jar file at root level of your source bundle 

原因

結果、下記の記事によって、アーカイブのフォルダ構成が正しくないことが分かった。
対象ファイル/フォルダ自体を圧縮せずに、新たなフォルダに入れて圧縮していたのが原因だった...

解凍されたファイルが、新しい上位フォルダまたはディレクトリではなく、アーカイブ自体があるフォルダに表示されていることを確認します。

さらに試してみた

今度は無事、デプロイ成功!早速、画像投稿するも...

[error] 31794#0: *108 client intended to send too large body: 3275907 bytes,...

設定が変更されていない!!

原因

コマンドラインでzipコマンドを使うときに、-rオプションをつけていなかったため、./platform以下の中身が空っぽになっていた。。。

zip -r 圧縮後のファイル名.zip 圧縮したいファイル/フォルダ

上記が解決してやっと、設定が反映されました。

まとめ

  1. 同様環境下で、client intended to send too large body (413 Request Entity Too Large)エラーが出たら、nginxのclient_max_body_sizeを変更する。
  2. Elastic Beanstalkだと、簡単な方法として、ソースバンドルに設定ファイルを含むことで変更可能。
  3. ソースバンドルは、親フォルダまたは最上位ディレクトリを含まない(サブディレクトリを除く)。
  4. zip コマンドを使う場合、フォルダを含むなら、-rを忘れない。
0
1
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
0
1