django: 服务静态文件 nginx

我用 apache + mod_wsgi 为了 django.

和所有

css/js/images

服务通过 nginx.

对于一些奇怪的原因

others/friends/colleagues

试着去网站

jquery/css

没有加载它们,所以页面随机查找。

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

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

我的配置 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/;
}
}

有一个目录

/opt/aa/webroot/static/

谁有适当的

css

&

js

目录。

当我向他们吸引时,这通常会显示页面。 我清理了自己的

cache/etc

, 但我的页面正常从不同的浏览器加载。

另外,我没有看到任何错误 404 在日志文件中 nginx. 实际上是杂志 nginx 根本没有更新。

我重新启动了服务器 nginx 与权利有关 root, 这是不对的? 自定义WWW数据在配置文件中定义 nginx.

任何指针都会很棒。
已邀请:

帅驴

赞同来自:


server_name

必须适合主机的名称

link

/

script

URL。 或声明此接口的默认配置:对端口 (

listen 8000 default

)

Nginx 必须收听主机的IP地址绑定的接口 (在你的情况下,这是正常的)

窦买办

赞同来自:

首先,使用时 django 如果你为你服务了 css / js, 使用 MEDIA_URL, 多么肯定 settings.py.

现在假设你有

MEDIA_URL='/site_media/'

你有你的模板

src={{MEDIA_URL}}js/foo.js

或者

href={{MEDIA_URL}}css/bar.css

.

在您的配置中 nginx 您将需要相应的条目 MEDIA_URL. 有点:

server {
...

location /site_media/ {
autoindex on;
root /opt/aa/webroot/;
}
}

这应该解决问题。 只是为了检查你是否可以添加

/static/

在 href / src 启用文件 js / css 并看看会发生什么。

石油百科

赞同来自:

我把我所做的事情放在这里,因为评论领域似乎很困惑。

FIM和ALAZ.

- 谢谢你帮助的提示。

这就是我如何为被困在同一条船上的每个人决定它的方式:


settings.py

-

MEDIA_ROOT = ''

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


In my html

-

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


In my view

-

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


nginx

-

听 x.x.x.x: 8000;

服务器名称 x.x.x.x .;


Restarted nginx


Restarted apache

要回复问题请先登录注册