Score:0

map nginx $request_uri to port

om flag

I want to create a map between nginx $request_uri and $port I have many location and each location works with its own port. for example I have:

location /path1 {
    proxy_pass 127.0.0.1:10000
}

location /path2 {
    proxy_pass 127.0.0.1:10001
}

I want this:

map $request_uri $port {
  /path1 10000;
  /path2 10001;
}

location $request_uri {
    proxy_pass 127.0.0.1:$port
}

How can I config like this?

Score:0
us flag

I haven't tried this, but this approach should work:

http {
    map $reqest_uri $port {
        ~^/path1 10000;
        ~^/path2 10001;
        default 9000;
    }
}

server {
    location / {
        proxy_pass 127.0.0.1:$port;
    }
}

How it works:

location / catches requests to all paths (unless there are more specific matches). Then nginx resolves the value for $port variable by running it through the map.

In the map, we use a regular expression for prefix matching the URI. Otherwise that match would be exact match, which wouldn't likely work.

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.