DEV Community

Cover image for How To Fix Error 413 in Apps Behind Nginx on AWS Elastic Beanstalk
Antonello Zanini for Writech

Posted on • Originally published at writech.run

How To Fix Error 413 in Apps Behind Nginx on AWS Elastic Beanstalk

I recently dealt with error 413 on an application running on AWS Elastic Beanstalk and using Nginx as a reverse proxy. This error is generated by Nginx when a client makes a request that exceeds the web server size limit. I found several possible solutions to this problem online, but none of them solved it.

After several attempts, I finally managed to find a solution that is also backed by the official AWS EB documentation.

Let's now find out what the "413 Request Entity Too Large" error is and how to solve it in Nginx and when using Nginx as a reverse proxy on AWS Elastic Beanstalk.

What Is the HTTP Error 413?

"The HTTP 413 Payload Too Large response status code indicates that the request entity is larger than limits defined by server; the server might close the connection or return a Retry-After header field." — MDN "413 Payload Too Large" page

Note that the HTTP 413 Payload Too Large status code was previously referenced to as 413 Request Entity Too Large. In detail, remember that in most cases, it is still called that way. So, if you encounter a "Request Entity Too Large" error, you can assume it is an HTTP 413 Payload Too Large error.

Check out this article to learn how to fix HTTP error 413 on Node.js.

Fixing Error "413 Request Entity Too Large" in Nginx

As explained earlier, Nginx only accepts requests with a payload of less than MB. This restrictive limitation is the cause of the "413 Request Entity Too Large" error. To avoid this, you can simply set the client_max_body_size option to a larger value.

To accomplish this, you have to edit the /etc/nginx/nginx.conf file.

Set client_max_body_size in the http block to affect all virtual hosts:

http {
    # ...
    client_max_body_size 10M;
}
Enter fullscreen mode Exit fullscreen mode

Set client_max_body_sizein the server block to affect a particular website or web application. Here's the code:

server {
    # ...
    client_max_body_size 10M;
}
Enter fullscreen mode Exit fullscreen mode

Set client_max_body_sizein the location block to affect a particular directory of a specific website or web application. Here's the code:

location /uploads {
    # ...
    client_max_body_size 10M;
}
Enter fullscreen mode Exit fullscreen mode

Now, save the /etc/nginx/nginx.conf file and restart the Nginx web server to apply the recent changes with the following commands:

systemctl restart nginx service nginx restart
Enter fullscreen mode Exit fullscreen mode

Congrats! You just extended the Nginx request entity size limit from 1 MB to 10 MB.

Configuring Nginx on AWS Elastic Beanstalk to Avoid Error 413

Unfortunately, to apply the solution above to an application on AWS Elastic Beanstalk that uses Nginx as a reverse proxy, you should manually configure Nginx on each instance.

Each AWS EB instance is created from scratch and does not share web server configurations with other instances. Therefore, every time you add a new instance, you may need to take action via SSH to configure Nginx, as shown earlier. As you can imagine, this is not a viable solution.

Luckily, as explained in the "Extending Elastic Beanstalk Linux platforms" documentation page, AWS Elastic Beanstalk allows you to extend the default Nginx configuration when used as a reverse proxy. This custom configuration is shared by all AWS EB instances.

Follow these steps to extend the Nginx default configuration on AWS Elastic Beanstalk to avoid the "413 Request Entity Too Large" errors.

  1. Make sure your application source bundle has a .platform folder. If this is not present, create it.

  2. Create a XXX.conf configuration file inside the .platform/nginx/conf.d/ path of your application source bundle. You can call this .conf file whatever you like. For example, you can call it proxy.conf.

  3. Initialize your .conf file as follows:

client_max_body_size 10M;
Enter fullscreen mode Exit fullscreen mode

This is what your .platform folder in your application source bundle may now look like:

.platform/nginx/conf.d/proxy.conf

Et voilà! You just defined an extended configuration to avoid 413 errors for Nginx when used by AWS Elastic Beanstalk as a reverse proxy.

Conclusion

In this article, you saw what an HTTP error 413 is and why it occurs. Specifically, you had the opportunity to learn how to fix the "413 Request Entity Too Large" error on the Nginx web server. Also, you learned how to apply that solution to an AWS Elastic Beanstalk environment using Nginx as a reverse proxy.

Thanks for reading! I hope you found this article helpful.


The post "How To Fix Error 413 in Apps Behind Nginx on AWS Elastic Beanstalk" appeared first on Writech.

Top comments (0)