Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Nginx on multiple ports

Tags:

nginx

I am trying to configure nginx on two ports with the same instance, for example on port 80 and port 81, but no luck so far. Here is an example of what I am trying to do:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen  80;
        server_name  chat.local.com;

        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
            proxy_buffering off;

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

    server {
        listen  81;
        server_name  console.local.com;
        location / {
            proxy_pass http://127.0.0.1:8888;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
            proxy_buffering off;
        }
    }
}

When I try to run console.local.com, it shows the content from chat.local.com. Is there a way to make nginx run on two ports? Thanks in advance!

like image 884
Uday Avatar asked Apr 25 '13 06:04

Uday


People also ask

What ports can I use for Nginx?

By default, the Nginx web server listens to all incoming connections on port 80. If you have installed an SSL certificate, then it will listen to all secure connections on port 443.

How many connections can Nginx handle?

NGINX can handle a maximum of 512 concurrent connections. In newer versions, NGINX supports up to 1024 concurrent connections, by default.

Can Nginx have multiple servers?

There is, in theory, no limit to the number of sites that you can host on your VPS with Apache or Nginx.


2 Answers

your config looks ok

I think the problem is this (correct me if I'm wrong):

  • you have console.local.com listening on port 81,
  • that means you need to access it as http://console.local.com:81/
  • when you access it as http://console.local.com/ (no explicit port so defaults to port 80) nginx will check, notice that noting is listening on port 80 for that server_name, and consequently will pass the request to the default server-block. Since the defaut server-block is the first one (in the absence of configuration to change it) you end up in the chat.local.com handling.

In all likelyhood you want to change your console.local.com to listen on port 80 also since:

  • the server_name directive in both serverblocks is enough to differentiate the requests
  • that avoids you having to add the :81 to the domainname in the requests all the time
like image 60
cobaco Avatar answered Oct 20 '22 07:10

cobaco


You can add listen statement 2 times simple; like below
listen 80;
listen 81;

This should work with nginx

like image 41
Kaushal Avatar answered Oct 20 '22 06:10

Kaushal