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.
Set Up IP-based Virtual Hosts in Nginx Ubuntu Cloud Server : Steps
We are taking that, you can install Nginx normally and can run WordPress on Ubuntu. This amount of knowledge is mandatory. Normally install means – one server hosted on one website with one IP.
Suppose your Nginx has the public folder on :
1 | /usr/share/nginx/html |
If two domains are abhishekghosh.pro
and abhishekghosh.net
, then do lik these steps first :
1 2 3 4 5 6 7 | cd /usr/share/nginx/html && ls rm -r * && ls mkdir abhishekghosh.pro abhishekghosh.net chown -R www-data:www-data /usr/share/nginx/html/abhishekghosh.pro chown -R www-data:www-data /usr/share/nginx/html/abhishekghosh.net sudo chmod 755 /usr/share/nginx/html nano /usr/share/nginx/html/abhishekghosh.pro/index.html |
For both the domain’s index.html
files, I need to put a bit different two plain html files :
1 2 3 4 5 6 7 8 | <html> <head> <title>abhishekghosh.pro</title> </head> <body> <h1>Moron: abhishekghosh.pro is a Virtual Host</h1> </body> </html> |
and, second one :
1 | nano /usr/share/nginx/html/abhishekghosh.net/index.html |
1 2 3 4 5 6 7 8 | <html> <head> <title>abhishekghosh.net</title> </head> <body> <h1>Imbecile: abhishekghosh.net is a Virtual 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | nano /etc/nginx/conf.d/default.conf # edit rightly 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 :
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 :
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 :
1 | nano /etc/nginx/nginx.conf |
and play with these :
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.
Real part starts with running the command :
1 | ifconfig |
When one IP is set, you will see the output like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | eth0 Link encap:Ethernet HWaddr 04:01:31:a2:fe:01 inet addr:104.131.164.225 Bcast:104.131.175.255 Mask:255.255.240.0 inet6 addr: fe80::601:31ff:fea2:fe01/64 Scope:Link inet6 addr: 2604:a880:800:10::325:a001/64 Scope:Global UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4033561 errors:0 dropped:0 overruns:0 frame:0 TX packets:4451922 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:807570148 (807.5 MB) TX bytes:1343093157 (1.3 GB) eth1 Link encap:Ethernet HWaddr 04:01:31:a2:fe:02 inet addr:10.132.177.245 Bcast:10.132.255.255 Mask:255.255.0.0 inet6 addr: fe80::601:31ff:fea2:fe02/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:8 errors:0 dropped:0 overruns:0 frame:0 TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:648 (648.0 B) TX bytes:578 (578.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:5748 errors:0 dropped:0 overruns:0 frame:0 TX packets:5748 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:567045 (567.0 KB) TX bytes:567045 (567.0 KB) |
If you edit this file :
1 | nano /etc/hosts |
and entries like :
1 2 3 | 127.0.0.1 localhost 10.0.0.2 abhishekghosh.pro 10.0.0.3 abhishekghosh.net |
and restart :
1 | /etc/init.d/dns-clean start |
Linux will understand what we want to do – matter of subnet mask like HP Cloud.
Secondly, if we edit /etc/nginx/sites-available/default
, we can make it listening to Public IP addresses. Copy the default file first :
1 2 | cp /etc/nginx/sites-available/default /etc/nginx/sites-available/abhishekghosh.net cp /etc/nginx/sites-available/default /etc/nginx/sites-available/abhishekghosh.pro |
Delete all these :
1 2 | rm /etc/nginx/sites-available/default rm /etc/nginx/sites-enabled/default |
Symlink has been removed. We need to again symlink :
1 2 | rm /etc/nginx/sites-available/default rm /etc/nginx/sites-enabled/default |
Then edit each of the files :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | server { listen 10.0.0.3:80; root /usr/share/nginx/html/abhishekghosh.net; index index.php index.html index.htm; server_name abhishekghosh.net; #access_log path/to/access.log; #error_log /path/to/error.log; # Redirect server error pages location / { try_files $uri $uri/ /index.php; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html/abhishekghosh.net; } # PHP scripts to FastCGI location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; try_files $uri =404; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; include fastcgi_params; } } |
You need to edit both the files, then symlink each :
1 2 3 4 | sudo ln -s /etc/nginx/sites-available/abhishekghosh.net /etc/nginx/sites-enabled/abhishekghosh.net # symlink both or all domains after editing them, then nginx -t service nginx restart |
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.
Tagged With set ip based virtual hosts ngnix cloud server , nginx ipbased virtual server