1

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.

5
  • Please post the output of ip a. – Michael Hampton Feb 8 at 0:57
  • Added as an image (IP addresses blocked out), but needs admin approval before it will show. – Andy Sawyer Feb 8 at 1:19
  • OK, what is the error you got from nginx? That bit seems to be missing. – Michael Hampton Feb 8 at 1:21
  • Done. It's the same error regardless of whether I have net.ipv4.ip_nonlocal_bind = 1 set or not. – Andy Sawyer Feb 8 at 2:01
  • Are you trying to run nginx inside a docker container? Unless you are using host networking for docker, the external IPs won't be available inside the docker container and you need to use port mapping when starting docker container with nginx. – AlexD Feb 8 at 10:14
1

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.

0

I think this is more to do with Docker than it is Nginx. So no need to follow up. I'll post the answer if I find it, else I'll reframe the question around docker support.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.