Score:0

How to reverse proxy host paths to absolute urls using ngix?

tw flag

I have a series of local services all running on different ports. I'd like to expose these services via a nginx reverse proxy. The routing patterns should follow the pattern:

  • 0.0.0.0/app -> locahost:3000
  • 0.0.0.0/api -> locahost:3001
  • 0.0.0.0/db -> locahost:3002

I've tried the following:

http {
 
    sendfile on;

    upstream app {
      server localhost:3000;
    }

    upstream api {
      server localhost:3001;
    }

    upstream db {
      server localhost:3002;
    }
 
    server {
      listen 80;
      server_name 0.0.0.0

      location / {
        proxy_pass http://app
      }

      location /api {
        proxy_pass http://api
      }

      location /db {
        proxy_pass http://db
      }
    }
}

The main issue I'm encountering at the moment is that the location is appended to the final path resulting in:

  • 0.0.0.0/db -> localhost:3002/db

I'm guessing I could use a rewrite rule, but I'm having trouble getting it right.

Score:0
in flag

You are missing the / at the end.

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

From the documentation:

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

The same for the others.

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.