我使用的是Apache + mod_wsgi for django。
所有的css/js/images通过Nginx服务。
对于一些奇怪的原因,当others/friends/colleagues尝试访问该网站, jquery/css没有得到加载他们,因此该页面看起来混乱起来。
我的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>
我的网站nginxconfiguration – 可用是这样的 –
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目录。
奇怪的是,当我访问它们时,页面显示正常。 我已经清除了我的cache/etc ,但从各种浏览器的页面加载罚款。
另外,在nginx日志文件中我没有看到任何404错误。 其实nginx的日志根本没有被刷新。
我使用root重新启动了nginx服务器,是不正确的? 在nginxconfiguration文件中定义了一个用户www-data。
任何指针都会很棒。
首先,在使用django的时候,如果你使用settings.py中定义的MEDIA_URL服务你的css / js,那就更好了。
现在,假设您有一个MEDIA_URL='/site_media/'并且在您的模板中有src={{MEDIA_URL}}js/foo.js或href={{MEDIA_URL}}css/bar.css 。
在你的nginxconfiguration中,你需要一个合适的MEDIA_URL条目。 就像是:
server { ... location /site_media/ { autoindex on; root /opt/aa/webroot/; } }
这应该照顾这个问题。 只是为了检查你可以添加/static/包含js / css文件的href / src,看看会发生什么。
server_name必须与link / script URL中的主机名匹配。 要么声明你的configuration默认为这个接口:端口对( listen 8000 default ) 把我所做的,在这里,因为评论区似乎已经混乱了。
Fim&Alaz – 感谢那些帮助的提示。
下面是我解决这个问题的方法:
settings.py –
MEDIA_ROOT =''
MEDIA_URL =' http:// xxxx:8000 / static / '
In my html –
<script type =“text / javascript”src =“{{MEDIA_URL}} js / jquery-1.3.2.min.js”> </ script>
In my view –
return'render_to_response('templates / login-register.html',{},context_instance = RequestContext(request));
nginx –
听xxxx:8000;
server_name xxxx;
Restarted nginx
Restarted apache