realpython / dockerizing-django

https://realpython.com/blog/python/django-development-with-docker-compose-and-machine/
1.34k stars 484 forks source link

Serving of /static in production is not working. #59

Closed mmichealjroberts closed 5 years ago

mmichealjroberts commented 5 years ago

The serving of static assets in the production deployment is not working.

The call to anything in /static/ returns a 404 Nginx Error.

mmichealjroberts commented 5 years ago

I have done the usual python manage.py collectstatic --no-input command on run.

Tried:

$ docker-compose -f production.yml run nginx ls /usr/src/app/static

Output:

$ ls: cannot access /usr/src/app/static: No such file or directory

mmichealjroberts commented 5 years ago

$ docker-compose -f production.yml run nginx ls /www/static

Returns:

$ admin

mmichealjroberts commented 5 years ago

The fix was to change the location alias to point to the same as in the production.yml file, i.e., /www/static.

Change sites-enabled/django-project to:

server {

    listen 80;
    server_name example.org;
    charset utf-8;

    location /static {
        alias /www/static;
    }

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

}