root
root

Reputation: 1583

django serving static file with nginx

I couldn't seem to get routing to work properly. I've also included the collectstatic when i run.

In my settings.py i have the follow code

STATIC_URL = '/static/'
STATIC_ROOT = "/code/static"

in my nginx config file

worker_processes 1;

events {

    worker_connections 1024;

}

http {

    server {
        listen 80;
        server_name example.org;

        access_log /dev/stdout;
        error_log /dev/stdout info;

        location /static/ {
            autoindex on;
            root /code;
        }


        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;
            proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

Error Log

 2017/05/04 13:14:54 [error] 5#5: *1 open() "/code/static/css/bootstrap.min.css" failed (2: No such file or directory),client: 172.18.0.1, server: example.org, request: "GET /static/css/bootstrap.min.css HTTP/1.1", host: "localhost", referrer: "http://localhost/polls/"

Server Directory

root@729dc46f5760:/# cd /code/static
root@729dc46f5760:/code/static# ls -l
total 16
drwxr-xr-x 6 root root 4096 May  4 13:40 admin
drwxr-xr-x 2 root root 4096 May  4 13:40 css
drwxr-xr-x 2 root root 4096 May  4 13:40 fonts
drwxr-xr-x 2 root root 4096 May  4 13:40 scripts
root@729dc46f5760:/code/static#

Upvotes: 1

Views: 1653

Answers (2)

Phuong Vu
Phuong Vu

Reputation: 537

Try update your nginx config file at location:static section to this

...
location /static/ {
    autoindex on;
    alias /code/static/;
}
...

FYI deploy django with nginx

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599450

Your STATIC_ROOT is "/code/static" but your nginx configuration maps /static/ to "/code". These should be the same.

Upvotes: 0

Related Questions