django:通过nginx提供静态文件

我使用的是Apache + mod_wsgi for django
所有的css/js/images通过nginx
对于一些奇怪的原因,当others/friends/colleagues尝试访问该网站, jquery/css not getting loaded他们,因此该页面看起来混乱起来。

我的HTML文件使用这样的代码 –

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

我的sites-available nginxconfigurationsites-available是这样的 –

  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/有相应的cssjs目录。

奇怪的是,当我访问它们时,页面显示正常。
我已经清除了我的caching/等,但从各种浏览器的页面加载罚款。

此外,我没有看到Nginx日志文件中的任何错误404。

任何指针都会很棒。

  1. server_name必须与link / script URL中的主机名匹配。 要么声明你的配置默认为这个接口:端口对( listen 8000 default
  2. Nginx必须监听你的主机的IP绑定的接口(在你的情况下似乎确定)

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

 # django settings.py MEDIA_URL = '/static/' # nginx server config server { ... location /static { autoindex on; alias /opt/aa/webroot/; } } 

希望这使事情变得更简单。

不得使用MEDIA_URL来为js等静态内容提供服务。Django提供了可以使用的单独的STATIC_URL设置选项。

所以这可以改变为

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

此外,它更标准的使用静态文件应用程序templatetag是这样的:

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

文档在这里

菲姆和亚历山大 – 感谢那些帮助的提示。
下面是我解决这个问题的方法:

settings.py

 >MEDIA_ROOT = '' MEDIA_URL = 'http://xxxx:8000/static/' 

在我的HTML

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

在我的views.py

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

nginx里的网站,可用的配置文件 –

 listen xxxx:8000; server_name xxxx; 

重新启动nginx
重新启动apache

我也为此而挣扎。 但是,下面的技巧为我工作:

 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方面不需要修改。