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:
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
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.
Thanks! Why not put all domain.conf files in sites-enabled, instead of sites-available? Is the change of folders not largely unnecessary?
Hi sir, how about serving a static folder via nginx using express js? because x.x.x.x:3000/defaults does not work. And I manage it to work but with like this x.x.x.x/defaults, my problem is what happen if I set it up for multiple node js app on one server? Thanks
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.