以下のようなリバースプロキシ構成でJSPとか使ってWebアプリケーションを開発しているのですが、アプリケーションについているファイルアップロード機能で「413 Request Entity Too Large というエラーが出てファイルのアップロードに失敗する」という問題が発生しました。
[client]----->[nginx]----->[Tomcat]
詳しく聞いてみると、いつもよりちょっと大きい1.5MB程度のファイルをアップロードしたところこのエラーが出たとのこと。
たしか、Tomcatにこういう制限があったなと思いだして、マニュアルを見てみると。
maxPostSize
The maximum size in bytes of the POST which will be handled by the container FORM URL parameter parsing. The limit can be disabled by setting this attribute to a value less than zero. If not specified, this attribute is set to 2097152 (2 megabytes). Note that the FailedRequestFilter can be used to reject requests that exceed this limit.
デフォルトで2MB... 2メガバイトまではいけるのにどうして??
と暫く考えましたが、前段のnginxにも同様の設定があり、
Syntax: client_max_body_size size;
Default: client_max_body_size 1m;Sets the maximum allowed size of the client request body, specified in the “Content-Length” request header field. If the size in a request exceeds the configured value, the 413 (Request Entity Too Large) error is returned to the client. Please be aware that browsers cannot correctly display this error. Setting size to 0 disables checking of client request body size.
こっちはもっと小さく、デフォルト1MB ... 1メガバイト でした。
ここでエラーになっていたわけですね。
そこで、nginxの設定ファイルに client_max_body_size
を足して再起動し、無事アップロードできるようになりました!
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
client_max_body_size 10M;
....
通り道にも気を付けましょうというお話でした。