代理地址不加斜杠

location定位的路径设置,是否带斜杠,没有关系。

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/api/getInfo
location ^~/api/ {
  proxy_pass http://127.0.0.1:8000;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际指向为:http://127.0.0.1:8000/api/getInfo
location ^~/api {
  proxy_pass http://127.0.0.1:8000;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

代理地址 + 斜杠

如果代理地址加了斜杠,不管location是否加斜杠,api路径都省略了。

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/getInfo
location ^~/api/ {
  proxy_pass http://127.0.0.1:8000/;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000//getInfo
location ^~/api {
  proxy_pass http://127.0.0.1:8000/;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

代理地址 + 后缀

如果location带斜杠,会省略url中的斜杠。

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/wxgetInfo
location ^~/api/ {
  proxy_pass http://127.0.0.1:8000/wx;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/wx/getInfo
location ^~/api {
  proxy_pass http://127.0.0.1:8000/wx;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

代理地址 + 后缀 + 斜杠

如果location带斜杠,会省略url中的斜杠。

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/wx/getInfo
location ^~/api/ {
  proxy_pass http://127.0.0.1:8000/wx/;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}

# 请求路径为:http://127.0.0.1:8080/api/getInfo
# 实际代理为:http://127.0.0.1:8000/wx/getInfo
location ^~/api {
  proxy_pass http://127.0.0.1:8000/wx/;
  proxy_set_header Host $http_host; 
  proxy_set_header X-Real-IP $remote_addr; 
}