【问题标题】:Nginx doesn't serve staticNginx 不提供静态服务
【发布时间】:2010-12-01 06:08:35
【问题描述】:

我在 Ubuntu Server 9.04 上运行 Django。

Django 运行良好,但 nginx 不返回静态文件 - 总是 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。

所有目录都存在并生成 777(调试偏执狂)。当我取消注释时,注释掉的块没有帮助。

【问题讨论】:

  • srry,我不明白你的意思

标签: django static nginx


【解决方案1】:

您的目录设置如何? /home/user/www/oil/oil_database/static_files 中有文件夹 static 吗?在这种情况下,指令应如下所示(注意/static/ 中的尾部斜杠):

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

如果要将路径 /home/user/www/oil/oil_database/static_files 映射到 URL /static/,则必须要么

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

    location /static/  {
        autoindex    on;
        root /home/user/www/oil/oil_database/;
    }
    
  • 使用别名:

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

请参阅有关 rootalias 指令的文档。

【讨论】:

  • 不,没有“静态”目录。谢谢,我试试别名。
  • 我遇到了同样的问题,但是这里的解决方案似乎对我不起作用。我在这里发布了我自己的问题stackoverflow.com/questions/24131830/…
【解决方案2】:

我的 Django 网站有一个类似的配置,但我认为您想为您的媒体使用 alias 而不是 root。例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2016-05-23
    • 1970-01-01
    • 2021-12-02
    • 2020-09-14
    • 2019-06-27
    • 1970-01-01
    相关资源
    最近更新 更多