How to run multiple Nginx instances on different port

Okay, I found a solution.

What I did was run a 'master' server, as root, on port 80. From there I add server blocks for each server on their ports with a proxy_pass directive to the server.

  server {
    listen      80;
    charset     utf-8;
    server_name domain1.com;
    location / {
        proxy_pass http://domain1.com:2345;
  }
  server {
    listen      80;
    charset     utf-8;
    server_name domain2.com;
    location / {
        proxy_pass http://domain2.com:2346;
  }

Perhaps there is a more elegant solution but this seems to work well.


Webbrowsers automatically choose port 80 if you don't specify a port. From your explanation it seems like none of the two instances listen on port 80.

What you might want to do is instead serve both domain1.com and domain2.com from the same nginx instance using virtual hosts.


If it is possible for you to have multiple ip addresses for your server you can run several nginx instances on port 80 given that you specify different ip-addresses in the listen directive.

An example:

On the first instance:

server {
  listen a.b.c.e:80
  ...
}

On the other instance

server {
  listen f.g.h.i:80
  ...
}

However this will become a pain if you have more than just a couple of users. Also multiple public ip-addresses might be hard to come by.