How to proxy the full url at nginx config?
Solution 1:
nginx documentation explains the behavior:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
In your case, it means that the request URI is replaced by the /api/v1/
URI specified in your proxy_pass
directive.
If you want the URI to be passed as-is, then you need to do:
location /api/v1/ {
proxy_pass http://frontend-api.preprod.example.com;
}
In this case, nginx uses the URI from the original request with the proxy_pass
destination, as described by:
If proxy_pass is specified without a URI, the request URI is passed to the server in the same form as sent by a client when the original request is processed, or the full normalized request URI is passed when processing the changed URI:
If you want to replace part of the request URI with different part, you need to use regular expression capture:
location ~ ^/source/path/(.*) {
proxy_pass http://example.com/destination/path/$1;
}
Edit:
To get the domain name of the original request passed to the upstream, one needs to set the Host
HTTP header for the upstream request:
proxy_set_header Host $host;
It is often useful to pass also the IP address of the visitor via header:
proxy_set_header X-Forwarded-For $remote_addr;