http://example.com/app/page?location /app/ {
proxy_pass http://backend/;
}When proxy_pass URL ends with a slash, nginx replaces the matching location prefix with the path in proxy_pass. Here, /app/ is replaced by /, so /app/page becomes /page on the backend.
http://example.com/app/page?location /app/ {
proxy_pass http://backend;
}When proxy_pass URL does not end with a slash, nginx appends the full original request URI to the backend URL. So /app/page is appended to http://backend, resulting in http://backend/app/page.
/service/ to http://backend/ so that /service/api becomes http://backend/service/api. Which proxy_pass directive achieves this?Option D uses proxy_pass http://backend; without a trailing slash, so nginx appends the full original URI /service/api to backend, resulting in http://backend/service/api.
Option D replaces /service/ with /, so the backend URL becomes http://backend/api, which is not desired.
Options C and D add /service again, causing duplication.
/app/page. Why?location /app/ {
proxy_pass http://backend/app/;
}When proxy_pass has a URI part with trailing slash, nginx replaces the matching location prefix with that URI part. Here, location is /app/ and proxy_pass is http://backend/app/. So /app/page becomes /app/page after replacement, but nginx appends the rest of the URI after the matched prefix, causing duplication: /app/app/page.
This causes backend to receive a wrong path, leading to 404.
$http_host contains the original Host header from the client request. Setting proxy_set_header Host $http_host; preserves it when forwarding.
$host is the hostname of the server handling the request, which may differ.
$proxy_host is the hostname of the proxy server, not the client.
Setting Host to a fixed string like 'backend' breaks Host preservation.