Django将静态文件与nginx一起使用



我似乎无法进行正常工作的路由。我运行时还包括collectstatic

在我的settings.py中,我有关注代码

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

在我的nginx配置文件中

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;
        }
    }
}

错误日志

 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/"

服务器目录

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#

尝试在位置更新您的nginx配置文件:静态部分

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

fyi用nginx

部署django

您的static_root是"/code/static",但是您的nginx配置映射/static/to"/code"。这些应该是相同的。

最新更新