I am having trouble configuring Nginx to redirect to two different web applications on CentOS 7.
I have 2 web applications running on Node.js.
First app A running on port 1000 has domain www.domainA.example
, application B running on port 2000 has domain www.domainB.example
.
Both domains have separate SSL certificates.
Since I have one dedicated IP, I need to redirect traffic to one application or another based on what host is accessed through browser.
This is my nginx configuration file:
server {
listen 80;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name *.domainA.example domainA.example;
ssl_certificate /path/to/certificateA.pem;
ssl_certificate_key /path/to/certificateA.key;
ssl on;
location / {
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_pass http://127.0.0.1:1000;
proxy_redirect http://127.0.0.1:1000 https://domain1.com;
}
}
server {
listen 443;
server_name *.domainB.example domainB.example;
ssl_certificate /path/to/certificateB.crt;
ssl_certificate_key /path/to/certificateB.key;
ssl on;
location / {
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_pass http://127.0.0.1:2000;
proxy_redirect http://127.0.0.1:2000 https://domainB.com;
}
}
When I try to connect in browser to www.domainA.example
it just gave me a 502 Bad Gateway Nginx page. Altough when I access www.domainB.example
it gets redirected to https://www.domainA.example
but still an error is shown (unable to connect; can't establish a connection to the server).
I also tried a different nginx configuration running app B on port 443. This way the B application works, but the A app also redirects to application B, even if I set the proxy_pass to the correct port.
Any hints on what could I do to fix this issue?