Search This Blog

Thursday, March 22, 2012

NGINX: Slicehost Articles: How to serve multiple domains

Slicehost Articles: How to serve multiple domains: "^/(.*) http://"


I am often asked by people how to use their Slice to serve multiple domains.
The question often surprises me as they may have setup their Slice, installed a web server (Apache, Nginx, etc) and even created a virtual host to serve their main domain.
Don't get me wrong, the question itself is good (I like questions as it makes me feel useful!), but the answer is always so simple:
Create another vhost.


There may not be a great deal I can add to the answer but let me outline the process of setting up a Slice and creating a virtual host (I won't go into any details of the installation and creation process - please see the relevant articles for detailed help).
When your Slice is first created it is a minimal Linux install (it doesn't matter what OS you choose).
You SSH into the Slice and update and secure it.
Then you install your preferred web server (Apache, Nginx, Litespeed, etc).
Then the detailed stuff begins. It doesn't matter if your site is PHP based or Rails based or something else entirely. You install the language and framework basics (say Ruby and rubygems or mod_php and so on).
Once that is all done, you come to the part that allows you to server your site: creating virtual hosts.


Greatly simplified the procedure for serving a website is as follows:
A browser send a request to your Slice IP asking for the contents of '' (your domain name).
Your web server jumps into action and says 'yes! I have something for you'. The web server does its 'thing' and serves up an http representation of your site which is sent to the browser.
The browser then translates the http and parses it to a human form of the web site (something like this one).
All jolly good but how does your web server know what to send?

Virtual Hosts

This is where name based virtual hosts come in.
One of the first lines in any virtual host contains the domain name that is related to the vhost.
Something like this for Apache:
<VirtualHost *:80>

and something like this for Nginx:
server {

  rewrite ^/(.*)$1 permanent;
Each one starts slightly differently but the same principle applies - that particular virtual host will respond to queries for '' and ''.

Multiple domains

So, to serve different content for different domains is as simple as adding another virtual host.
Let's say you have a subdomain called '' serving a blog (I know, shocking originality!).
The basic creation process would be to create a folder in your public_html folder with the relevant files (let's say a Wordpress install).
A virtual host would be created with the server_name or ServerName as '' which would be configured to point to the blog files and folders in your public_html folder.

Language and frameworks

It doesn't matter what language or framework your domain uses.
To serve multiple Rails applications for example requires the same setup for each application.
Of course, there would be some differences such as port numbers for the mongrel or thin instances. Virtual hosts can't share ports.
For example, you may create a mongrel cluster to run from port 8000 - 8002 for one domain and another running from 8010 - 8012 for your blog and so on.


Once the Slice has been setup and the web server has been installed, serving multiple domains is very easy:
Simply create more vhosts...

1 comment:

  1. ProxyPassReverse / http://%{HTTP_HOST}:8088/
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !.*\.(gif|png|jpg)$
    RewriteRule ^/(.*) http://%{HTTP_HOST}:8088/$1 [P]