【问题标题】:Static Media Failure with NGINX and GunicornNGINX 和 Gunicorn 的静态媒体故障
【发布时间】:2020-09-18 00:13:14
【问题描述】:

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

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

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

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

由于这是一个 Django 网络应用程序,我使用内置测试网络服务器执行了一个简单的测试,并且所有静态文件都正确呈现;这几乎可以肯定是 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(本地虚拟机)
  • Ubuntu 20.04(本地虚拟机)
  • Centos 8.1 (Azure)
  • 使用 Apache 替代设置方法时出现相同错误

如果有任何想法可以验证权限或检查日志等内容,我将不胜感激。

【问题讨论】:

    标签: django nginx gunicorn


    【解决方案1】:

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

    所以试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-21
      • 2021-05-12
      • 2012-11-20
      • 2017-11-25
      • 2015-05-08
      • 2012-05-11
      • 1970-01-01
      • 2021-01-02
      相关资源
      最近更新 更多