【问题标题】:django production deployment does not load static filesdjango 生产部署不加载静态文件
【发布时间】:2015-08-22 09:06:43
【问题描述】:

我已经尝试了大约一整天的时间在生产环境中部署 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 中的文件夹包含所有适当的文件。

我的部署仍然没有提供任何静态文件:/

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

提前谢谢你

【问题讨论】:

    标签: python django nginx django-staticfiles static-files


    【解决方案1】:

    为服务器配置添加静态别名

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

    【讨论】:

    • 它不会吐出任何相关错误。最后一个是我之前解决的2015/06/08 00:24:50 [emerg] 3209#0: still could not bind()
    • warn 指令添加到error_log error_log /tmp/nginx.error.log warn;。重启 nginx 并查看日志。
    • 仍然,它没有吐出任何东西:/
    • Nginx 以user nobody nogroup; 运行,这可能是从 sattic 目录读取文件的权限问题。您可以尝试将其更改为当前用户组(这里似乎是 ubuntu)。
    • 将其更改为 user ubuntu ubuntu 仍然没有:/
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-05
      • 1970-01-01
      • 2014-06-18
      • 2014-10-13
      • 2017-03-01
      • 1970-01-01
      • 2018-02-18
      • 2017-12-06
      相关资源
      最近更新 更多