The Docker & Nginx Guide to Secure Multi-Domain Hosting


Introduction

Everyone loves websites, be it simpler and one page or having huge content.

Websites can be hosted on any type of server from shared hosting to dedicated hosting. The server is the hardware that holds your website. When any user visits your site, those user requests are handled by the server. In a way, servers are a very powerful piece of hardware tools.

Table of Contents

Types of Hosting

  1. Shared Hosting
  2. Dedicated Hosting
  3. Virtual private server hosting
  4. Cloud hosting
  5. Managed hosting

Each hosting type are having various pros and cons. Which kind of hosting you require is based on the requirement you have for your website. For example, a small informative site of your local store business would run smoothly on shared hosting with limited resources. If you want to control everything in your server along with security, operating system, you can go for dedicated hosting.

The Goal: Multi-Site Hosting

The one topic to focus on in this article is to host multiple sites on one dedicated server. Here assuming you are the owner of multiple sites, or you want to launch multiple sites then what approach can be easier and manageable.

How Web Requests Work

Before moving, one should have a clear understanding of how web requests work in general:

  • The user enters the site URL in the address bar of the browser or clicks on the link to open.
  • The request goes through ISP (Internet service provider) to DNS (Domain Name Server).
  • DNS server maps the URL to the assigned IP address of the Server.
  • DNS forwards the request to the identified server.
  • The server processes the request and executes the code.
  • Returned response is redirected back to the browser where the request was originated. Typical User interaction flow with site

Moving forward to hosting multiple sites on the same server below diagram can be visualized.

  • Three users enter 3 different site URLs in the address bar of the browser or click on the link to open.
  • The request goes through ISP (Internet service provider) to DNS (Domain Name Server).
  • DNS server maps all URLs to the assigned IP address of the Server.
  • DNS serve finds the same IP address for all site requests.
  • DNS forwards the request to the identified IP of the server.
  • All servers process the request and execute the code.
  • Returned response is redirected back to the browser where the request was originated.

Let’s see the below graph: Multiple Site Hosting flow

An appropriate way is to create virtual hosts for each domain so your one server hosting can handle multiple sites. With my work experience, I found that just doing virtual hosts in the parent OS can cause trouble later on.

The Problem with Traditional Virtual Hosts

Troubles I face while serving multiple sites in the same parent OS as below:

  • I can not host multiple sites with the requirement of different versions.
  • Since OS can support one version at a time, I needed to find another hosting.
  • Even if multiple sites are using the same version of the platform, upgrading it can be very risky.
  • One issue can lead to taking down all sites hosted on the same server.

The Docker Solution

To handle such a case I found a very good solution using Docker. Docker is a virtualization platform that provides containers. Each container works and operates separately which gives freedom to host multiple sites with a different version of the same platform at the same time. In Simpler words, Docker helps to create blocks in the parent OS of the server, which we can utilize as a separate server and can install or remove any blocks at any time we want. YaY!!!

Step-by-Step: Setting Up Docker Containers

Now, since you have got the basic idea, let’s get started configuring multiple sites in the same hosting server. Here, I’m assuming that you have already installed docker in your hosting server. If not, you can find out step by step guide on the official site https://docs.docker.com/engine/install/.

In this example, I will show you with creating two sites: A personal site and a demo site. Let’s say the IP of my hosting server is: 1.2.3.4

docker container run -it --name my-blog -d -p 8081:80 ubuntu

In the above command, I’m installing ubuntu’s latest version of the new container. This new container’s name is my-blog, which exposes port 8081. so, the site will be accessible on http://1.2.3.4:8081

docker container run -it --name family-store -d -p 8082:80 ubuntu

I created another container that hosts the site for my family store. This container’s name is family-store, which exposes on port 8082. So, the site will be accessible on http://1.2.3.4:8082

And my requirement is, my blog should be accessible on http://my-blog.com and my family store should be accessible on http://family-store.com After creating docker contains for both sites it will look like the below image:

Docker Structure

Configuring Nginx as a Reverse Proxy

Now, I’m going to install Nginx on the parent OS of my hosting server. You can follow online guides to setup Nginx server in your parent OS: https://www.nginx.com/resources/wiki/start/topics/tutorials/install/

The next step is to configure Nginx for reverse proxy.

nano /etc/nginx/sites-available/my-site.com

Enter the below data in the file:

server {
    listen 80;
    server_name my-site.com;
    location / {
        proxy_pass http://localhost:8081;
    }
}
ln -s /etc/nginx/sites-available/my-site.com /etc/nginx/sites-enabled
nginx -t
service nginx restart

The same steps apply to the family-store sites as well.

server {
    listen 80;
    server_name family-store.com;
    location / {
        proxy_pass http://localhost:8082;
    }
}
ln -s /etc/nginx/sites-available/familiy-store.com /etc/nginx/sites-enabled
nginx -t
service nginx restart

Also, in your domain name server from where you have purchased domain names, you would need to add the ns server path in configuration from your account. Once done it will point to both different servers while executing both domain names.

Conclusion

When you have multiple projects and some chances that its core version might get updated, then avoid hosting multiple sites on the same server. Instead of better handling it with docker. Docker makes it easier to manage each project. You can handle execution very easily without affecting other sites on the server.