Score:0

nginx proxy_pass of different subdomains to different locations within one server block

br flag
aag

The typical way of reverse-proxying different subdomains to different places with Nginx is to install a unique server for each subdomain, like this:

server {
    server_name subdomain1.example.com;
    location / {
        proxy_pass       http://hostname1:port1;
    }
}
server {
    server_name subdomain2.example.com;
    location / {
        proxy_pass       http://hostname2:port2;
    }
}

Is it possible to achieve the same result within one single server block (e.g. server_name .example.com, without any specified subdomain), by specifying different locations within that server block?

Score:1
us flag

Yes, it is possible.

First, set up a map to map domains into proxy_pass destinations:

map $host $dest {
    www1.example.com 192.168.10.10:8001;
    www2.example.com 192.168.10.11:8002;
    default 192.168.10.12;
}

server {
    server_name *.example.com;

    proxy_pass http://$dest;
}

When nginx receives a request, it goes to proxy_pass directive. Then it resolves $dest using the map, which maps different virtual host names to destinations. Then nginx proxies the request using the resolved destination.

Remember to set up the default destination properly. Every public web server receives requests for all kinds of domain names. Typically you want to return 404 on unknown virtual hosts.

aag avatar
br flag
aag
update. It seems that proxy_pass to a variable requires an explicit rewrite rule. I am playing around with such rules but I haven't figured it out yet.
us flag
Did you try just using different destination URL in the `map`? `icinga.example.com 10.10.10.7/icingaweb2` as the `map` line? Also, you need to set up the base URL properly in each application configuration.
aag avatar
br flag
aag
if `icinga.example.com` is mapped to `10.10.10.7/icingaweb2`, the client is sent to the non-existing page `https://icinga.example.com/icingaweb2/authentication/login`
aag avatar
br flag
aag
Dear @tero, I have to come back to this. Your solution works for simple 1:1 transfers. However, e.g. `https://icinga.example.com` should be proxy_passed to `http://10.10.10.7/icingaweb2`, whereas `https://mail.example.com` should go to `http://10.10.10.9/iredmail/mail` etc. I seem to understand that I need to define location blocks, but those won't work with subdomains. Do you have further advice?
us flag
Are you sure you have set the root URL of `icinga` application correctly?
Score:1
jp flag

If you specify different locations then it won't be the same result as you have the same location / in two server blocks.

aag avatar
br flag
aag
thank you for taking the time to respond. I may have expressed myself ambiguously. The goal is to reverse-proxy various subdomains, each one of which will be served by a different internal server. Rather than installing a number of individual nginx servers, which makes the configuration unwieldy, I was wondering whether I can specify a single "wildcard server" which will then direct the requests to the correct internal servers depending on the subdomains requested.
jp flag
You can use a variable as `proxy_pass` parameter See https://stackoverflow.com/questions/5743609/dynamic-proxy-pass-to-var-with-nginx-1-0
aag avatar
br flag
aag
I now did use a variable, but it turns out that I then need to explicitly declare a rewrite rule. I have yet to figure out how to do that correctly.
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.