Image for post
Image for post

How to Setup Server Blocks (Virtual Hosts) with Nginx on Digital Ocean

Save money by hosting multiple websites on one server. Note: This tutorial assumes that you have a nodejs droplet (server) and the DNS setup.

Code

#Server block 1
server {
root /var/www/domain1.com/public;
server_name domain1.com;
index index.html;
client_max_body_size 256M;
access_log logs/domain1.access.log main;
location / {
try_files $uri $uri/ uri.html =404;
}
}
#Server block 2
server {
root /var/www/domain2.com/public;
server_name domain2.com;
index index.html;
client_max_body_size 256M;
access_log logs/domain2.access.log main;
location / {
try_files $uri $uri/ uri.html =404;
}
}

What I Am Using

sudo apt-get update
sudo apt-get install nginx

Sample Webpage

const express = require('express');
const app = express();
const port = 4002;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, ()=> {
console.log(`Example app listening at http://localhost:${port}`);
});
const express = require('express');
const app = express();
const path = require('path');
port = 4002;
app.use(express.static('public'));app.listen(port, ()=> console.log('app listening on port ${port}'));
domain1
- app.js
- package.json
- public

The Server Block

How to Create Server Blocks to Create Multiple Websites

Written by

Leading technological advancement community for food and resources.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store