django:通过nginx提供静态文件 [英] django : Serving static files through nginx

查看:134
本文介绍了django:通过nginx提供静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用apache + mod_wsgi作为django。

所有 css / js / images 通过 nginx

出于某些奇怪的原因,当 others / friends / colleagues 尝试访问该网站时,对于他们来说,jquery / css 没有加载,因此页面看起来很混乱。

I'm using apache+mod_wsgi for django.
And all css/js/images are served through 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.

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

My html files use code like this -

<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配置是像这样 -

My nginx configuration in sites-available is like this -

    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 &

There is a directory /opt/aa/webroot/static/ which have corresponding css & js directories.

奇怪的是,当我访问它们时,页面显示正常。

我已经清除了我的缓存/ etc,但是从各种浏览器来看,该页面对我来说很好。

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.

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

Also, I don't see 404 any error in the nginx log files.

任何指针都很棒。

推荐答案


  1. server_name 必须与链接中的主机名匹配 / 脚本网址。声明您的配置为此接口的默认值:端口对( listen 8000 default

  2. Nginx必须在主机的IP被绑定(在你的情况下好像好)

  1. server_name must match hostname in link/script URLs. Either declare your configuration as default for this interface:port pair (listen 8000 default)
  2. Nginx must listen on the interface where your host's IP is bound (seems ok in your case)

这篇关于django:通过nginx提供静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆