CentOS - Nginx from source layout


Following from the main installing Nginx from source and creating an init script articles we can now move onto looking at the Nginx layout.

The idea of this article is to change the default layout to one more in keeping with the CentOS' style. Installing Nginx via the package manager creates this layout automatically - we need to do it by hand.

Contents

Why?

You do, of course, have the option of leaving the layout exactly as it is.

However, any further Nginx articles will use the 'CentOS' layout which includes a conf.d directory used to store your configuration files.

This gives common ground to those who installed Nginx via the 'yum' package manager and those who installed Nginx via source.

Layout

As alluded to, the layout we want to create is the same as if we installed Nginx via the package manager.

The main difference being that virtual hosts will now be defined in a 'virtual.conf' file and SSL enabled virtual hosts will be defined in a 'ssl.conf' file. Both configuration files are located in a 'conf.d' folder.

This makes for easy administration to avoid confusion between the main Nginx configuration and your Vhost settings.

Folders

Let's start by creating the conf.d folder (remember Nginx was installed into '/usr/local/nginx'):

sudo mkdir /usr/local/nginx/conf/conf.d

Done.

The conf.d directory is used to store other configuration files that while separate from the main Nginx configuration file, can be used in the same manner.

Configuration

Now we need to adjust the main Nginx configuration file to look in the conf.d folder for our standard and SSL enabled Vhosts:

sudo nano /usr/local/nginx/conf/nginx.conf

I won't list the contents of the file here but note that the vast majority of the contents consists of three virtual hosts - a default for port 80, a second example Vhost, and a default for port 443. The latter of the two are actually commented out.

This is a great example of an unwieldy file. We want to condense this file for easier reading and navigation.

The easiest thing is to delete the contents of the file (you can make a copy if you wish) and replace it with this:

user              nobody;
worker_processes  1;

error_log         logs/error.log;

pid               logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #gzip  on;

    include /usr/local/nginx/conf/conf.d/*.conf;
}

Let's move on with creating the rest of the layout.

Virtual.conf

We need to make a file to store our standard Vhosts on port 80 so they are separated from the main Nginx configuration.

Create the file:

sudo nano /usr/local/nginx/conf/conf.d/virtual.conf

And place the default Vhost as you would see in the original configuration in this file:

server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 }

To create any further virtual hosts on port 80 all you need to do is add them to the end of this file and restart Nginx.

SSL.Conf

Now we need to make the necessary configuration file to store any SSL enabled Vhosts.

Create the ssl.conf file:

sudo nano /usr/local/nginx/conf/conf.d/ssl.conf

For reference, let's place the example Vhost for port 443 from the default nginx.conf in this file:

# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

Make sure no lines wrap to the next line or you could see an error like this when attempting to restart Nginx:

2009/02/17 10:15:49 [emerg] 15662#0: unknown directive "ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP" in /usr/local/nginx/conf/conf.d/ssl.conf:15
2009/02/17 10:15:49 [emerg] 15662#0: the configuration file /usr/local/nginx/conf/nginx.conf test failed

Let's restart Nginx to implement our new changes.

Restart

Restart Nginx:

sudo /etc/init.d/nginx restart

Navigating to your Server IP will show the default "Welcome to nginx!' page.

Done.

All very simple and very organized.

Summary

Mirroring the layout used when installing Nginx via the Yum package manager allows for much easier administration of your virtual hosts. The standard Vhosts as well as any SSL-enabled configurations can now be separated from the main file to avoid confusion.

The next article will look at configuring and optimizing Nginx for use on your Server.

 



Was this content helpful?




© 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


See license specifics and DISCLAIMER