Score:0

Nginx - transparently redirect traffic into another server block

cn flag

I have a NGINX configuration with two virtual hosts (server {} blocks): one serving a human-readable site on the primary domain, and the other acting as a reverse-proxy for a specialized service on a subdomain.

http {
    <...>

    server {
        listen 443 ssl http2;
        server my.tld;
        <...>

        location / {
            root /srv/http;
            <...>
        }
    }

    server {
        listen 443 ssl http2;
        server svc.my.tld;

        <complex reverse-proxy setup with many location blocks>
    }
}

For some reason, some clients of the specialized service send their requests to the primary domain (i. e. I see my.tld/api/endpoint instead of svc.my.tld/api/endpoint in the logs). I cannot fix these clients. They also don't follow redirects.

Is there any way to transparently redirect those requests into the correct server {} block, without duplicating the whole reverse-proxy configuration in the main server block?

Lex Li avatar
vn flag
If it's that clear `/api/endpoint` is the pattern for those clients, then you can add a `location` block in the `my.tld` `server` block to reverse proxy such requests to `svc.my.tld` (and as you wished, not to clone things from within `svc.my.tld` `server` block).
jp flag
You can use `include` directive to avoid code duplication.
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.