0

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.

1 Answers1

0

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.

drookie
  • 8,625
  • 1
  • 19
  • 29