【问题标题】:Django running under Ngnix doesn't serve static content在 Ngnix 下运行的 Django 不提供静态内容
【发布时间】:2012-04-28 20:54:39
【问题描述】:

我按照guide 在 Ubuntu 11.10 上部署 Django + Nginx。 Django 运行良好,但 Nginx 不提供任何静态文件。

我正在尝试将 app.com/static/ 指向本地文件夹 /project/app/static/

我也尝试了question 中的所有内容,但无济于事

这是我的 settings.py 的摘录:

STATIC_ROOT = ''

STATIC_URL = '/static/'

ADMIN_MEDIA_PREFIX = DOMAIN_URL + '/static/admin/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

来自 nginx.conf

server {
                listen 80;
                server_name localhost;
                # site_media - folder in uri for static files
                location /static/  {
                        autoindex on;
                        alias /project/app/static;
                        }
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|p$
  access_log   off; # po co mi logi obrazków :)
  expires      30d;
}
                location / {
                        # host and port to fastcgi server
                        fastcgi_pass 127.0.0.1:8080;
                        fastcgi_param PATH_INFO $fastcgi_script_name;
                        fastcgi_param REQUEST_METHOD $request_method;
                        fastcgi_param QUERY_STRING $query_string;
                        fastcgi_param CONTENT_TYPE $content_type;
                        fastcgi_param CONTENT_LENGTH $content_length;
                        fastcgi_pass_header Authorization;
                        fastcgi_intercept_errors off;
                        }

我在这里做什么?

这是 nginx 日志

  WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$
2012/04/17 13:48:24 [error] 15598#0: *3 open() "/etc/nginx/html/static/css/rese$
2012/04/17 13:48:24 [error] 15598#0: *4 open() "/etc/nginx/html/static/images/l$
2012/04/17 13:48:24 [error] 15598#0: *5 open() "/etc/nginx/html/static/js/scrip$
2012/04/17 13:48:24 [error] 15598#0: *6 open() "/etc/nginx/html/static/js/jquery$
2012/04/17 13:48:24 [error] 15598#0: *7 open() "/etc/nginx/html/static/style.cs$
2012/04/17 13:48:24 [error] 15598#0: *8 open() "/etc/nginx/html/static/js/jquery$
2012/04/17 13:48:24 [error] 15598#0: *5 open() "/etc/nginx/html/static/images/i$
2012/04/17 13:48:24 [error] 15598#0: *7 open() "/etc/nginx/html/static/images/i$
2012/04/17 13:48:24 [error] 15598#0: *8 open() "/etc/nginx/html/static/images/i$
2012/04/17 13:48:24 [error] 15598#0: *6 open() "/etc/nginx/html/static/images/s$
2012/04/17 13:48:24 [error] 15598#0: *3 open() "/etc/nginx/html/static/images/s$
2012/04/17 13:48:24 [error] 15598#0: *4 open() "/etc/nginx/html/static/images/s$
2012/04/17 14:07:33 [error] 16231#0: *1 FastCGI sent in stderr: "WSGIServer: mi$
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$
2012/04/17 14:07:33 [error] 16231#0: *3 open() "/etc/nginx/html/static/css/rese$
2012/04/17 14:07:33 [error] 16231#0: *4 open() "/etc/nginx/html/static/style.cs$
2012/04/17 14:07:33 [error] 16231#0: *5 open() "/etc/nginx/html/static/js/jquery$
2012/04/17 14:07:33 [error] 16231#0: *6 open() "/etc/nginx/html/static/js/scrip$
2012/04/17 14:07:33 [error] 16231#0: *7 open() "/etc/nginx/html/static/js/jquery$
2012/04/17 14:07:34 [error] 16231#0: *7 open() "/etc/nginx/html/static/images/l$
2012/04/17 14:07:34 [error] 16231#0: *6 open() "/etc/nginx/html/static/images/i$
2012/04/17 14:07:34 [error] 16231#0: *4 open() "/etc/nginx/html/static/images/i$
2012/04/17 14:07:34 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/i$
2012/04/17 14:07:34 [error] 16231#0: *3 open() "/etc/nginx/html/static/images/s$
2012/04/17 14:07:34 [error] 16231#0: *8 open() "/etc/nginx/html/static/images/s$
2012/04/17 14:07:34 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/s$
2012/04/17 14:07:35 [error] 16231#0: *5 open() "/etc/nginx/html/static/images/f$
2012/04/17 14:10:30 [error] 16231#0: *9 FastCGI sent in stderr: "WSGIServer: mi$
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$
2012/04/17 14:11:57 [error] 16471#0: *1 FastCGI sent in stderr: "WSGIServer: mi$
WSGIServer: missing FastCGI param SERVER_PORT required by WSGI!
WSGIServer: missing FastCGI param SERVER_PROTOCOL required by WSGI!" while read$

更新:

已解决。我没有指定 nginx.conf 中的根目录 一旦我这样做了,一切就都到位了!

捂脸

 server_name localhost;
                root /home/user/projects;
                # site_media - folder in uri for static files
                location /static/  {

                        root /static/;
}

【问题讨论】:

  • 这不是 Django 问题。相反,您有一个 nginx 配置问题。检查您的 nginx 日志文件并查看问题所在。如果您无法理解错误,请在此处发布错误。
  • 如果您找到了问题的答案,您应该将其作为答案发布,而不是添加到您的问题中。
  • 声誉低于 100 的用户在提出问题后的 8 小时内(我知道是任意的)不能发布自己的答案。因此更新。
  • 您实际上本可以留在alias。这就是我在 nginx conf 中所做的一切,而不是再次设置 root。

标签: django nginx


【解决方案1】:

我没有指定 nginx.conf 中的根目录 一旦我这样做了,一切就都到位了! 掌心

            server_name localhost;
            root /home/user/projects/;
            # site_media - folder in uri for static files
            location /static/  {

                    root /static/;}

【讨论】:

    【解决方案2】:

    看起来 nginx 正在 为文件提供服务,只是它们不存在。这是因为 1) 你没有指定 STATIC_ROOT 和 2) 你显然没有运行 collectstatic 这会给你一个错误。

    首先,设置:

    STATIC_ROOT = '/project/app/static'
    

    或者,为了更好的便携性:

    import os.path
    
    STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
    

    然后,在您的生产环境中运行以下命令:

    python manage.py collectstatic
    

    【讨论】:

      【解决方案3】:

      Django 实际上并不是为提供静态文件而设计的。他们在整个文档中多次提到这一点。只需使用 nginx 分别为您的静态文件提供服务,然后将 django 指向该位置。

      【讨论】:

      • 是的,我正在尝试将 nginx 指向静态文件目录,如我的 nginx.conf 中所示
      猜你喜欢
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 2020-12-29
      • 2012-04-27
      • 2020-09-09
      • 1970-01-01
      相关资源
      最近更新 更多