Score:0

Nginx rewrites correctly for one but not the other

fr flag

Basically, I am trying to use the proxy_pass directive to call a remote API.

So far, this is what I got:

server {
  location /a {
    proxy_pass https://a.com;
    rewrite ^/a(.*)$ $1 break; # no trailing slash, defined in application code
  }
  location /b {
    proxy_pass https://b.com;
    rewrite ^/b(.*)$ $1 break; # no trailing slash, defined in application code
  }
  location / {
    # Rest of configuration
  }
}

I am stuck with the fact that location /a works fine but location /b doesn't for some reason (HTTP/404).


I tried using a trailing slash for location /b this way

location /b/ {
  proxy_pass https://b.com/;
  rewrite ^/b/(.*)$ $1 break;
}

but this doesn't work either.

Any help is very welcome.

Score:0
fr flag

I found the answer to my particular issue.

The two API servers are not configured the same way and I had to tweek the nginx config a bit.

  • Server b.com needed a proxy_set_header Host $host directive and no rewrite directive
  • Server a.com needed the rewrite directive but not the proxy_set_header Host $host

This leaves me with the following (working for me) config:

server {
    location /a {
        proxy_pass  https://a.com;
        rewrite ^/a(.*)$ $1 break;
    }
    location /b {
        proxy_set_header Host $host;
        proxy_pass  https://b.com;
    }
}
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.