Score:2

Proxy pass on subdirectory not working

qa flag

I have an api running at http://127.0.0.1:9650. For example the following is giving a json object as response:

curl http://127.0.0.1:9650/ext/health

I would like to make the following work at https://example.com/my-node/ext/health.

I have the following nginx configuration:

 location /my-node {
    proxy_pass http://127.0.0.1:9650;
    proxy_http_version 1.1;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  }

but this does not seem to work (I am getting a 404)

I tried adding:

proxy_redirect http://127.0.0.1:9650 /my-node;

but no luck there either. Does anyone have a suggestion how I can solve this?

djdomi avatar
za flag
please share the complete details of `nginx -T -t`
Richard Smith avatar
jp flag
You need to use `location /my-node/ { proxy_pass http://127.0.0.1:9650/; ... }` - notice the `/` at the end of both the `location` and `proxy_pass` statements. See [the proxy_pass directive](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass) for details.
xfscrypt avatar
qa flag
@RichardSmith yes that's it! it is working now. Thank you!. If you add it as an answer I will accept it
Score:2
jp flag

Using proxy_pass without an optional URI (e.g. proxy_pass http://127.0.0.1:9650;), the requested URL is passed upstream unmodified.

So https://example.com/my-node/ext/health is passed upstream to http://127.0.0.1:9650/my-node/ext/health - notice that the "/my-node" part remains intact.


Using proxy_pass with an optional URI within a prefix location will translate the requested URL by substituting the location prefix with the optional URI.

For example:

location /my-node/ {
    proxy_pass http://127.0.0.1:9650/;
    ...
}

In this case, the optional URI is /, so /my-node/ in the requested URL is replaced by / before passing upstream.

So https://example.com/my-node/ext/health is passed upstream to http://127.0.0.1:9650/ext/health - notice that the "/my-node" part has been removed.

See the proxy_pass directive for details.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.