- sudo apt-get update
- sudo apt-get install nginx
- systemctl status nginx
- ls /etc/nginx/
- vi /etc/nginx/nginx.conf (just to see all config)
- sudo vi /etc/nginx/sites-available/default
upstream project {
server 192.168.31.102:3000;
server 192.168.31.103:3000;
server 192.168.31.104:3000;
}
server {
listen 80;
location / {
proxy_pass http://project;
}
}
- sudo systemctl reload nginx
Box2(192.168.31.102), Box3(192.168.31.103), Box4(192.168.31.104): Install node.js in all box and build app using express generator:
- sudo apt-get update
- sudo apt-get install build-essential libssl-dev
- curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh -o install_nvm.sh
- vi install_nvm.sh
- bash install_nvm.sh
- source ~/.profile
NVM Commands:
nvm ls-remote
nvm install 10.16.0
nvm use 10.16.0
nvm list
nvm alias default 10.16.0
nvm use default
- sudo apt-get install apache2-utils
- sudo vi /etc/nginx/sites-available/default (comment out 2 server to check the load in 1 server only)
- sudo systemctl reload nginx
- ab -c 40 -n 1000 http://192.168.31.101/ (took 13 sec to finish)
- sudo vi /etc/nginx/sites-available/default (revert the changes and uncomment all to check load in all the server)
- sudo systemctl reload nginx
- ab -c 40 -n 1000 http://192.168.31.101/ (took 8 sec to finish)
- sudo vi /etc/nginx/sites-available/default
upstream node_cluster {
server 192.168.31.102:3000; # Node.js instance 1
server 192.168.31.103:3000; # Node.js instance 2
server 192.168.31.104:3000; # Node.js instance 3
}
server {
listen 80;
server_name mynodedomain.dev www.mynodedomain.dev;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://node_cluster/;
proxy_redirect off;
}
location ~* \.(css|js|gif|jpe?g|png)$ {
expires 168h;
}
}
- sudo systemctl reload nginx
- ab -c 40 -n 1000 http://192.168.31.101/stylesheets/style.css (took 0.215 sec to finish)
Note: All four boxes are running on ubuntu machine. I install nginx on 192.168.31.101 and all 3 node.js app is running on 192.168.31.102, 192.168.31.103 and 192.168.31.104 machine resp. Port is same for all 3 node app i.e 3000. After configuration we can see it in action from box1(192.168.31.101).