[Answered ]-Static Files not being served by NGINX, instead it passes the request to Django app

1👍

So I’m not sure but maybe instead of alias you could use root and rewrite the request on NGINX. It can be like:

location /static{
    rewrite /static/(.*) /static/$1 break;
    root /path/to/project;
}

First try without the rewrite, just setting the root for the static files, if it doesn’t work try to set the root to the path of the project and rewrite to static.

0👍

I found the solution,
By including proxy headers in nginx conf file, requests were handled correctly.

upstream django_server {
    server django:8000;
}

server {
    listen 80;

    location /static/ {
        alias /app/static/;
    }

    location /media/ {
        alias /app/media/;
    }

    location / {
        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;
        proxy_pass http://django_server;
    }
}
👤Anurag

Leave a comment