【问题标题】:Nginx - 403 error only on static filesNginx - 仅在静态文件上出现 403 错误
【发布时间】:2019-01-30 21:37:42
【问题描述】:

栈是:Ubuntu // Supervisor // Nginx Gunicorn Django 1.11

静态文件夹:/home/sitebiz/sitebiz/static/

Nginx 配置:/etc/nginx/sites-enabled/site.biz

server {
        listen   80;
        listen   [::]:80;
        access_log off;
        server_name site.biz;
        return 301 https://$server_name$request_uri;
}


server {
    listen              443 ssl;
    #listen              80 ssl;
    server_name         site.biz;
    ssl_certificate     /etc/letsencrypt/live/site.biz/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/site.biz/privkey.pem;
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    #listen 80;

    gzip on;
    access_log /var/log/nginx-access.log;
    error_log /var/log/nginx-error.log;


    location /static {
        root  /home/sitebiz/sitebiz;
        internal;
    }

    location /track {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
        proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8899;
            break;
        }
    }

    location /income {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
        proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8899;
            break;
        }
    }


    location / {
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header GEOIP_COUNTRY_CODE $geoip_country_code;
        proxy_set_header GEOIP_COUNTRY_NAME $geoip_country_name;
        proxy_redirect off;

        if (!-f $request_filename) {
            proxy_pass http://127.0.0.1:8000;
            break;
        }
    }
}        

我尝试将 /home/sitebiz/sitebiz/static/ 目录及其所有内容所有权更改为 sitebiz 用户和 www-data ,但没有任何帮助。 甚至 Django 本身也不能提供静态文件,我不知道为什么。

来自 django 设置:

SITE_ROOT = os.path.abspath(os.path.dirname(name)) 
MEDIA_URL = '/media/' 
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 
STATIC_URL = '/static/' 
STATIC_ROOT = os.path.join(SITE_ROOT, 'static') 

提前谢谢你。

【问题讨论】:

  • root /sitebiz/sitebiz; 这不会导致/home/
  • 对不起,我不知道,为什么它在这里被破坏了。该块是 location /static/ { root /home/sitebiz/sitebiz;内部的; }
  • nginx 错误日志中有条目吗?另外,nginx worker 用户名是否对/home/sitebiz/sitebiz/static/ 上面的所有目录具有读取权限?
  • location /static {} 删除斜线
  • collectstatic 工作了吗?禁止 403 错误。您可能需要 chown 要将静态文件收集到的目录,收集它们,然后 chown 将其返回。此外,如果这确实是问题,Fabric 对这些东西很有帮助:fabfile.org

标签: django nginx ubuntu-16.04 gunicorn nginx-config


【解决方案1】:

我不知道为什么,但 POST 的解决方案可以正常工作

只需在 shell 中执行:

sudo chmod o+x /root

如果有人可以解释这个逻辑 - 为什么根文件夹必须具有执行权限 - 我会非常感激(并且赞成)

【讨论】:

    猜你喜欢
    • 2015-03-13
    • 2018-12-31
    • 2013-12-09
    • 2018-12-05
    • 1970-01-01
    • 2015-04-28
    • 2013-07-19
    • 2019-06-16
    • 2017-09-04
    相关资源
    最近更新 更多