Getting Nginx to serve to two websites (mapped to two external IP addresses)

I'm trying to set up Nginx (in Docker) on a VPS. The VPS has two external IP addresses, and I have two domains.

  • domain1 - 212.x.x.149
  • domain2 - 89.x.x.60

I can't seem to work out how to write a conf file that accommodates this.

server {
  listen localhost:80;
  server_name domain1;
  location / {
    proxy_pass http://...:8080;
  }

server {
  listen localhost:80;
  server_name domain2;
  location / {
    proxy_pass http://...:3000;
  }

Doesn't work as it complains of a duplicate listen on localhost.

server {
  listen 212.x.x.149:80;
  server_name domain1;
  location / {
    proxy_pass http://...:8080;
  }

server {
  listen 89.x.x.60:80;
  server_name domain2;
  location / {
    proxy_pass http://...:3000;
  }

doesn't work as it says it can't bind to 212.x.x.149.

Both addresses are set up in /etc/hosts.

Any guidance would be appreciated.

Regards,

Andy

Edit: Just to note that I have tried adding 'net.ipv4.ip_nonlocal_bind = 1' to my /etc/sysctl.conf which didn't help.

Per request, here is the output from ip a (I know enough about networks to know to blank stuff out, but not enough to know which need blanking and which don't, so sorry if I've gone a bit overboard here): ip a

Error given when listening for the two different IP addresses:

2021/02/08 01:55:35 [emerg] 1#1: bind() to 212.x.x.149:80 failed (99: Cannot assign requested address)

nginx: [emerg] bind() to 212.x.x.149:80 failed (99: Cannot assign requested address)

If I switch the order around, it simply tells me the same except referencing IP 89.x.x.60.


With nginx you can specify multiple listening sections without specifying the IP address:

server {
  listen 80;
  server_name domain1;
}

server {
  listen 80;
  server_name domain2;
}

Then nginx would route the traffic to the upper or lower server section depending on the destination hostname (aka server_name) only.