我正在尝试在生产环境中部署Django应用程序,但我似乎无法正确渲染CSS。在我当地的环境中它工作正常。在设置中,我已设置:
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'
在服务器上,我运行了collectstatic将文件收集到以下文件夹中:
STATIC_ROOT = '/sites/thetweethereafter.com/public/static'
网络服务器是nginx,我的nginx.conf是:
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name www.thetweethereafter.com;
rewrite ^/(.*) http://thetweethereafter.com/$1 permanent;
}
server {
listen 80;
server_name thetweethereafter.com;
access_log /sites/thetweethereafter.com/logs/access.log;
error_log /sites/thetweethereafter.com/logs/error.log;
location /static {
autoindex on;
root /sites/thetweethereafter.com/public/;
}
location / {
proxy_pass http://127.0.0.1:29000;
}
}
}
如果我直接浏览静态文件,我可以毫无问题地使用它们。
http://thetweethereafter.com/static/css/styles.css
但是,当我加载引用其中一个文件的页面时,浏览器不会呈现它们。
我不能为我的生活弄清楚我做错了什么。我有很多其他项目以类似的方式设置,它们工作正常。我错过了什么?
答案 0 :(得分:6)
检查您的网站索引我注意到css文件是“text / plain”而不是“text / css”文件类型。所以问题应该依赖于服务器配置。
Nginx必须根据文件扩展名知道应用哪种mime类型。在新的debian安装中,http部分中有一个include指令,如下所示:
http {
[...]
include /etc/nginx/mime.types;
[...]
}
这应该可以解决问题。
答案 1 :(得分:2)
之前我遇到过完全相同的问题。要解决CSS和其他外部静态文件:
编辑mime.types文件(如果不存在,则创建一个)。 (您的mime.types文件可能位于/ etc / nginx /中)
复制并粘贴(引自http://wiki.nginx.org/FullExample):
types {
text/html html htm shtml;
text/css css;
text/xml xml rss;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
text/plain txt;
text/x-component htc;
text/mathml mml;
image/png png;
image/x-icon ico;
image/x-jng jng;
image/vnd.wap.wbmp wbmp;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/pdf pdf;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/zip zip;
application/octet-stream deb;
application/octet-stream bin exe dll;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/mpeg mp3;
audio/x-realaudio ra;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-msvideo avi;
video/x-ms-wmv wmv;
video/x-ms-asf asx asf;
video/x-mng mng;
}
接下来,插入nginx.conf文件的http部分(位于/ etc / nginx /中):
include /etc/nginx/mime.types;
(或任何指向mime.types文件的路径)
最后,通过在命令行输入来重新加载nginx.conf:
sudo /etc/init.d/nginx reload
我还会重新启动nginx:
sudo /etc/init.d/nginx restart
编辑:我不知道为什么上面的代码片段格式如此古怪。道歉。
答案 2 :(得分:0)
确保您的location /static
规则在nginx conf中的location /
规则之前。以下是我的例子:
server {
listen 81; ## listen for ipv4
server_name 172.27.111.152;
root /opt/projects/msv/www;
access_log /var/log/nginx/msv.access.log;
location /static {
root /opt/projects/msv/www;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov) {
access_log off;
expires 30d;
}
location / {
uwsgi_pass 127.0.0.1:3002;
include uwsgi_params;
}
}