2009-09-24 120 views
20

我在Ubuntu Server 9.04上运行Django。Nginx不提供静态

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(调试偏执狂)。当我取消注释时,注释掉的块不起作用。

+1

属于对serverfault.com – ChristopheD 2009-09-24 22:13:03

+0

srry,我不明白你的意思 – DataGreed 2009-09-27 19:33:55

回答

56

你的目录设置如何? /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指令的文档。

+1

+1打我给它。 :-) – 2009-09-24 22:13:04

+0

不,没有“静态”目录。谢谢,我会尝试别名。 – DataGreed 2009-09-27 19:34:42

+1

别名工作!太好了,非常感谢:) – DataGreed 2009-09-27 19:40:20

1

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

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