Set Up IP-based Virtual Hosts in Nginx Ubuntu Cloud Server

Advertisement

Here are simple steps to set up IP-based Virtual Hosts in Nginx running Ubuntu. IP-based Virtual Hosts will help to use two IP addresses on one server. Notice the title carefully – Set Up IP-based Virtual Hosts in Nginx Ubuntu Cloud Server. This is not exactly what we might do on a dedicated server. Dedicated costs a huge – in thousands of USD per month. Run a Blue Pill-Red Pill test to detect if you are running within a Virtualized OS Instance and fooled as Dedicated. However, if routers, hub, switch etc. are not different, it is not possible to detect; whether the server is really dedicated. Settings on a Dedicated demands more knowledge on UNIX and manpower in real life.

We talked about Virtual Hosts before in the linked article. Virtual Hosts can be – Name-Based, IP-Based, Port-Based etceteras. What you normally read in this and that websites are Name-Based Virtual Hosts.

Set Up IP-based Virtual Hosts in Nginx Ubuntu Cloud Server : Basics

We talked about Virtual Hosts before in the linked article. Virtual Hosts can be – Name-Based, IP-Based, Port-Based etceteras. What you normally read in this and that websites are Name-Based Virtual Hosts. At the time of writing this article, we have two IP address 15.125.77.166 and 15.125.77.27. Our setup is complex, this website has 5K posts right now, plus there is podcast and other stuffs. On a multi-tenant environment, we have lot of servers behind one domain. These are called – nodes. It is needed to distribute the load in a short sentence.

IP-based Virtual Hosts is exactly the opposite – one server but multiple websites. Now, these websites will also have multiple IPs. This is great for smaller websites, like my personal website dr.abhishekghosh.net and abhishekghosh.pro. One 01 GB server is enough to host them. But the hosting provider must have the facility to add allocate floating IPs. There will be an extra charge for this extra IP.

With different IP, it is easier to install SSL certificate, Google bots can not understand whether there is one server or multiple servers, cost reduction etc.

If two domains are abhishekghosh.pro and abhishekghosh.net, then do lik these steps first :

Vim

1

2

3

4

5

6

7

cd/usr/share/nginx/html&&ls

rm-r*&&ls

mkdir abhishekghosh.pro abhishekghosh.net

chown-Rwww-data:www-data/usr/share/nginx/html/abhishekghosh.pro

chown-Rwww-data:www-data/usr/share/nginx/html/abhishekghosh.net

sudo chmod755/usr/share/nginx/html

nano/usr/share/nginx/html/abhishekghosh.pro/index.html

Advertisement

---

For both the domain’s index.html files, I need to put a bit different two plain html files :

Vim

1

2

3

4

5

6

7

8

<html>

<head>

<title>abhishekghosh.pro</title>

</head>

<body>

<h1>Moron:abhishekghosh.pro isaVirtual Host</h1>

</body>

</html>

and, second one :

Vim

1

nano/usr/share/nginx/html/abhishekghosh.net/index.html

Vim

1

2

3

4

5

6

7

8

<html>

<head>

<title>abhishekghosh.net</title>

</head>

<body>

<h1>Imbecile:abhishekghosh.net isaVirtual Host</h1>

</body>

</html>

Moron and Imbecile will help me to really understand whether the Virtual Hosts are properly set.

Read, but do not do these steps until we are telling you to do. This is called Nginx Server blocks :

Vim

1

2

3

4

5

6

7

8

9

10

11

12

13

14

nano/etc/nginx/conf.d/default.conf

#editrightly

server{

server_name abhishekghosh.pro;

#create server log later and uncomment

#access_log logs/abhishekghosh-pro.access.log main;

root/usr/share/nginx/html/abhishekghosh.pro;

}

server{

server_name abhishekghosh.net;

#create server log later and uncomment

#access_log logs/abhishekghosh-net.access.log main;

root/usr/share/nginx/html/abhishekghosh.net;

}

Now reload Nginx :

Vim

1

nginx-t&&service nginx reload

Up to this part, things are exactly like Name-based Virtual Hosts in Nginx. Yes, we could also edit this :

Vim

1

/etc/nginx/sites-available/default

and create two different settings, symlink to make essentially Name-based Virtual Hosts working in Nginx. To make it more easy to understand, if you edit this file :

Vim

1

nano/etc/nginx/nginx.conf

and play with these :

Vim

1

2

3

4

5

include/etc/nginx/conf.d/*.conf;

include/etc/nginx/sites-enabled/*;

##or

include/etc/nginx/conf.d/*.conf;

#include/etc/nginx/sites-enabled/*;

Rinse, Lather, Repeat.

You will understand the things are not rocket science, it is various peoples who wrote guides in their own used ways without informing that, other methods exist. This is kind of experts’ bias – doctors also do not offer too much alternative treatment options. Instead of editing /etc/nginx/conf.d/default.conf, we will edit /etc/nginx/sites-available/default and resume your doing from this steps. We asked you to stop, now start to do again.

I provided examples with 10.0.0.2, 10.0.0.3; depending on the Cloud Computing software, you have to replace it with Public IP either in both files or in one file () only. Yes, it is not difficult but very painful. After doing twice or thrice, if you are new, you’ll get used. Errors are due to improper permission, conflicting settings etc. cPanel like things are for that reason – hosting hundreds of domains with one IP plus enabling to assign one domain with a different IP in a scripted manner.

Always remember, best way to check header is curl -> curl -I abhishekghosh.net. Browser can fool some few seconds. There are gross things like – pointing the domain using a DNS server towards proper IP.

Now, there are complicated factors like firewall (for the main nodes of webhost), suppose if OpenStack not supported IP-based Virtual Hosts, then nothing could be done! Ask your web host first, because it is not very easy from networking point of view. Steps get used and become easy.