Nginx in Docker with two networks, two websites, from two external IP’s on the host. I’m stuck

  docker, ip, nginx

Hoping for some guidance. I’ve Googled but seem to be in no luck. I’ve found the following, but these don’t appear to be the same issue:

I have a VPS with two external IP addresses, and a domain address pointed to each:

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

I have Docker on this VPS and Nginx running within it.

I want to specify Nginx to render the correct website depending on the domain that the user arrived from.

In order to do this (and it doesn’t work so I’m either completely off track or missing a piece), I have done the following:

docker-compose.yml

version: "3"

services:
  nginx:
    image: nginx:latest
    container_name: nginx
    volumes:
      - ~/nginx/conf:/etc/nginx/conf.d
    networks:
      - domain1net
      - domain2net
    ports
      - "80:80"
      - "443:443"

networks:
  domain1net:
    external: true
  domain2net:
    external: true

My domain1 network is 172.18.0.0/16, and domain2 is 172.19.0.0/16.

This said, I’m unsure that these are actually mapped to the external IP’s?

Nginx default.conf

server {
  listen 172.18.0.2:80;
  server_name: domain1;
  location / {
    root ...
  }
}

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

Right now, whichever external IP address I go to, I'm seeing the first website (which right now is just the default 'welcome to nginx!' page.

Can someone help me understand how to map the two external IP addresses?

Regards,

Andy

Source: Docker Questions

LEAVE A COMMENT