Nginx / Django:访问静态文件导致403 Forbidden

时间:2013-02-11 12:19:21

标签: django nginx

我一直在尝试调试这几个小时,我不确定还有什么要检查。我的问题是Nginx没有服务器Django静态文件。访问静态文件会导致错误 403 Forbidden

nginx错误日志的确切错误是:

2013/02/11 05:42:13 [error] 22526#0: *29 open() "/home/mydomain/public_html/test2/src/bootstrap.css" failed (13: Permission denied), client: XXX.XXX.XX.XX, server: mydomain.com, request: "GET /src/bootstrap.css HTTP/1.1", host: "www.mydomain.com"

这是我的 nginx配置文件

server {
    listen   XX.XX.X.XXX:80;
    server_name mydomain.com;
    root /home/mydomain/public_html/test2/app;
    # serve directly - analogous for static/staticfiles
    location /media/ {
        # if asset versioning is used
        if ($query_string) {
            expires max;
        }
    }
    location /admin/media/ {
        # this changes depending on your python version
        root /home/mydomain/public_html/test2/lib/python2.7/site-packages/django/contrib;
    } 
    location /src/ { 
        autoindex    on;   
        root /home/mydomain/public_html/test2;
    }     
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off; 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

静态文件存储在/home/mydomain/public_html/test2/src

我已经尝试chown mydomain.mydomain -R *chmod 755 /home/mydomain -R *而没有任何效果。

3 个答案:

答案 0 :(得分:2)

使用此

顺便说一句。 IfIsEvil

server {
    listen 80;
    server_name mydomain.com;

    #access_log /var/log/nginx/x_access.log;
    #error_log /var/log/nginx/x_error.log;

    location /static {
        alias /path/to/your/static;
    }

    location /media {
        alias /path/to/your/media;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off; 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    }
}

答案 1 :(得分:2)

这是我最初问题的工作解决方案

server {
    listen   XX.XX.X.XXX:80;
    server_name mydomain.com;
    root /home/mydomain/public_html/test2/app;

    location /admin/media/ {
        # this changes depending on your python version
        root /home/mydomain/public_html/test2/lib/python2.7/site-packages/django/contrib;
    }
    location /src {
        root /home/mydomain/public_html/test2;
    }
    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://localhost:8000/;
    } 
    # what to serve if upstream is not available or crashes
    error_page 500 502 503 504 /media/50x.html;
}

答案 2 :(得分:0)

另一种方法是使用try_files。这样做的好处是Nginx将首先查找要提供的真实文件,如果找不到它,它会将执行传递给您的django应用程序。这非常适合提供动态的sitemap.xml,例如,因为您不需要在nginx.conf中对该文件进行特殊处理。

# Set default expires headers (used for static assets)
expires 30d;

server {
  listen 80;
  server_name mydomain.com;

  root /some/path/assets/;
  try_files $uri @django;

  location @django {
    expires -1d;
    proxy_set_header  Host $host;
    proxy_set_header  X-Real-IP $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect    off;
    proxy_pass        http://unix:/some/path/server.sock;
  }

  location /static/admin/ {
    alias /some/path/lib/python2.7/site-packages/django/contrib/admin/static/admin/;
  }
}