我正在尝试重定向来自最初在 localhost:5000 上运行的旧 API 站点的流量。我已在 api.example.com 设置了反向代理,但现在 api.example.com 的 SSL 证书已过期。我们不再使用 *.example.com;相反,我们使用 *.example2.com,它已经拥有有效的 SSL 证书。还有其他网站在此域上运行。
我的目标是将流量从 api.example.com 重定向到 api.example2.com。然而,尽管配置了 nginx 服务器块,我还是遇到了 404 错误。当我用curl-I检查时,它显示了301永久移动响应
以下是该站点的 nginx 配置:
server {
listen 80;
listen [::]:80;
server_name api.example.com;
# Redirect to api.example2.com
return 301 https://api.example2.com$request_uri;
}
server {
listen 443 ssl;
server_name api.example.com;
ssl_certificate /etc/nginx/ssl/example2.com.crt;
ssl_certificate_key /etc/nginx/ssl/example2.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_pass http://localhost:5000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
我需要帮助来解决这个问题。谢谢你的帮助
确保 SSL 证书 (example2.com.crt) 和相应的密钥 (example2.key) 与新域 (api.example2.com) 匹配。如果您更改了域,则为新域使用旧证书可能会导致 SSL 错误。
更新第二个服务器块中的 server_name 以反映新域:
server {
listen 443 ssl;
server_name api.example2.com; # Update to the new domain
ssl_certificate /etc/nginx/ssl/example2.com.crt;
ssl_certificate_key /etc/nginx/ssl/example2.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
# ... (rest of the configuration remains the same)
}
确保 api.example2.com 的 DNS 记录正确指向运行 Nginx 的服务器。另外,请留出一些时间来传播 DNS 更改。
更改 Nginx 配置后,重新启动 Nginx 以应用更改:
sudo systemctl restart nginx
有时,浏览器会缓存重定向。清除浏览器缓存或尝试在隐身/私密浏览窗口中访问该网站,以避免缓存的重定向。
使用curl测试重定向并检查标头:
curl -I https://api.example.com
确保您收到包含正确新位置的 301 响应。
通过解决这些问题,您应该能够解决 404 错误并成功将流量从 api.example.com 重定向到 api.example2.com。如果问题仍然存在,请仔细查看 Nginx 错误日志 (/var/log/nginx/error.log) 中是否有任何特定错误消息,这些消息可能会提供有关问题的更多见解。