Django Elastic Beanstalk Static Configuration not working

80 views Asked by At

I'm trying to deploy a Django application on Elastic Beanstalk, and had problems with my css files in the static folder generated by python3 manage.py collectstatic being read by my html templates.

I digged deeper, and turns out when I SSH into the EC2 instance that is hosting my application, the file /etc/nginx/conf.d/elasticbeanstalk/01_static.conf contains

location /static/ {
    alias /var/app/current/static;
    access_log off;
}

My application only works when I manually change this file to contain

location /static/ {
    alias /var/app/current/static/;
    access_log off;
}

The only change here is that I added a trailing slash at the end of the alias. I looked this up more, and was recommended that I create a 01_static.config file in the .ebextensions directory within the root directory, which I did and populated with the following:

files:
  "/etc/nginx/conf.d/elasticbeanstalk/01_static.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      alias /var/app/current/static/;

However, this doesn't seem to do anything. Does anyone have solutions as to how I can add the trailing slash without manually SSHing into an EC2 instance on every deployment? Thanks in advance.

1

There are 1 answers

4
andylee On BEST ANSWER

You can also generate static files with container commands. Try the config files below in ebextensions.

container_commands:   01_collectstatic:
    command: "source /var/app/venv/*/bin/activate && python manage.py collectstatic --noinput"
    leader_only: true 
option_settings:   aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: YOURAPP.settings

--EDIT-- My bad I didn't understand your question. You can make custom settings for nginx using extension.

Try the following config files if you are using AL2.

option_settings:
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static 

Answer from here Static files configuration in AWS not working has this setting combined with my previous answer.

If that doesn't workout, you can also try to make a custom settings for nginx.
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html.

Make a custom config files under .platform/nginx/conf.d/custom.config