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?

up vote 65 down vote accepted

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/
}
  • Good Explantion – billcyz Dec 28 '14 at 9:00
  • 8
    It's annoying that one can't do listen 80 81; isn't it? Oh well... – Matt Fletcher Nov 27 '15 at 13:41
  • @MattFletcher It is possible to do something similar, see Felix's comment: serverfault.com/a/755791/308219 – PaulMest Nov 1 '16 at 18:36

You can also do the following:

server {
    listen 80;
    listen 8000;
    server_name example.org;
    root /var/www/;
}
  • Much more convenient. thanks for the help. – Krishnendu Jan 16 '17 at 13:50
  • 1
    This 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... – Aaron Sofaer Jul 2 at 22:56
  • 3
    This 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. – Isaac Sutherland Aug 24 at 3:26
  • @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 – cedbeu Oct 3 at 6:56
  • @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. – Krishnendu Oct 4 at 9:05

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.