NGINX和Gunicorn发生静态媒体故障

问题描述 投票:0回答:1

您好,感谢您抽出宝贵时间阅读此帮助请求。

我正在安装一个名为Netbox的Web应用程序,该应用程序基于Django构建。一个基本的Gunicorn在rather simplie configuration中是NGINX的前端。

我遇到的问题是Web应用程序报告它无法加载任何静态文件,并且我可以验证我是否为这些请求得到404。

我已经确认可以在NGINX路径/static/中引用的/opt/netbox/netbox/static路径中查看正确的文件,并且权限也已正确设置。

由于这是Django Web应用程序,因此我已使用内置的测试Web服务器执行了一个简单的测试,并且所有静态文件均已正确呈现;这几乎肯定是Gunicorn和我的NGINX配置之间的问题。

nginx.conf

server {
    listen 443 ssl;

    # CHANGE THIS TO YOUR SERVER'S NAME
    server_name netbox.example.com;

    ssl_certificate /etc/ssl/certs/netbox.crt;
    ssl_certificate_key /etc/ssl/private/netbox.key;

    client_max_body_size 25m;

    location /static/ {
        alias /opt/netbox/netbox/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    # Redirect HTTP traffic to HTTPS
    listen 80;
    server_name _;
    return 301 https://$host$request_uri;
}

gunicorn.py

bind = '127.0.0.1:8001'
workers = 5
threads = 3
timeout = 120
max_requests = 5000
max_requests_jitter = 500

error message when browsing http://localhost:8001/

我在以下设置上收到了相同的结果:

  • Ubuntu 18.04(Azure)
  • Ubuntu 19.10(本地VM)
  • Ubuntu 20.04(本地VM)
  • Centos 8.1(Azure)
  • 使用Apache备用设置方法时出现相同的错误

我将不胜感激任何可以验证权限或登录日志的想法。

django nginx gunicorn
1个回答
0
投票

尝试使用root代替别名。当位置与指令值的最后一部分匹配时,Nginx documentation建议使用root。我认为是避免在使用别名时在示例中重复$ uri,/ static。

© www.soinside.com 2019 - 2024. All rights reserved.