Nginx不服务于静态 [英] Nginx doesn't serve static

查看:137
本文介绍了Nginx不服务于静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ubuntu Server 9.04上运行Django。

I'm running Django on Ubuntu Server 9.04.

Django运行良好,但nginx不返回静态文件 - 总是404。

Django works well, but nginx doesn't return static files - always 404.

这是配置:

server {
    listen 80;
    server_name localhost;

    #site_media - folder in uri for static files
    location /static  {
        root /home/user/www/oil/oil_database/static_files;
        autoindex on;
    }

    #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) {
    #   root /home/user/www/oil/oil_database/static_files;
    #   access_log off;
    #   expires 30d;
    #}

    location / {
        root html;
        index index.html index.htm;
        # host and port to fastcgi server
        #fastcgi_pass 127.0.0.1:8080;
        fastcgi_pass unix:/home/user/www/oil/oil_database/oil.sock;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        fastcgi_param REQUEST_METHOD $request_method;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_param CONTENT_TYPE $content_type;
        fastcgi_param CONTENT_LENGTH $content_length;
        fastcgi_pass_header Authorization;
        fastcgi_intercept_errors off;
    }

    access_log /var/log/nginx/localhost.access_log;
    error_log /var/log/nginx/localhost.error_log;
}

Nginx版本是0.6.35。

Nginx version is 0.6.35.

所有目录都存在并进行了777(调试偏执)。当我取消注释时,注释掉的块不会有帮助。

All directories exist and made 777 (debugging paranoia). The commented-out block doesn't help when I uncomment it.

推荐答案

你的目录设置如何?您在 / home / user / www / oil / oil_database / static_files 中有一个文件夹 static ?在这种情况下,指令应该看起来像这样(注意 / static / 中的尾部斜杠):

How is your directory setup? Do you have a folder static in /home/user/www/oil/oil_database/static_files? In that case, the directive should look like this (note the trailing slash in /static/):

location /static/  {
    autoindex    on;
    root /home/user/www/oil/oil_database/static_files;
}

如果要映射路径 / home /用户/ www / oil / oil_database / static_files 到URL / static / ,你必须要么

If you want to map the path /home/user/www/oil/oil_database/static_files to the URL /static/, you have to either


  • 将文件夹 static_files 重命名为 static 并使用此指令:

  • rename the folder static_files to static and use this directive:

location /static/  {
    autoindex    on;
    root /home/user/www/oil/oil_database/;
}


  • 使用别名:

  • use an alias:

    location /static/  {
        autoindex    on;
        alias /home/user/www/oil/oil_database/static_files/;
    }
    


  • root 别名 指令。

    这篇关于Nginx不服务于静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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