0

I am deploying multiple websites to an Ubuntu server equipped with Nginx. I also use the proxy_pass feature to pass the traffic to the service.

The problem is whichever websites is set on Nginx first, all incoming traffics are rerouted to.

I created 3 different .conf files in /etc/nginx/conf and here is the details for each:

  • subdomain1.conf
server {
listen       <the second ip>:80;
server_name  subdomain1.mydomain.com;

location / {
        proxy_pass         http://127.0.0.1:11111;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
  • subdomain2.conf
server {
listen       <the second ip>:80;
server_name  subdomain2.mydomain.com;

location / {
        proxy_pass         http://127.0.0.1:22222;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}
  • subdomain3.conf
server {
listen       <the second ip>:80;
server_name  subdomain3.mydomain.com;

location / {
        proxy_pass         http://127.0.0.1:33333;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

Finally, I reload Nginx using the following command line: sudo nginx -s reload

However, all incoming traffics for other subdomains (subdomain2 and subdomain3) are somehow passed to subdomain1. What I have noted so far is whichever set first, all incoming traffics are bound to it.

Amir H. Bagheri
  • 1,416
  • 1
  • 9
  • 17
  • Can you use `listen 80;` instead of `listen :80;`? – Richard Smith Mar 09 '21 at 07:51
  • It didn't work. Seemingly, I can't have more than 1 "server" defined and I should only use "location" to distinguish requests which is very odd. – Amir H. Bagheri Mar 09 '21 at 22:41
  • So as for solution, I just added static IP addresses – Amir H. Bagheri Mar 09 '21 at 22:42
  • What happens if you put all three server blocks in a single .conf file instead? I have a similar setup with multiple subdomains on the same IP and it works fine. – miknik Mar 11 '21 at 10:55
  • @miknik It would be awesome if you could share it. However, I am setting up a pipeline and it would be very difficult to write a bash script to handle such a circumstance. – Amir H. Bagheri Mar 11 '21 at 19:28
  • My conf file is super long, but the only difference is I have a server block at the start which is for the root domain with no subdomain and in the `listen` directives I dont specify an IP address, just the port. I'm also using SSL for them all. – miknik Mar 12 '21 at 16:04

0 Answers0