简体   繁体   English

django:通过 nginx 提供静态文件

[英]django : Serving static files through nginx

I'm using apache+mod_wsgi for django.我正在为 Django 使用 apache+mod_wsgi。
And all css/js/images are served through nginx .并且所有 css/js/images 都是通过nginx
For some odd reason, when others/friends/colleagues try accessing the site, jquery/css is not getting loaded for them, hence the page looks jumbled up.出于某种奇怪的原因,当其他人/朋友/同事尝试访问该站点时,jquery/css 没有为他们加载,因此页面看起来很混乱。

My html files use code like this -我的 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>

My nginx configuration in sites-available is like this -我在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/;    
         }    
     }   

There is a directory /opt/aa/webroot/static/ which have corresponding css & js directories.有一个目录/opt/aa/webroot/static/有对应的css & js目录。

The odd thing is that the pages show fine when I access them.奇怪的是,当我访问页面时,页面显示正常。
I have cleared my cache/etc, but the page loads fine for me, from various browsers.我已经清除了我的缓存/等,但是从各种浏览器加载页面对我来说很好。

Also, I don't see 404 any error in the nginx log files.另外,我在 nginx 日志文件中没有看到 404 任何错误。

Any pointers would be great.任何指针都会很棒。

I think using root in location block is incorrect.我认为在位置块中使用root是不正确的。 I use alias and it works fine, even without re-configuring django.我使用alias并且它工作正常,即使没有重新配置 django。

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

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

Hope this makes things simpler.希望这能让事情变得更简单。

  1. server_name must match hostname in link / script URLs. server_name必须与link / script URL 中的主机名匹配。 Either declare your configuration as default for this interface:port pair ( listen 8000 default )将您的配置声明为此接口的默认配置:端口对( listen 8000 default
  2. Nginx must listen on the interface where your host's IP is bound (seems ok in your case) Nginx 必须侦听绑定主机 IP 的接口(在您的情况下似乎没问题)

I also struggled with this.我也为此苦苦挣扎。 However, following trick worked for me:但是,以下技巧对我有用:

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

I just marked static as a regex with ^ and nginx started serving static files.我只是用^将 static 标记为正则表达式,nginx 开始提供静态文件。 No modification on Django side was needed.不需要在 Django 端进行修改。

MEDIA_URL shall not be used to serve the Static content like js etc. Django provides a separate STATIC_URL settings option that can be used. MEDIA_URL 不得用于提供静态内容,如 js 等。Django 提供了一个单独的 STATIC_URL 设置选项可以使用。

So this can be changed as所以这可以改变为

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

Also, its more standard to use staticfile app templatetag like this:此外,像这样使用静态文件应用程序模板标签更标准:

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

Docs Here 文档在这里

Fim & Alexander - Thanks for the hints those helped. Fim & Alexander - 感谢您提供帮助的提示。
Here is how I solved it for anyone stuck in the same boat -这是我为困在同一条船上的任何人解决它的方法 -

settings.py -设置.py -

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

In my html -在我的 html -

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

In my views.py -在我的 views.py 中-

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

nginx inside the sites-available config file - nginx在站点可用的配置文件中 -

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

Restarted nginx重启nginx
Restarted apache重启apache

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

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