How to serve two web applications behind an nginx reverse proxy How to serve two web applications behind an nginx reverse proxy express express

How to serve two web applications behind an nginx reverse proxy


I think something like this:server { listen 443; server_name .example.com; location /web1 { proxy_pass http://www.web1.com:80; } location /web2 { proxy_pass http://www.web2.com:80; } location / { if ($http_referer ~* (/web1) ) { proxy_pass http://www.web1.com:80; } if ($http_referer ~* (/web2) ) { proxy_pass http://www.web2.com:80; } }}


You should be able to do this by checking the $http_referer, something like:

location / {  if ($http_referer ~ ^http://(www.)?example.com/web1) {    proxy_pass http://www.web1.com:80;  }  if ($http_referer ~ ^http://(www.)?example.com/web2) {    proxy_pass http://www.web2.com:80;  }}

The browser would be setting the referer to http://example.com/web1/some/page when it requests /js/script.js so the apps shouldn't need to change, unless they need to process or care about the referer internally.

The $http_referer does not seem to be easy to find in nginx docs, but is mentioned in a few sites:


How about using cookie and ngx_http_map_module?

Add add_header Set-Cookie "site=web1;Path=/;Domain=.example.com;"; to location /web1 {...} (web2 too).

Add map to under http

map $cookie_site $site {  default http://www.web1.com:80;  "web2" http://www.web2.com:80;}

Default location is this

location / {    proxy_pass $site;}

You can pass the value of cookie to proxy_pass directly. But, using map is more secure way.