Ubuntu Gutsy - Nginx config layout

Assuming you have followed this article and have installed Nginx via the Aptitude package manager, we can now have a look at what the install, um, installed.

If you have used Apache with a debian based layout, you will note some very nice similarities and consistencies.


Looksee

Let's start by moving to the Nginx directory and listing what is there.

cd /etc/nginx
ls

You will see this:

Nginx Configuration Directory

The folders are highlighted in blue. Let's look at those first:

sites-available

Inside here will be files containing the configurations for each site you want to serve. Have a look now and see that there is one site (default) available. Although we'll look in detail later at the sites configuration files, you can take a look now:

sudo nano sites-available/default

As I say, we'll look in more detail at the actual configuration later.

Do note that a file in sites-available does not mean they are active (i.e. your users will not be able to browse to the domain) as you need to enable them first. Which brings us to...

sites-enabled

These are symlinks to whichever sites you want to enable.

So if you had 5 sites configured and available (i.e. in the sites-available folder), nothing would happen until you enabled them to be served.

Have a look at the contents of the directory:

ls -l sites-enabled

You will see this:

Nginx sites-enabled

Hmm, nice colours.

You will see the link is named 'default' and points to an 'available' site at /etc/nginx/sites-available/default.

This means that the 'default' site has been enabled. Without the symlink in this folder it would remain available (in the sites-available folder) but not active.

Enabling sites

We've seen that the layout is pretty consistent with an Apache install that uses the Debian structure.

Unfortunately, the similarity ends there so you don't get a handy 'a2ensite' or 'a2enmod' command to play with.

However, it is very simple to enable a site and we'll go through the process by, counter intuitively, disabling the default site.

Disabling a site

To disable the default site, remove the symlink in the sites-enabled folder:

sudo rm /etc/nginx/sites-enabled/default

Now restart Nginx:

sudo /etc/init.d/nginx restart

The first time you do this you will receive the following warning:

Restarting nginx: 2007/12/12 11:49:42 [emerg] 2114#0:
glob() "/etc/nginx/sites-enabled/*" failed in /etc/nginx/nginx.conf:26

You get that as there is a line in the nginx.conf file (line 26) that tells it to include any sites in the sites-enabled directory. Well, we just deleted the default entry so it's a bit confused.

Anyway, now when you navigate to your site:

http://123.45.67.890

You will get a 'Failed to open page' or similar warning from your browser. This is a good thing as we just disabled the default virtual host and would not expect any content to be served.

As you can imagine, enabling the site is just as easy.

Enabling a site

All you do is create a symlink to the desired virtual host file in sites-available:

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

Restart Nginx:

sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx start

Why not use 'restart'?

Well, a restart or force-reload makes no difference. I had to stop and start Nginx to ensure it enabled the new symlink.

Once you have done that, navigating to your IP address will show you the default 'Welcome to nginx' screen.

Summary

There are similarities in the Nginx layout to the commonly used 'Debian style' Apache install.

This allows for consistency and convenience, especially when dealing with enabling and disabling sites.

It also ensures you do not have one huge nginx.conf file which contains all the vhost settings which, as you can imagine, gets unwieldy and inefficient very quickly.

Although there is no handy 'a2ensite' command, creating and deleting the 'sites-enabled' symlinks is very easy to do.

The next articles will concentrate on the main nginx.conf file and then look at creating a new virtual host and what settings are available to us.

PickledOnion.

Article Comments:

Kenny commented Wed Jun 25 20:08:25 UTC 2008:

Would it be possible to update this with info on where the files are if nginx was installed from source?

Björn Lindqvist commented Thu May 06 12:59:33 UTC 2010:

If nginx is installed from source in the directory "/opt/nginx" then the configuration is placed in "/opt/nginx/conf/nginx.conf". In that case, nginx will not use the sites-enabled/available but all configuration will be placed in the same file.

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)