NGINX: How to setup multiple port in one server or

2019-03-29 06:24发布

问题:

Hi Guys I am new to nginx,. I am having trouble with my setup, I want my server to run with multiple port on public.

For Ex:

server {
  listen 443 ssl;
  server_name <https - mydomainname>;
  ssl_certificate <location cert>;
  ssl_certificate_key <location key>;
    location /tags.txt {
      add_header 'Access-Control-Allow-Origin' '*';
    }
}

From the above setup I am now able to access perfectly. But what if I have http://localhost:6006 and http://localhost:5005 multiple ports in my localhost and I want to publish it. I tried to access it using this https - mydomainname : port 6006 and https - mydomainname : port 5005 but it fails.

Should I make a setup for another port? Like for port 6006

server {
 listen 6006 ssl;
 server_name <https - mydomainname>;
 ssl_certificate <location cert>;
 ssl_certificate_key <location key>;
  location /tags.txt {
    add_header 'Access-Control-Allow-Origin' '*';
    proxy_pass http://localhost:6006;
  }
}

and port 5005

server {
 listen 5005 ssl;
 server_name <https - mydomainname>;
 ssl_certificate <location cert>;
 ssl_certificate_key <location key>;
  location /tags.txt {
    add_header 'Access-Control-Allow-Origin' '*';
    proxy_pass http://localhost:5005;
  }
}

I have no idea how to fix this. Any help is very appreciated thanks.

回答1:

You can have multiple listen directives per server:

server {
 listen 5005 ssl;
 listen 6006 ssl;
 server_name <https - mydomainname>;
 ssl_certificate <location cert>;
 ssl_certificate_key <location key>;
  location /tags.txt {
    add_header 'Access-Control-Allow-Origin' '*';
  }
}


回答2:

I just faced the same issue.

I duplicated the server_part in the config and so that I could reach the site via a secondary fqdn too.

server {
 listen 5005 ssl;
 listen 6006 ssl;
 server_name <https - mydomainname>;
 server_name <https - mydomainname>;
 ssl_certificate <location cert>;
 ssl_certificate_key <location key>;
  location /tags.txt {
    add_header 'Access-Control-Allow-Origin' '*';
  }
}

.



标签: nginx