Setting up multiple nodejs applications using nginx vitual hosts

May 28, 2017 720 views
Nginx Node.js Ubuntu

I have followed following tutorial to create virtual hosts using nginx and its working perfectly fine.
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04
I have followed separate tutorial to setup nodejs application and it is also working fine.
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04

Now I want to set up multiple nodejs webapps using virtual hosts and I have following questions:

  1. virtual hosts are displaying static content located as location "/var/www/example.com/html/index.html" How to I change this to nodejs. I am running nodejs app using pm2, Can I use the same or I need to change it to something like 'forever' tool
  2. Do I need to use different ports for my nodejs applications or evenif I use same ports I will work ?
1 Answer

@conquistadorjd

A very basic server block for that handles incoming requests and proxies them to your NodeJS app would look something like this (note: don't use this one, read on down for proxy_params and further setup info):

server
{
    listen 80;
    listen [::]:80;
    server_name domain01.com www.domain01.com;

    location /
    {
        proxy_pass http://127.0.0.1:3626;
    }
}

What the above does is accepts a request on port 80 and proxies it to 127.0.0.1:3626.

To ensure that the above works as it should, you'd make sure that your NodeJS application runs on the IP 127.0.0.1 (which is localhost) and that the port it binds to is 3626 (just an example).

Now, if you wanted to replicate this multiple times over for multiple domains and applications, you'd essentially copy and paste, then make changes to the domain and port for that app.

Ideally, you'd isolate each server block to its own file, i.e.:

/etc/nginx/sites-available/domain01.conf
/etc/nginx/sites-available/domain02.conf
/etc/nginx/sites-available/domain03.conf
/etc/nginx/sites-available/domain04.conf

etc.

I would also recommend opening /etc/nginx/nginx.conf and changing the line that reads either:

include sites-enabled/*;

or

include /etc/nginx/sites-enabled/*;

to read:

include /etc/nginx/sites-available/*;

The reason for this is because if you don't, you'll need to symlink every file from

/sites-available

to

/sites-enabled

... which is largely unnecessary.

...

I would also recommend creating a new file for your proxy configuration that won't change. To do that, run:

nano /etc/nginx/proxy_params

and within it, paste in:

        proxy_buffers 16 32k;
        proxy_buffer_size 64k;
        proxy_busy_buffers_size 128k;
        proxy_cache_bypass $http_pragma $http_authorization;
        proxy_connect_timeout 59s;
        proxy_hide_header X-Powered-By;
        proxy_http_version 1.1;
        proxy_ignore_headers Cache-Control Expires;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
        proxy_no_cache $http_pragma $http_authorization;
        proxy_pass_header Set-Cookie;
        proxy_read_timeout 600;
        proxy_redirect off;
        proxy_send_timeout 600;
        proxy_temp_file_write_size 64k;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Cookie $http_cookie;
        proxy_set_header Host $host;
        proxy_set_header Proxy '';
        proxy_set_header Referer $http_referer;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Original-Request $request_uri;

Now, you'd include this file below the proxy_pass directive for each site you want to proxy.

Going back to the above and setting up 4x domains/apps, we'd use something such as:

/etc/nginx/sites-available/domain01.conf

server
{
    listen 80;
    listen [::]:80;
    server_name domain01.com www.domain01.com;

    location /
    {
        proxy_pass http://127.0.0.1:3626;
        include /etc/nginx/proxy_params;
    }
}

/etc/nginx/sites-available/domain02.conf

server
{
    listen 80;
    listen [::]:80;
    server_name domain02.com www.domain02.com;

    location /
    {
        proxy_pass http://127.0.0.1:3627;
        include /etc/nginx/proxy_params;
    }
}

/etc/nginx/sites-available/domain03.conf

server
{
    listen 80;
    listen [::]:80;
    server_name domain03.com www.domain03.com;

    location /
    {
        proxy_pass http://127.0.0.1:3628;
        include /etc/nginx/proxy_params;
    }
}

/etc/nginx/sites-available/domain04.conf

server
{
    listen 80;
    listen [::]:80;
    server_name domain04.com www.domain04.com;

    location /
    {
        proxy_pass http://127.0.0.1:3629;
        include /etc/nginx/proxy_params;
    }
}

As you can see, in these examples, all I did was change the domain and increment the port by 1. All you need to do with NodeJS is make sure the application is running and that PORT:IP corresponds to what you set in the server block.

You can definitely use PM2 -- there's a few different ones out there, though PM2 is probably one of the most popular and it's what I see most people using.

Have another answer? Share your knowledge.