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

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

问题描述

我在 Django 中使用 apache+mod_wsgi.
并且所有 css/js/images 都通过 nginx 提供.
出于某种奇怪的原因,当其他人/朋友/同事尝试访问该站点时,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 &js 目录.

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

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

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.

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

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

任何指针都会很棒.

推荐答案

  1. server_name 必须与 link/script URL 中的主机名匹配.将您的配置声明为此接口的默认配置:端口对(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天全站免登陆