{"id":1034,"date":"2015-02-27T13:57:03","date_gmt":"2015-02-27T21:57:03","guid":{"rendered":"http:\/\/blog.portnumber53.com\/?p=1034"},"modified":"2015-02-27T13:57:03","modified_gmt":"2015-02-27T21:57:03","slug":"mapping-multiple-domains-to-different-nodejs-ports-using-nginx","status":"publish","type":"post","link":"https:\/\/blog.portnumber53.com\/index.php\/2015\/02\/27\/mapping-multiple-domains-to-different-nodejs-ports-using-nginx\/","title":{"rendered":"Mapping multiple domains to different nodejs ports using nginx"},"content":{"rendered":"<p>\n\tAt the moment of this writing, I only have one VPS, so it wouldn&#39;t take long for me to be running more than 1 node app in it.\n<\/p>\n<p>\n\tSince I want to keep all of them working in this semi-production environment (and in a box I have at home). node cannot be using the same port and I don&#39;t want to keep stop\/restarting an app everytime I&#39;m working on it.\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\tSince I&#39;ve been pretty happy using nginx as a reverse proxy for Apache and a nodejs, then I decided to keep using it and figure how to have multiple nodejs apps running.\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\tMy scenario, (YMMV):\n<\/p>\n<p>\n\t&#8211; I want to have mutiple domains resolving to the same box\n<\/p>\n<p>\n\t&#8211; I don&#39;t want to change the URL for socket.io\n<\/p>\n<p>\n\t&#8211; I know I need to use different ports for different nodejs apps\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\tSo after googling for a while and discovering that <a href=\"http:\/\/wiki.nginx.org\/IfIsEvil\" target=\"_blank\" rel=\"noopener\">IF is evil<\/a>. I stumble upon &quot;map&quot; which let me do exactly what I wanted:\n<\/p>\n<p>\n\tHere&#39;s the relevant sections of nginx.com (<a href=\"https:\/\/www.youtube.com\/watch?v=qEKHuvO22QM\" target=\"_blank\" rel=\"noopener\">the names have been changed to protect the innocents<\/a>)\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<pre>\nhttp {\n.\n.\n.\n    map $http_host $custom_nodejs_port {\n        default 50000;\n        domain1.com   50000;\n        domain2.com   30000;\n    }\n.\n.\n\nserver {\n        location \/socket.io\/ {\n            rewrite ^\/node(.*)$ $1 break;\n            proxy_pass http:\/\/127.0.0.1:$custom_nodejs_port;\n\n            proxy_http_version 1.1;\n            proxy_set_header Upgrade $http_upgrade;\n            proxy_set_header Connection &quot;upgrade&quot;;\n\n            proxy_set_header Host $host;\n        }\n.\n.\n}<\/pre>\n<p>\n\tThis way I can have multiple nodejs applications running in different ports and nginx will map connections to different domains to them.\n<\/p>\n<p>\n\tThe only downside is that I need to reload the config and it&#39;s not automatic. Maybe soon&#8230;..\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;\n<\/p>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At the moment of this writing, I only have one VPS, so it wouldn&#39;t take long for me to be running more than 1 node app in it. Since I want to keep all of them working in this semi-production environment (and in a box I have at home). node&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/blog.portnumber53.com\/index.php\/2015\/02\/27\/mapping-multiple-domains-to-different-nodejs-ports-using-nginx\/\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[230,249,250,258,260],"class_list":["post-1034","post","type-post","status-publish","format-standard","hentry","category-linux","tag-map","tag-multiple-apps","tag-multiple-ports","tag-nginx","tag-nodehs"],"_links":{"self":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/1034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/comments?post=1034"}],"version-history":[{"count":0,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/posts\/1034\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/media?parent=1034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/categories?post=1034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.portnumber53.com\/index.php\/wp-json\/wp\/v2\/tags?post=1034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}