nginx multisite reverproxy using path variable

Viewed 101

I have multisite settings on single instance, I mean using site id, I'm switching content, logo theme colors etc and I have different domain names for each site.

Paths

http://localhost:3000/{{site_id}}/ 
http://localhost:3000/{{site_id}}/about-us/
http://localhost:3000/{{site_id}}/product/1232/slug

etc...

For Example

http://localhost:3000/1 => site-a.local
http://localhost:3000/2 => site-b.local
http://localhost:3000/3 => site-c.local

I'm using nginx, reverse proxy from port 80 to 3000. How can I forward the site id in the nginx

server {

   listen 80;
   listen [::]:80;
   
   server_name site-a.local;


   location ~ /(.*) {
    
        proxy_pass http://localhost:3000/1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    
    }

}


server {

   listen 80;
   listen [::]:80;
   
   server_name site-b.local;


   location ~ /(.*) {
    
        proxy_pass http://localhost:3000/2;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
     
    }

}

The above idea is not working for some reason can someone help me?

1 Answers
upstream node {
  server 127.0.0.1:3000;
  keepalive 32;
}



server {
  server_name site-a.local;
  listen 80;
  location / {
    proxy_pass http://node/1/;
    proxy_set_header Host $host;
  }
}


server {
  server_name site-b.local;
  listen 80;
  location / {
    proxy_pass http://node/2/;
    proxy_set_header Host $host;

  }
}


server {
  listen 3000;
  location /1/ { return 200 "Hello from 1 with URL $uri"; }
  location /2/ { return 200 "Hello from 2 with URL $uri"; }
}

With this you are proxying to your node backend, listening on port 3000.

$:/etc/nginx/conf.d# curl -H "Host: site-a.local" 127.1/test/location/
Hello from a with URL /a/test/location/


$:/etc/nginx/conf.d# curl -H "Host: site-b.local" 127.1/test/location/
Hello from b with URL /b/test/location/

Related