Score:0

Nginx rewrite with proxypass

pk flag

I have an Nginx running in the front of a node js server (next.js). I'm trying to write my Nginx config in order to achieve this behavior, I want to add the hostname as the first part in the path before proxy_pass to node js.

for example, the client will write a.com/ or a.com/product/... or a.com/**.
my nextjs application except for something like http://a.com/[:domainname]/.... while the domain name is the same as the host.

so the goal is to change the URL from a.com/** to a.com/a.com/**, before passing that to nextjs server.

the nginx config i created :

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                # if ($host = a.com) { proxy_pass http://site/a/; }
                # if ($host = b.com) { proxy_pass http://site/b/; }
                rewrite ^(/.*)$ /$host/$1 ;
                proxy_pass http://site;
        }

but that didn't work.

Score:0
za flag

Looks valid, you should check what's the upstream is getting in it's access log. Bet the answer is there. Though I suppose the main issue is that you forgot to pass the Host header:

proxy_set_header Host <whatever the Host is expected on the upstream, definitely not $host though>

But this would be the clue only if the upstream server isn't the default or only one.

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.