{"id":973,"date":"2014-12-12T18:07:53","date_gmt":"2014-12-12T18:07:53","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=973"},"modified":"2015-08-16T20:07:46","modified_gmt":"2015-08-16T20:07:46","slug":"how-to-host-multiple-node-js-applications-with-multiple-different-domains-on-one-vps-with-nginx","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/how-to-host-multiple-node-js-applications-with-multiple-different-domains-on-one-vps-with-nginx\/","title":{"rendered":"How to host multiple Node.js applications with multiple different domains on one VPS with Nginx"},"content":{"rendered":"<p>First things first, if you bought your domain elsewhere then you first need to point that domain to your server. You basically have two options here<\/p>\n<ul>\n<li>installing, setting up and running a DNS on your VPS and pointing the DNS (from the control panel where you bought a domain) records to your VPS<\/li>\n<li>setting your DNS Zone file A record (in the control panel where you bought a domain) to the VPS IP<\/li>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/10856266\/setting-dns-on-a-vps\">this posts<\/a> explain what are the pros and cons<\/li>\n<\/ul>\n<p>Now, if you do that for multiple domains they will all point to your server&#8217;s IP and show the same thing, essentially the thing which is running on port 80 (and that, in our main Nginx installation, will be a default Nginx welcome screen). If you have multiple Node.js applications, which are running on different ports, and you want to pinpoint the domains to that particular applications, then this is where the Nginx comes in so that it takes the requests for each domain and routes it\u00a0to an appropriate port where the appropriate Node.js application is running. Basically, what Nginx does in this case is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Reverse_proxy\">reverse proxying<\/a>.<\/p>\n<p>Useful links:<\/p>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/5009324\/node-js-nginx-and-now\">this StackOverflow question<\/a><\/li>\n<li><a href=\"http:\/\/blog.grabeh.net\/Digital-Ocean-VPS-Nginx-Express-apps\">http:\/\/blog.grabeh.net\/Digital-Ocean-VPS-Nginx-Express-apps<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-host-multiple-node-js-applications-on-a-single-vps-with-nginx-forever-and-crontab<\/a><\/li>\n<li><a href=\"http:\/\/ludovf.net\/blog\/configure-nginx-to-proxy-virtual-hosts-to-apache\/\">http:\/\/ludovf.net\/blog\/configure-nginx-to-proxy-virtual-hosts-to-apache\/<\/a><\/li>\n<li><a href=\"http:\/\/kbeezie.com\/apache-with-nginx\/\">http:\/\/kbeezie.com\/apache-with-nginx\/<\/a><\/li>\n<li><a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-nginx-virtual-hosts-server-blocks-on-centos-6\">https:\/\/www.digitalocean.com\/community\/tutorials\/how-to-set-up-nginx-virtual-hosts-server-blocks-on-centos-6<\/a><\/li>\n<\/ul>\n<p>Use forever:<\/p>\n<ul>\n<li><strong>npm install forever -g<\/strong><\/li>\n<li>forever start &#8211;spinSleepTime 10000 yourApp.js<\/li>\n<li>if the app crashes, forever starts it up again<\/li>\n<\/ul>\n<p>Lets start:<\/p>\n<ul>\n<li><strong>sudo vim \/etc\/nginx\/conf.d\/example.com.conf<\/strong>:\n<pre class=\"lang:default decode:true\">server {\r\n    listen 80;\r\n\r\n    server_name your-domain.com;\r\n\r\n    location \/ {\r\n        proxy_pass http:\/\/localhost:{YOUR_PORT};\r\n        proxy_http_version 1.1;\r\n        proxy_set_header Upgrade $http_upgrade;\r\n        proxy_set_header Connection 'upgrade';\r\n        proxy_set_header Host $host;\r\n        proxy_cache_bypass $http_upgrade;\r\n    }\r\n}<\/pre>\n<\/li>\n<li>to reference <strong>multiple<\/strong> domains for <strong>one<\/strong> Node.js app (like www.example.com and example.com) you need to add the following code to the file <strong>\/etc\/nginx\/nginx.conf<\/strong> in the http section:\u00a0<strong>server_names_hash_bucket_size 64;<\/strong><\/li>\n<li>but, as it always is, the upper statement didn&#8217;t work for me, so I ended up using <a href=\"http:\/\/stackoverflow.com\/questions\/7947030\/nginx-no-www-to-www-and-www-to-no-www\">this solution<\/a> from StackOverflow:\n<pre class=\"lang:default decode:true\">server {\r\n    #listen 80 is default\r\n    server_name www.example.com;\r\n    return 301 $scheme:\/\/example.com$request_uri;\r\n}<\/pre>\n<\/li>\n<li><strong>sudo service nginx restart<\/strong><\/li>\n<\/ul>\n<p>Forever keeps your application running when it crashes but if VPS is rebooted it won&#8217;t start anymore. Simple cronjob\u00a0solves this issue.\u00a0Create <strong>starter.sh<\/strong> in your application&#8217;s home folder and copy the following code:<\/p>\n<pre class=\"lang:default decode:true\">#!\/bin\/sh\r\n\r\nif [ $(ps -e -o uid,cmd | grep $UID | grep node | grep -v grep | wc -l | tr -s \"\\n\") -eq 0 ]\r\nthen\r\nexport PATH=\/usr\/local\/bin:$PATH\r\nforever start --sourceDir \/path\/to\/your\/node\/app main.js &gt;&gt; \/path\/to\/log.txt 2&gt;&amp;1\r\nfi<\/pre>\n<p>where main.js is\u00a0your application&#8217;s main script. Add to crontab the following line:\u00a0<strong>@reboot \/path\/to\/starter.sh<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First things first, if you bought your domain elsewhere then you first need to point that domain to your server. You basically have two options here installing, setting&hellip;<\/p>\n","protected":false},"author":1,"featured_media":974,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,38],"tags":[],"class_list":["post-973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nodejs","category-servers"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=973"}],"version-history":[{"count":1,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions"}],"predecessor-version":[{"id":976,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/973\/revisions\/976"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/974"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}