简体   繁体   English

django+gunicorn+nginx 404 服务静态文件

[英]django+gunicorn+nginx 404 serving static files

I have django+gunicorn+nginx running on 192.168.1.81:3000.我在 192.168.1.81:3000 上运行 django+gunicorn+nginx。 The web app will not serve any static files; Web 应用程序不会提供任何静态文件; it returns a 404 error.它返回 404 错误。 This suggests that there is a problem with the nginx virtual server config file.这表明 nginx 虚拟服务器配置文件存在问题。 I have tried several solutions offered on stack overflow with no success.我已经尝试了几种关于堆栈溢出的解决方案,但都没有成功。 What is wrong with the nginx virtual server file? 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;
                }
        }
}

In Nginx virtual server conf file inside server section you need to add to location sections for Nginx can serve files in /static/ and /media/ folders:服务器部分内的 Nginx 虚拟服务器 conf 文件中,您需要添加到位置部分,以便 Nginx 可以为 /static/ 和 /media/ 文件夹中的文件提供服务:

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

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

After that test Nginx configuration:在测试 Nginx 配置之后:

sudo nginx -t

and reload Nginx:并重新加载 Nginx:

sudo nginx -s reload

(or restart - sudo /etc/init.d/nginx restart ) (或重新启动 - sudo /etc/init.d/nginx restart

try this config using server root and try_files使用服务器根目录和 try_files 尝试此配置

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

This will try to find your static file and then move onto your app server.这将尝试找到您的静态文件,然后移至您的应用服务器。

Make sure nginx is running as the right user to access your files and that your static files permissions are correctly set perhaps with:确保 nginx 以正确的用户身份运行以访问您的文件,并且您的静态文件权限可能已正确设置:

chmod -R u=rwX,g=rwX,o=rX static_dir

我的情况是静态目录的权限问题,它在分配适当的权限后工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM