djangoamazon-web-servicesnginxamazon-ec2amazon-elastic-beanstalk

How to modify the nginx.conf file during AWS Elastic Beanstalk deployment


I have developed a Django application and deployed it in an Elastic Beanstalk environment (with nginx as proxy server).

The Linux platform should be the first version because if I run the command: cat /etc/os-release in the terminal of the EC2 instance managed by EB I get these information:

os-release file of the EC2 instance

Also I double confirm that it's Linux 1 and NOT Linux 2 by going to the EC2 console:

Linux platform of the EC2 instance

Now, after having clarified which Linux platform I'm using in the EC2 instance, let's go ahead with the problem.

When I access the EC2 instance and I go the file etc/nginx/nginx.conf, what I see is this:

Content of the nginx.conf file

Now, what I want to do is to increase the timeout settings under the "server" section of the nginx.conf file. I also would like to increase the client_max_body_size somehow.

I read from some documentation online the I have to create a .ebextensions folder in the root of my Django project with inside some .config files and also a buildspec.yml file in the root folder of the project.

So this is where I have located my files:

my_project
  |- .ebextensions
  |  |- django.config
  |  |- instances.config
  |  |- nginx.config
  |- buildspec.yml
  |- manage.py
  |...

Here the content of each file:

django.config:

container_commands:
  01_makemigrations:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py makemigrations --noinput"
    leader_only: true
  02_migrate:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate --noinput"
    leader_only: true
  03_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
    leader_only: true

instances.config:

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: "my_project.wsgi:application" 
  aws:elbv2:listener:443:  
    ListenerEnabled: 'true'  
    Protocol: HTTPS  
    SSLCertificateArns: arn:aws:acm:<region>:<user_id>:certificate/<certificate_id>
  aws:elasticbeanstalk:environment:proxy:staticfiles:  
    /static: staticfiles 
  aws:elbv2:loadbalancer:
    IdleTimeout: 900

nginx.config:

 files:
   "/etc/nginx/conf.d/nginx.custom.conf":
       mode: "644"
       owner: "root"
       group: "root"
       content: |
         client_header_timeout   900;
         client_body_timeout     900;
         send_timeout            900;
         proxy_connect_timeout   900;
         proxy_read_timeout      900;
         proxy_send_timeout      900;
         client_max_body_size    500M;

 container_commands:
   01_restart_nginx:
     command: "sudo service nginx reload"

buildspec.yml:

version: 0.2  
  
phases:  
  install:  
    runtime-versions:  
      python: 3.11  
    commands:  
      - echo Installing dependencies...  
      - pip install -r requirements.txt  
  pre_build:  
    commands:  
      - echo Running migrations...  
      - python manage.py migrate --noinput  
  build:  
    commands:  
      - echo Build completed on `date`  
  
artifacts:  
  files:  
    - '**/*'  

But after deploying the application on the Elastic Beanstalk environment I always see the default nginx configuration.

So my question is, how can I change the configuration of the nginx server during deployment in order to increase the following parameters:


Solution

  • As per the comment that I added this is basically because of the Amazon Linux version that you use in the elasticbeanstalk environment.

    In this case, you are using Amazon Linux 2 or Amazon Linux 2023. So you need to use .platform instead of .ebextensions. if you are using Amazon linux 1, then the .ebextensions would work.

    It should be as follows if you are using Amazon Linux 1.

    ~/workspace/my-app/.ebextensions/nginx/conf.d/nginx.config
    

    if you are using Amazon Linux 2 or Amazon Linux 2023.

    ~/workspace/my-app/.platform/nginx/conf.d/elasticbeanstalk/nginx.config