【问题标题】:nginx 404 on root/homepage with Django + Gunicorn使用 Django + Gunicorn 在根/主页上的 nginx 404
【发布时间】:2021-03-01 08:51:34
【问题描述】:

我正在将我的网站从一台主机迁移到另一台主机。除了每当我导航到站点的主页时,我都会看到 nginx 的 404 页面,迁移大部分成功率为 99%。每隔一个页面,静态和媒体文件都会正确呈现。我绞尽脑汁想找到解决方案,但我没有找到任何解决方案,更不用说遇到类似问题的人了(相反,其他人的主页可以正常工作,但其他人的主页却是 404)。

我有两个不同的域(一个属于我的公司,另一个属于我们所在的城市)。我已经更新了我拥有的域上的 DNS,以确保它可以与新主机一起使用,并且确实如此。当我使用主机 (example.com) 或服务器的 IP 地址导航时,网站会正确加载所有页面 - 除了主页,主页 - 再次 - 返回 nginx 的 404。所有其他 404 错误显示 404我通过 Django 设置的页面。

每当出现此问题时,gunicorn 错误日志都会添加一行说明该服务是“使用 pid 引导工作人员:...”

nginx.conf --> 保存路径和端口,这个 conf 与我当前主机上运行的 conf 相同

http {

    client_max_body_size 30M;       

    open_file_cache max=1000 inactive=300s;
    open_file_cache_valid 360s;
    open_file_cache_min_uses 2;
    open_file_cache_errors off;

    upstream cgac_server {
    server unix:/root/cgac/cinema_backend/cinema_backend.sock fail_timeout=0;
    }
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen 80 default_server;

        location /static/ {
           expires 365d;
           root /root/cgac/cinema_backend/static/;
        }

        location /media/ {
           expires 365d;
           root /root/cgac/cinema_backend/media/;
        }

        location / {

           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header Host $http_host;
           proxy_redirect off;

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

gunicorn.service --> gunicorn 的设置与活动服务器不同,但此文件中的参数相同,并且可以正常工作。

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=root
Group=www-data
WorkingDirectory=/root/cgac/cinema_backend
ExecStart=/root/cgac-venv/bin/gunicorn --access-logfile /root/logs/gunicorn-access.log --error-logfile /root/logs/gunicorn-error.log --workers 3 --bind unix:/root/cgac/cinema_backend/cinema_backend.sock cinema_backend.wsgi:application

[Install]
WantedBy=multi-user.target

Django URL 模式 --> 也与工作站点 100% 相同

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),  # NOQA
    url(r'^fobi/', include('fobi.urls.view')),
    url(r'^fobi/', include('fobi.urls.edit')),
    url(r'^captcha/', include('captcha.urls')),
    url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
        {'sitemaps': {'cmspages': CMSSitemap}}),
    url(r'^select2/', include('django_select2.urls')),
    url(r'^', include('cms.urls')),
)

【问题讨论】:

  • if (!-f $request_filename) 是干什么用的?
  • @RichardSmith ,我的理解(我实际上并没有自己编写代码)是它检查文件是否存在,如果存在,它将请求传递给上游服务器,即 gunicorn。根据nginx common pitfalls resource,不应该这样做。对于它的价值,如果我将 proxy_pass 行移到 if 语句之外,该站点仍然可以工作。

标签: django nginx gunicorn


【解决方案1】:

有很多方法可以修复它

  1. 将您的上游放在 http {}
  2. 重新加载你的守护进程
  3. 重启你的 gunicorn.service
  4. 您的cinema_backend 必须是/run/cinema_backend.sock(什么是正常的)
  5. 尝试使用不同的配置,例如 site-availabe

【讨论】:

    【解决方案2】:

    该问题与 nginx 或 gunicorn 配置完全无关。事实证明,主页上轮播中的图像不知何故被损坏了,这让 Django 发疯了。我删除了 CMS 中修复问题的条目,然后继续重新上传文件,一切都很好。

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 2016-04-16
      • 2021-02-02
      • 2020-12-29
      • 2014-11-18
      • 2015-11-30
      • 2020-04-06
      • 2018-02-16
      相关资源
      最近更新 更多