Now we have Nginx installed (whether via the package manager or from source) we are in a position to serve multiple domains using Virtual Hosts.
Do note the layout used in this article is explained here - feel free to use the directories of your choice.
Contents |
In this example we'll be creating two domains, domain1.com and domain2.com
As the default permissions only allow us, the 'demo' user, to browse our home folder, let's start off by giving Nginx access to this folder as well:
chmod 755 /home/demo
OK, good.
We can now create the basic layout for each domain. In your home directory create a 'public_html' folder:
mkdir /home/demo/public_html
Now for each domain you want to host (I use the examples of domain1.com and domain2.com) create a folder with a standard set of sub-folders:
mkdir -p /home/demo/public_html/domain1.com/{public,private,log,backup}and
mkdir -p /home/demo/public_html/domain2.com/{public,private,log,backup}That will create the folders public, private, log and backup for each of our domains (domain1.com and domain2.com).
The content of the public folder is, naturally, up to you but for this example I am going to use a very simple html file so we can check the virtual hosts work.
So for each domain:
nano /home/demo/public_html/domain1.com/public/index.html
Enter something like this into the file:
<html>
<head>
<title>domain1.com</title>
</head>
<body>
<h1>domain1.com</h1>
</body>
</html>Repeat the process so you have a similar file for domain2.com (don't forget to change the index.html content so it shows domain2.com and not domain1.com).
If you have been following the articles for the Nginx install, you will have a 'CentOS' style layout (using a conf.d directory to store your configuration files) whether you installed via the package manager or via source.
As such, we'll use that layout from now on when creating the virtual hosts.
Let's go ahead and edit the virtual file to add domain1.com:
sudo nano /etc/nginx/conf.d/virtual.conf
Remember to adjust the path according to your install. So installing from source would require:
sudo nano /usr/local/nginx/conf/conf.d/virtual.conf
And add the following:
server {
listen 80;
server_name www.domain1.com;
rewrite ^/(.*) http://domain1.com/$1 permanent;
}
server {
listen 80;
server_name domain1.com;
access_log /home/demo/public_html/domain1.com/log/access.log;
error_log /home/demo/public_html/domain1.com/log/error.log;
location / {
root /home/demo/public_html/domain1.com/public/;
index index.html;
}
}The first server module in the file is a simple rewrite rule that redirects visitors to domain1.com from www.domain1.com.
You can, of course, have this the other way around if you prefer.
The second server module has very basic information including the server_name which is the domain name you want to serve.
It then defines the log locations for easy analysis and finally sets the server root and the index file.
As said, very basic at this stage.
All that is left to enable our site is to reload Nginx:
sudo /etc/init.d/nginx reload
Now when you navigate to your domain:
You will see the equivalent of this:

Nice.
All you need to do for your next virtual host (domain2.com in this example) is to repeat the process:
sudo nano /etc/nginx/conf.d/nginx.conf ... # Enter the details for domain2.com as per the example shown above
I know I mention it a lot, but do remember to adjust any paths to match your Nginx installation.
Remember we defined custom locations for the domain logs?
Well, let's have a check they are there:
ls /home/demo/public_html/domain1.com/log/ ... access.log error.log
Excellent, everything is working as we expected and we have our domain logs in a nice and convenient location.
Setting up virtual hosts with Nginx is a simple process using the virtual.conf file to define our virtual hosts.
Although the example here is quite basic, I hope you can see that getting to grips with Nginx syntax and configurations is not too difficult.
The next article will concentrate on some other settings for use in the virtual hosts file, thus allowing for more control and flexibility for your hosting needs.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

4 Comments
Nginx Hosting
re: nginx
We do support nginx on our Managed Cloud level of service, so we could set that up for you on a Linux server through that service.
For the core (unmanaged) Cloud Servers offering, we don't have any images that come with nginx already set up. Since they are barebones Linux installs, you can install nginx from there the same as you might on any other Linux machine.
re: re: nginx
nginx
Add new comment