Is it possible to make Nginx listen to different ports?
235,823
Solution 1
Yes, it is.
What you probably want is multiple "server" stanzas, each with a different port, but possibly (probably?) the same server_name, serving the "different" content appropriately within each one, maybe with a different document root in each server.
Full documentation is here: http://nginx.org/en/docs/http/server_names.html
Example:
server {
listen 80;
server_name example.org www.example.org;
root /var/www/port80/
}
server {
listen 81;
server_name example.org www.example.org;
root /var/www/port81/
}
Solution 2
You can also do the following:
server {
listen 80;
listen 8000;
server_name example.org;
root /var/www/;
}
Related videos on Youtube
Author by
billcyz
Updated on September 18, 2022Comments
-
billcyz almost 2 years
I created one Nginx with one Linux Azure VM, is it possible to make nginx listen to different ports so that when I change the port number, the content would be different. I found there would be a collision if I created two or more ports related to HTTP on VM. Can anyone help me with that?
-
Matt Fletcher over 8 yearsIt's annoying that one can't do
listen 80 81;
isn't it? Oh well... -
PaulMest over 7 years@MattFletcher It is possible to do something similar, see Felix's comment: serverfault.com/a/755791/308219
-
Aaron Sofaer almost 6 yearsThis is great. In particular, being able to change the server config in only one server context seems far superior than risking changing one and leaving the other unchanged...
-
Jeremie almost 6 yearsThis does not answer the question asked by @billcyz. He asked for different ports and different content. This answer gives different ports with the same content.
-
cedbeu over 5 years@Krishnendu: "much more convenient"? more convenient than what? This doesn't answer the OP question. In that case, both :80 and :8000 point to same content. OP asked explicitly "when I change the port number, the content would be different". So, post from Craig Miskell answers much better to the question than this one
-
Krishnendu over 5 years@cedbeu: Yes you are right in context of the question asked Craig Miskell answer is more appropriate. I was looking for the other solution where I need to serve same content on multiple port, this was the exactly what I needed. Again for given question asked other ans is more appropriate, Sorry if I mislead some one, It was a honest mistake from my side.
-
cedbeu over 5 years@Krishnendu no problem :) everything clarified now
-
Brunis over 4 yearsThe specific question was to serve different content on different ports, this answer does not achieve that.
-
Muhammad over 4 yearsI have to do this with IP address instead of domain name, I have created 2 files in
sites-available
but it is not working in my case. -
Geoffrey McCosker about 4 yearsis "server_name" required?
-
Crupeng almost 3 years/var/www/port80 would mean example.com/port80 right?
-
Craig Miskell almost 3 yearsNo, with the configuration above the content in /var/www/port80 would be at example.org (no path suffix, implied port 80). The content in /var/www/port81 would be at example.org:81
-
Brunis almost 3 years@MattFletcher you can have multiple listen directives in a server declaration, so listen 80; listen 81;