If you have multiple websites that do not have high traffic or that are not used often, why have a server for each one when you can have them all on a single server and reduce your costs. Yes, I am talking about having goodwebsite.com and betterwebksite.com be hosted on a single server. All you need to do this is basic linux knowledge. We will achieve this by using nginx reverse proxy, so let’s begin.

To follow this tutorial, you will need a server and two or more applications that you will want to deploy and have mapped to domains or subdomains. I personally use DigitalOcean to host my server. It is a low cost, high performance hosting solution. To create a DigitalOcean account use this link to get 100 dollars of free credits to use in the first 2 months.

Nginx reverse proxy

For this tutorial we will use Nginx. Nginx is a open source tool that started as a web server. Later became much more, but aspect that we are interesting is that it is also a reverse proxy. We will also assume that you will have two applications running on two development ports, so not 80 but for example 3000 and 3001.

Process that I will describe here is called nginx reverse proxy and how this works is that, first, nginx will listen to port 80. Nginx will know from which domain request is coming from, eg. when request comes from goodwebsite.com, nginx will have this information and be able to act accordingly. We will add mapping for domains to local ports. Using this process we can add as many websites as we want and save on our monthly bill. As a developer we have more freedom to not waste resources and to develop and deploy multiple application or POCs live.

So, you rented droplet on DigitalOcean. Next thing you need to do is to access server by using SSH. After that you will have to install and run nginx by using:

sudo apt install nginx
sudo services nginx start

You can now deploy your applications and run them on development ports eg. 3000 and 3001. Now we will use nginx concept of available sites and enabled sites to achieve our forwarding. When file is added to sites available and system link is created to enable this file/site. When we do this, nginx can see our forwarding configuration and perform proper redirections. We will see this in example, let’s assume we are making farukljuca.com and blog.farukljuca.com redirections, but we can do this also with two top level domains.

cd /etc/nginx/sites-available
touch farukljuca.com
nano farukljuca.com

Add next text to file, change name of your service and local port to match your configuration:

server {
	listen 0.0.0.0:80
	server_name farukljuca.com
	access_log /var/log/nginx/farukljuca.com.log
	location / {
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header HOST $http_host;
		proxy_set_header X-NginX-Proxy true;
		proxy_pass http://127.0.0.1:3000;
		proxy_redirect off;
	}
}

Close nano editor by holding Ctrl and pressing X. After that press letter Y to save changes to file. And finally add link to your nginx configuration from sites available to sites enabled directory by running command:

sudo ln -s /etc/nginx/sites-available/farukljuca.com /etc/nginx/sites-enabled/farukljuca.com

We can now just restart nginx and our service will be up and running. As a result, we are now forwarding traffic from farukljuca.com to local service started on port 3000.

sudo service nginx restart

Multiple websites

For our other service, our blog for example, we can repeat same principle:

cd /etc/nginx/sites-available
touch blog.farukljuca.com
nano blog.farukljuca.com

server {
	listen 0.0.0.0:80
	server_name blog.farukljuca.com
	access_log /var/log/nginx/blog.farukljuca.com.log
	location / {
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header HOST $http_host;
		proxy_set_header X-NginX-Proxy true;
		proxy_pass http://127.0.0.1:3001;
		proxy_redirect off;
	}
}

sudo ln -s /etc/nginx/sites-available/blog.farukljuca.com /etc/nginx/sites-enabled/blog.farukljuca.com

sudo service nginx restart

Now after similar steps we have our blog running as a separate application. We can repeat this as much as we can but we should keep in mind that resources of our server are not unlimited and keep their usage at a healthy level.

Conclusion

In order to save money and use resources wisely you can host multiple low traffic websites on a single server. Process is very easy and anyone with basic linux skills can do it. This can enable you to make and deploy multiple POC websites, learning applications, blogs for multiple people and let you not worry in the process about high server bill.

Happy coding fellow developer!


0 Comments

Leave a Reply

Your email address will not be published.