django + gunicorn + nginx 404提供静态文件

时间:2015-09-04 04:46:37

标签: django nginx gunicorn

我有运行在192.168.1.81:3000的django + gunicorn + nginx。 Web应用程序不会提供任何静态文件;它返回404错误。这表明nginx虚拟服务器配置文件存在问题。我已经尝试过多次提供堆栈溢出的解决方案但没有成功。 nginx虚拟服务器文件有什么问题?

upstream app_server {
        server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}

server {
        listen 80;
        server_name 192.168.1.81;
        client_max_body_size 4G;
        access_log /home/pi/door_site/logs/nginx-access.log;
        error_log /home/pi/door_site/logs/nginx-error.log;
        location /static/ {
                alias /home/pi/door_site/static/;
        }

        location / {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                if (!-f $request_filename) {
                        proxy_pass http://app_server;
                        break;
                }
        }
}

3 个答案:

答案 0 :(得分:0)

服务器部分内的Nginx虚拟服务器配置文件中,您需要添加到位置部分,以便Nginx可以在/ static /和/ media / folders中提供文件:

location /media {
    alias /path/to/your/folder/media;
}

location /static {
    alias /path/to/your/folder/static;
}

之后测试Nginx配置:

sudo nginx -t

并重新加载Nginx:

sudo nginx -s reload

(或重启 - sudo /etc/init.d/nginx restart

答案 1 :(得分:0)

使用服务器根目录和try_files尝试此配置

data-win-options="{
layout:{type: WinJS.UI.ListLayout },
itemsReorderable: true,
...

这将尝试找到您的静态文件,然后移至您的应用服务器。

确保nginx以访问您文件的正确用户身份运行,并且可以使用以下方法正确设置您的静态文件权限:

upstream app_server {
    server unix:/home/pi/door_site/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;
    server_name 192.168.1.81;
    client_max_body_size 4G;
    access_log /home/pi/door_site/logs/nginx-access.log;
    error_log /home/pi/door_site/logs/nginx-error.log;

    root /path/to/root # place your static directories in root i.e /path/to/root/static

    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

答案 2 :(得分:0)

我个人认为这是静态目录上的权限问题,并且在分配了适当的权限后才起作用。