django使用nginx提供静态文件

时间:2017-05-04 13:00:31

标签: django nginx gunicorn

我似乎无法使路由正常工作。我跑的时候还包括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#

2 个答案:

答案 0 :(得分:0)

您的STATIC_ROOT是“/ code / static”但您的nginx配置将/ static /映射到“/ code”。这些应该是一样的。

答案 1 :(得分:0)

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

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

仅供参考deploy django with nginx