nginx proxy_pass 404 error, don't understand why

This

location /api {
    proxy_pass http://backend;
}

Needs to be this

location /api/ {
    proxy_pass http://backend/;
}

By some reason proxy_pass in Nginx cuts header "Host" before passing to upstream, and request catches by default server, and even proxy_header_pass doesn't helps, so I've to explicitly set it:

location / {
    proxy_set_header Host $host;
    proxy_pass  http://backend;
}

Just want to remind others that the slash(backend*/*) after your proxy_pass url is very important!

if the config is

location /api/ {
    proxy_pass http://backend;
}

and you visit http://abc.xyz/api/endpoint, you would be direct to http://backend/api/endpoint;

if the config is

location /api/ {
    proxy_pass http://backend/;
}

and you visit http://abc.xyz/api/endpoint, you would be directed to http://backend/endpoint.

That's the difference.

For more, refer to: Nginx reverse proxy return 404

Tags:

Nginx