繁体   English   中英

Django生产部署不会加载静态文件

[英]django production deployment does not load static files

我已经花了整整一天的时间在生产中部署Django的静态文件,但是直到现在我还没有运气,所以我绝对需要社区的帮助!

我的nginx配置是:

worker_processes 1;

user nobody nogroup;
pid /tmp/nginx.pid;
error_log /tmp/nginx.error.log;

events {
    worker_connections 1024;
    accept_mutex off;
}

http {
    include mime.types;
    default_type application/octet-stream;
    access_log /tmp/nginx.access.log combined;
    sendfile on;

    upstream app_server {
        server unix:/tmp/gunicorn.sock fail_timeout=0;
        # For a TCP configuration:
        # server 192.168.0.7:8000 fail_timeout=0;
    }

    server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # path for static files
        root /home/ubuntu/src/static;

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app_server;
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /home/ubuntu/src/static;
        }
    }
}

在django settings.py文件中,我进行了以下设置:

STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 )
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

当然,我已经运行了collectstatic django命令,并且路径/home/ubuntu/src/static的文件夹存在,其中包含所有适当的文件。

我的部署仍然没有服务器任何静态文件:/

有人知道我的设置可能有什么问题吗?

先感谢您

在这里使用了很棒的教程进行设置。 希望这会有所帮助。 我可以看到的一个明显变化是将location /static/用于静态文件,然后将location /直接转发给gunicorn(在我的情况下为uwsgi)

将静态别名添加到服务器配置

location /static {
            alias /home/ubuntu/src/static; # your Django project's static files - amend as required
         }

完整配置应该像

server {
        listen 80 default;
        client_max_body_size 4G;
        server_name _;

        keepalive_timeout 5;

        # path for static files
        # root /home/ubuntu/src/static;

        location /static {
            alias /home/ubuntu/src/static; # your Django project's static files - amend as required
         }

        location / {
            # checks for static file, if not found proxy to app
            try_files $uri @proxy_to_app;
        }



        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;

            proxy_pass   http://app_server;
        }

        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /home/ubuntu/src/static;
        }
    }
}

暂无
暂无

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

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