Skip to content

Instantly share code, notes, and snippets.

@dayitv89
Last active July 11, 2023 11:09
Show Gist options
  • Save dayitv89/784295b19a9164e57ea2814e48e505bc to your computer and use it in GitHub Desktop.
Save dayitv89/784295b19a9164e57ea2814e48e505bc to your computer and use it in GitHub Desktop.
Host multiple application on same server using nginx reverse proxy

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

Setup host name on your main system as:

[sudo] nano /etc/hosts

add below two lines:

127.0.0.1 first.server.com

127.0.0.1 second.server.com

Pull image and run as:

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

Inside the container run first 5001 and second server at 5002 then start nginx server on port 80

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

Configure Nginx, php, mysql and mywebsql on ubuntu 18.04

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/

For database porting we can use http://mywebsql.net/ or phpmyadmin instead of open port 3306

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name first.server.com;
location / {
proxy_pass http://localhost:5001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
server {
listen 80;
listen [::]:80;
server_name second.server.com;
location / {
proxy_pass http://localhost:5002;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment