301跳转死循环的示例分析:
1.nginx配置代码如下:
server{
listen 80;
server yisu.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ^~ /api/ {
rewrite '^/api/(.*)' /$1 break;
proxy_pass http://www.yisu.com;
}
}
2.通过反向代理调用接口:http://yisu.com/api/getUserData
3.问题解析:
调用后端接口时,yisu.com反向代理到www.yisu.com的url,www.yisu.com带请求头"yisu.com",又去请求yisu.com这个域名,造成了死循环。
4.解决方法:
(1)注释掉标记1处proxy_set_header Host $host;的代码
(2)添加标记2处的代码,例如:
server{
listen 80;
server yisu.com;
#标记1-方式1
# proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location ^~ /api/ {
rewrite '^/api/(.*)' /$1 break;
proxy_pass http://www.yisu.com;
#标记2-方式2
proxy_set_header Host "www.yisu.com"; #host配置成后端服务器访问域名
}
}