Separate website with same domain name by adding /organizationx path at the url

3/11/2020

I have 2 containers websites that listen to app1.com/ and app2.com/ and they can have the same endpoints.

I want to create an Nginx proxy to separate organizations:

  • http://<proxy-ip>/organisation1 to listen from here app1.com/
  • http://<proxy-ip>/organisation2 to listen from here app2.com/

Users should see http://<proxy-ip>/organisation1/movies and all css/js to be forwarded to http://<proxy-ip>/organisation1/css/a.css

The location in app is app1.com/movies and app1.com/css/a.css etc.

The problem is that rewrite method doesn't forward the /organisationX.

How to add and forward this to URL if the location doesn't exist in the real app?

server {
   listen 80;
   location /organisation1 {
       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;
       proxy_set_header   X-Forwarded-Host $host/organisation1;
       proxy_pass         http:app1.com

   }
   location /organisation2 {
       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;
       proxy_set_header   X-Forwarded-Host $host/organisation2;
       proxy_pass         http:app2.com
   }
}
-- seyit94267
docker
kubernetes
nginx
nginx-location
nginx-reverse-proxy

1 Answer

3/12/2020

ngx_http_rewrite_module will help you

See manual:

If the specified regular expression matches a request URI, URI is changed as specified in the _replacement_ string.

Try this code:

server {
   listen 80;
   location /organisation1 {
       rewrite ^/organisation1/(.*) /$1 break;       
       ...
       proxy_pass         http:app1.com

   }
   location /organisation2 {
       rewrite ^/organisation2/(.*) /$1 break;       
       ...
       proxy_pass         http:app2.com
   }
}
-- Yasen
Source: StackOverflow