Host multiple application on same server using nginx reverse proxy. This is highly cost saving technique for test servers or low request servers.
Note: Host server OS assume to be Ubuntu 18.04, below all config directory and command will on this linux flavour. You may use the diffent flavour thus config path and cmd may change. However core idea will be the same.
Step 1: As you need a single machine, where you install nginx on port 80.
And run your any language server say we have 4 websites as one in Ruby on Rails, second on nodejs and some are on php and golang. I started all application using their application server on different port as:
Ruby On Rails using Unicon: 3000 as => ror.server.com / rorserver.com
Nodejs using PM2: 3001 => node.server.com / nodeserver.com
Golang using docker: 3002 => go.server.com / goserver.com
php using nginx/apache: 3003 => php.server.com / phpserver.com
Step 2: In real world you will book domains or subdomain and change their A record to your server IP. Because we are running this all on local system(localhost) we need to modify /etc/hosts
as:
127.0.0.1 ror.server.com
127.0.0.1 node.server.com
127.0.0.1 goserver.com
127.0.0.1 phpserver.com
Step 3: So we just need 4 config files as sample above first.server.com
and second.server.com
at /etc/nginx/sites-available/first.server.com
.
Now just create symbolic link of the same file at /etc/nginx/sites-enabled
as ln -s /etc/nginx/sites-available/first.server.com /etc/nginx/sites-enabled
Step 4: Final step just need to start/restart the nginx server as service nginx start
. Voila try ror.server.com, node.server.com, goserver.com, phpserver.com on your machine browser and you can see the 4 diffent application on same port 80 is working fine. No need to expose port 3000-3003 just expose port 80 at server and we are good to go.
Docker image available for same demo at https://hub.docker.com/r/gauravds/nginx-node8-multiserver
[sudo] nano /etc/hosts
127.0.0.1 first.server.com
127.0.0.1 second.server.com
docker run -it -p 80:80 --name nginx-node8-multiserver gauravds/nginx-node8-multiserver:1.0
docker ps
docker exec -it <container_id> /bin/sh
pm2 start /root/first/server.js
pm2 start /root/second/server.js
service nginx start
/ service nginx restart
Ref: https://www.liberiangeek.net/2015/07/how-to-run-multiple-websites-using-nginx-webserver-on-ubuntu-15-04/ https://medium.com/@utkarsh_verma/configure-nginx-as-a-web-server-and-reverse-proxy-for-nodejs-application-on-aws-ubuntu-16-04-server-872922e21d38
https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-ubuntu-18-04 https://www.rosehosting.com/blog/how-to-install-mywebsql-on-ubuntu-18-04/