How To Fix Error 413 in Apps Behind Nginx on AWS Elastic Beanstalk
Tags: aws, programming, softwaredevelopment, softwareengineering
Published: 2024-01-19 21:30:36

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`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header field." — [MDN "413 Payload Too Large" page](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413)

Note that the HTTP `413 Payload Too Large` status code was [previously referenced to as](https://www.rfc-editor.org/rfc/rfc2616#section-10.4.14) [`413 Request Entity Too Large`](https://www.rfc-editor.org/rfc/rfc2616#section-10.4.14). 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](https://writech.run/blog/how-to-fix-413-request-entity-too-large-error-in-node-js-226c0070e4f4).

## 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:

```javascript
http {
    # ...
    client_max_body_size 10M;
}
```

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

```javascript
server {
    # ...
    client_max_body_size 10M;
}
```

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

```javascript
location /uploads {
    # ...
    client_max_body_size 10M;
}
```

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

```shell
systemctl restart nginx service nginx restart
```

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"](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html) 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.

3. 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`.

5. Initialize your `.conf` file as follows:

```javascript
client_max_body_size 10M;
```

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

![.platform/nginx/conf.d/proxy.conf](https://writech.run/wp-content/uploads/2023/08/image-60.png)

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](https://writech.run/blog/how-to-fix-error-413-in-apps-behind-nginx-on-aws-elastic-beanstalk-c68a1ef2c331/)" appeared first on [Writech](https://writech.run)._