Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serve two web applications behind an nginx reverse proxy

I have two web applications (node.js express apps), web1 and web2. These web apps expect to be hosted on sites that are typically something like http://www.web1.com and http://www.web2.com. I'd like to host them behind an nginx reverse proxy as https://www.example.com/web1 and https://www.example.com/web2. I do not want to expose the two web apps as two subdomains on example.com.

Here is a snippet of my nginx configuration (without SSL termination details) that I had hoped would accomplish 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;
  }
}

This works, except for the relative links that the web apps use. So web app web1 will have a relative link like /js/script.js which won't be handled correctly.

What is the best/standard way to accomplish this?

like image 767
Erik Avatar asked Jan 08 '16 19:01

Erik


People also ask

When you configure Nginx as a reverse proxy for Apache both may listen to the same port?

You do that by configuring NGINX as a reverse proxy for Apache. With this setup, NGINX will listen for all incoming requests to port 80 and pass them on to Apache, which is listening in on port 8080.

Can you run Nginx and Apache at the same time?

Configuring Apache and NginxApache and Nginx can definitely run simultaneously. The default config will not allow them to start at the same time because they will both try to listen on the same port and the same IP.


1 Answers

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; } } }

like image 112
bdn02 Avatar answered Sep 23 '22 10:09

bdn02