0

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?

Patrick Mevzek
  • 9,921
  • 7
  • 32
  • 43
  • 1
    Check the error log of nginx or post it here. – Meiram Chuzhenbayev Jun 27 '18 at 04:57
  • Sorry for the late reply. Ok , so I checked the error log and I've found this error: **(Permission denied) while connecting to upstream:[nginx]** . So this lead me to SELinux and I fixed the error with this command: **setsebool -P httpd_can_network_connect 1** . Thanks for your reply :) – Raul Stepan Jul 01 '18 at 17:31

0 Answers0