nginx 配置反向代理是url问题,主要有几种情况

1. proxy_pass 最后没有加/

location /api {
    proxy_pass http://example.com;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
}

如果访问地址http://localhost/api 实际上会代理到http://example.com/api

2. proxy_pass 最后有加/

location /api {
    proxy_pass http://example.com/;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
}

如果访问地址http://localhost/api 实际上会代理到http://example.com/

3. proxy_pass 最后有加路径并且没有/

location /api {
    proxy_pass http://example.com/store;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
}

如果访问地址http://localhost/api 实际上会代理到http://example.com/storeapi

4. proxy_pass 最后有加路径并且没有/

location /api {
    proxy_pass http://example.com/store/;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;
}

如果访问地址http://localhost/api/test.html 实际上会代理到http://example.com/store