【发布时间】: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 语句之外,该站点仍然可以工作。