3
votes

I have running my django application in my ubuntu lxc container at 127.0.0.1:8001 with gunicorn and nginx.

This is nginx conf in host:

upstream django_app  {
      server 10.0.8.100:8001;
}

server {
        listen  80;
        server_name     mysite.com;

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

And this is nginx conf in container:

server {
    listen 80;

    location / {
         proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
         proxy_set_header Host $http_host;
         proxy_pass http://127.0.0.1:8001;
     }

    location /static {
            alias /opt/static_files/django_app/;
    }

}

This configuration is working. I can see my application running from browser, but static files is not loading. I have 404 not found for each static file in browser.

the path /opt/static_files/django_app/ is where all static files collected with collectstatic command.

I could not find a way to serve static files with upstream.

Thank you

1
Does it work if you put the static location first! - Daniel Roseman
@DanielRoseman No, sir. It didn't work. Everything looks fine. But somehow, host cannot reach /opt/static_files/django_app/ path. which is in container machine. - user6447377

1 Answers

0
votes

/opt/static_files/ should be the same as the STATIC_ROOT setting in your settings.py django file.

Also, change:

location /static {
        alias /opt/static_files/django_app/;
}

to:

location /static/ {
        alias /opt/static_files/django_app/;
}