Nginx – How to run multiple Nginx instances on different port

nginx

I would like to have several Nginx instances running on my server on separate ports (for example one user runs Nginx on port 2345, and another user on port 2346). So far, I have been successful in compiling and running the server on their designated ports. However, I am running into a weird issue: If i visit domain1.com:2345 or domain2.com:2346 I get the correct (200) response but if I visit domain1.com or domain2.com (without specifying the port) I get no response at all.

Any help/clues in figuring out how to set this up would be highly appreciated.

Best Answer

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.

Related Topic