【问题标题】:django : Serving static files through nginxdjango:通过 nginx 提供静态文件
【发布时间】:2011-01-27 22:44:37
【问题描述】:

我正在为 django 使用 apache+mod_wsgi。
并且所有 css/js/images 都通过 nginx 提供。
出于某种奇怪的原因,当其他人/朋友/同事尝试访问该站点时,jquery/css 没有为他们加载,因此页面看起来很混乱。

我的 html 文件使用这样的代码 -

<link rel="stylesheet" type="text/css" href="http://x.x.x.x:8000/css/custom.css"/>
<script type="text/javascript" src="http://1x.x.x.x:8000/js/custom.js"></script>

我在sites-available的nginx配置是这样的-

    server {   
         listen   8000;   
         server_name  localhost;

         access_log  /var/log/nginx/aa8000.access.log;    
         error_log  /var/log/nginx/aa8000.error.log;    

           location / {   
               index  index.html index.htm;    
           }    

         location /static/ {    
            autoindex on;    
            root   /opt/aa/webroot/;    
         }    
     }   

有一个目录/opt/aa/webroot/static/,有对应的css & js 目录。

奇怪的是,当我访问它们时页面显示正常。
我已经清除了我的缓存/等,但页面加载对我来说很好,可以从各种浏览器中加载。

另外,我在 nginx 日志文件中没有看到任何 404 错误。

任何指针都会很棒。

【问题讨论】:

  • 我知道这可能很明显,但它不适合我,在你进行更改后运行:sudo nginx -s reload 这应该可以工作,它对我有用

标签: django nginx


【解决方案1】:

我认为在位置块中使用root 是不正确的。我使用alias,它工作正常,即使没有重新配置 django。

# django settings.py
MEDIA_URL = '/static/'

# nginx server config
server {   
    ...
    location /static {    
        autoindex on;    
        alias /opt/aa/webroot/;    
    }
}

希望这能让事情变得更简单。

【讨论】:

  • 确实,alias 是为整个目录提供服务的正确方法。
  • 同样适用于我,因为 /static 和 /static/ 是不同的位置,在第二个变体中,我总是从 nginx 得到权限错误。
  • 对于任何阅读本文的人。 Nginx 文档明确指出,“当位置与指令值的最后一部分匹配时,最好使用根指令”nginx.org/en/docs/http/ngx_http_core_module.html#alias 另请参阅此答案:serverfault.com/a/278359
  • 太棒了,谢谢你,我希望这在乘客教程中得到解释。我花了 4 天时间才找到这些信息。别名对我来说是正确的选择,因为该文件夹被称为“资产”而不是“静态”,因此您需要提供完全不同的名称而不仅仅是路径。
  • 在我的例子中,我在“location /static/ {}”中有一个斜杠。删除尾部斜杠并使用静态文件路径的别名对我有用。现在应该是 "location /static { alias }" 然后重新加载/重启 nginx。
【解决方案2】:
  1. server_name 必须与 link/script URL 中的主机名匹配。将您的配置声明为此接口的默认配置:端口对 (listen 8000 default)
  2. Nginx 必须监听主机 IP 绑定的接口(在你的情况下似乎没问题)

【讨论】:

  • 3. js/css 和其他静态文件的 URI 应该在你的 html 文件中以 /static/ 前缀开头,以便它可以匹配到你的 nginx conf 中的 /static/ 位置块。
【解决方案3】:

我也为此苦苦挣扎。但是,以下技巧对我有用:

server {   
     listen   8000;   
     server_name  localhost;

     access_log  /var/log/nginx/aa8000.access.log;    
     error_log  /var/log/nginx/aa8000.error.log;    

       location / {   
           index  index.html index.htm;    
       }    

     location ^/static/ {    
        autoindex on;    
        root   /opt/aa/webroot/;    
     }    
 } 

我刚刚使用^ 将静态标记为正则表达式,并且 nginx 开始提供静态文件。不需要在 Django 端进行任何修改。

【讨论】:

    【解决方案4】:

    MEDIA_URL 不得用于提供 js 等静态内容。Django 提供了一个单独的 STATIC_URL 设置选项可供使用。

    所以这个可以改成

    <script type="text/javascript" src="{{STATIC_URL}}js/jquery-1.3.2.min.js"></script>
    

    另外,像这样使用静态文件应用程序模板标签更标准:

    {% load static from staticfiles %}
    <script type="text/javascript" src="{% static 'js/jquery-1.3.2.min.js' %}"></script>
    

    Docs Here

    【讨论】:

      【解决方案5】:

      Fim & Alexander - 感谢那些帮助的提示。
      这是我为困在同一条船上的人解决它的方法 -

      settings.py -

      >MEDIA_ROOT = ''    
      MEDIA_URL = 'http://x.x.x.x:8000/static/'    
      

      在我的 html 中 -

      <script type="text/javascript" src="{{MEDIA_URL}}js/jquery-1.3.2.min.js"></script>
      

      在我看来.py -

      return render_to_response('templates/login-register.html', {},
                                context_instance=RequestContext(request));    
      

      nginx 在站点可用的配置文件中 -

      listen x.x.x.x:8000;    
      server_name x.x.x.x.;
      

      重启 nginx
      重启 apache

      【讨论】:

      • 除非你的 nginx conf 中还有“location /static”等,否则 Django 将提供静态文件。适合测试和开发,但不适用于生产。