Search code examples
djangoamazon-web-servicesnginxdjango-rest-frameworkamazon-elastic-beanstalk

Adding nginx configuration to AWS Elastic Beanstalk


I got client request body is larger than the buffer size allowed by nginx error in the app so I decided to add an nginx.config file to fix the issue. Here is the file.

user                    nginx;
events {
    worker_connections        1024;
}
http {
    client_body_buffer_size     20M;
    client_max_body_size        20M;

upstream api {
    server Sauftragdev-env.eba-zirmk4s9.eu-central-1.elasticbeanstalk.com:8000;
}
server {
    listen 80;
    location /static/ {
        alias /static/;
    }

    location / {
        proxy_pass http://api;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

}

Now what do I need to add in the Elastic Beanstalk configuration to make this work? It's platform is Python 3.8 running on 64bit Amazon Linux 2/3.4.3.


Solution

  • Elastic Beanstalk uses nginx as the default reverse proxy to map your application to your Elastic Load Balancing load balancer. Elastic Beanstalk provides a default nginx configuration that you can extend or override completely with your own configuration - based on AWS official docs.

    First, you have to understand what does your application bundle directory look like?

    https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html#platforms-linux-extend.example

    From the official documentation, the nginx.conf is placed under the following path:

    .platform/nginx/
    

    There is a note that you have to follow:

    When you add or edit an nginx.conf configuration file, be sure to encode it as UTF-8.

    Second, simply add a .conf file onto the the following path from your application bundle.

    .platform/nginx/conf.d/
    

    If you want to override the default settings, include the configs under the following file:

    .platform/nginx/nginx.conf
    

    To extend not only the nginx but also buildfile, hooks, we can refer to this part: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html