Next.js Discord

Discord Forum

NGINX serve many servers

Unanswered
Ninhache posted this in #help-forum
Open in Discord
Hi

I'm trying to serve many servers with my nginx :
/ -> 127.0.0.1:3000 (My nextJS server)
/foo -> 127.0.0.1:3001 (random http server we don't really care)

But I'm facing a problem, when I try to access /foo, I'm ALWAYS redirected on / and then into /foo.. but through nextjs server, and then get a 404, because my server doesnt handle /maze

Here's a part of the config, may anyone can help me to solve the issue :
...
server {
listen  80;
server_name  _;

  location  /foo {
    proxy_pass  http://127.0.0.1:3001;
  }

  location / {
    proxy_pass  http://127.0.0.1:3000;
  }
}

7 Replies

I assume its mostly because your next server is taking over the route management of all the routes and subroutes

Not sure if this will work but try something like this:
https://serverfault.com/a/792769
@Ninhache did this work?
nah i've just retry and it didnt, may I'm missing something tho
also, that's for subdomains and I was looking for "not subdomains".. (i've adapted the code but didnt work)
I've make it work but that's problematic af aha

many of what I want to serve are (basically) static file.. with many css file :

Imagine a project FOO with index.html, style.css..
index.html would ask css with that path css/style.css (or something similar)..

but the reverse proxy won't be able to find the style.css until I provide the route for the project (for example /foo.. to retrieve css as /foo/css/style.css)

Anywayys
server {
    server_name _;
    location ~ ^/foo {
        rewrite ^/maze(.*) /$1 break;
        proxy_pass http://127.0.0.1:3001;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'Upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
    location ~  {
        proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'Upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
    }
}

basically the conf. looks like this, idk why it wasnt working before
anyways