Skip to content
Advertisement

Setting up nginx with multiple IPs

I have my nginx configuration file under /etc/nginx/sites-available/ with two upstreams say

upstream test1 {
server 1.1.1.1:50;
server 1.1.1.2:50;
}

upstream test2 {
server 2.2.2.1:60;
server 2.2.2.2:60;
}

server {
location / {
proxy_pass http://test1;
}
location / {
proxy_pass http://test2;
}
}

Sending a curl request to <PrimaryIP>:80 works but I want to use <SecondaryIP1>:80 for test1 and <SecondaryIP2>:80 for test2. Is it possible to define this in nginx?

Advertisement

Answer

You have to have two server directives to accomplish this task:

upstream test1 {
     server 1.1.1.1:50;
     server 1.1.1.2:50;
}

upstream test2 {
     server 2.2.2.1:60;
     server 2.2.2.2:60;
 }

 server {
      listen 80
      server_name <SecondartIP1>
      location / {
          proxy_pass http://test1;
       }
 }
server {
     listen 80
     server_name <SecondarIP2>
     location / {
          proxy_pass http://test2;
      }
 }
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement