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.mlszssj.com;
}
}
2.通過反向代理調用接口:http://yisu.com/api/getUserData
3.問題解析:
調用后端接口時,yisu.com反向代理到www.mlszssj.com的url,www.mlszssj.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.mlszssj.com;
#標記2-方式2
proxy_set_header Host "www.mlszssj.com"; #host配置成后端服務器訪問域名
}
}