在用Nginx转发到另一个域名的时候,发现报错404,转发后端连接都正常,查看日志也没发现问题,后端服务器也是Nginx,最后查看后端Nginx日志发现,转发过来的域名不对,导致后端服务器没解析对。这是Host头错误
,修改为proxy_set_header Host $proxy_host;
就可以了。
具体步骤
- 前端服务器A,域名:a.zengwu.com.cn
- 后端服务器B,域名:b.zengwu.com.cn
现在需要将 a.zengwu.com.cn/api/send
接口调用到 b.zengwu.com.cn/api/send
接口。在服务器A的Nginx配置加入
server {
listen 80;
server_name a.zengwu.com.cn;
location /api/send {
proxy_set_header Host $proxy_host; # 这里是重点,需要使用代理域名proxy_host
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 指定目标服务器地址(含完整文件路径)
proxy_pass http://b.zengwu.com.cn/api/send;
}
# 其他配置
}
评论区