Introduction to Nginx

i

Introduction to Nginx

Introduction to Nginx

Contents

1 Nginx installation on Linux

1.1

1.2

1.3

1.4

Introduction .

.

.

.

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Installation of Nginx in Debian Wheezy 7.2 using online repositories . . . . . . . . . . . . . . . . . . . .

Installation of Nginx in Debian Wheezy 7.2 from sources . . . . . . . . . . . . . . . . . . . . . . . . . .

Installation of Nginx on Ubuntu 12.04 LTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

.

.

.

.

.

.

.

1.5 Use of the checkinstall package to keep track of all the ﬁles created or modiﬁed by an installation script

1.6

Installing Nginx in CentOS 6.4 .

1.6.1

From repositories .

1.6.2

From sources .

.

.

1.6.3 Enabling modules

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

1.7 Adding Nginx as a system service .

2 Nginx Conﬁguration Guide

2.1 Conﬁguration ﬁle syntax .

2.2 Conﬁguration directives .

.

.

.

2.3 Organization and inclusions .

2.4 Base modules .

.

.

2.5 The HTTP Server .

2.6 Mail server proxy .

2.7 Virtual hosts .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3 Nginx and Apache

3.1

Introduction .

.

.

.

.

.

.

3.2 Nginx as reverse proxy .

3.3 Nginx proxy module .

3.4 A note on variables .

3.5 Conﬁguring Apache .

3.6 Conﬁguring Nginx .

3.7 Separating content

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

3.8 Download the conﬁguration ﬁles .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

ii

1

1

1

3

5

8

9

9

.

.

.

.

.

.

.

. 10

. 10

. 12

14

. 14

. 14

. 16

. 18

. 20

. 20

. 21

24

. 24

. 24

. 25

. 27

. 27

. 30

. 31

. 34

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Introduction to Nginx

4 Load balancing with Nginx

4.1

Introduction - The need for load balancing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

4.2 Necessary modules .

.

.

.

4.2.1

upstream module .

.

.

.

.

.

.

4.3 Download the conﬁguration ﬁle

.

.

.

5 Nginx SSL conﬁguration guide

5.1

Introduction .

.

.

.

.

.

.

.

.

.

.

.

5.2 Adding support for SSL to Nginx .

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5.3 Creating, signing, and using a certiﬁcate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5.4 Download the ﬁles

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6 Nginx Websockets proxying guide

6.1

6.2

6.3

Introduction .

.

.

.

Installing Node.js .

.

.

.

.

.

.

.

.

.

.

.

.

Installing additional libraries .

.

.

.

.

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6.4 So. . . what does Nginx has to do with all of this? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6.5 Download source ﬁles .

.

.

.

.

.

.

.

.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

iii

35

. 35

. 35

. 35

. 43

44

. 44

. 44

. 45

. 55

56

. 56

. 56

. 57

. 59

. 62

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Introduction to Nginx

iv

Copyright (c) Exelixis Media P.C., 2016

All rights reserved. Without limiting the rights under
copyright reserved above, no part of this publication
may be reproduced, stored or introduced into a retrieval system, or
transmitted, in any form or by any means (electronic, mechanical,
photocopying, recording or otherwise), without the prior written
permission of the copyright owner.

Introduction to Nginx

v

Preface

Nginx is an open source HTTP and reverse proxy server, as well as a mail proxy server, load balancer, and HTTP cache. The
nginx project started with a strong focus on high concurrency, high performance and low memory usage. It runs on Linux, BSD
variants, Mac OS X, Solaris, AIX, HP-UX, as well as on other *nix ﬂavors. It also has a proof of concept port for Microsoft
Windows. According to Netcraft nginx served or proxied 17.65% busiest sites in March 2014.

This ebook will introduce you to the magic of nginx. You will learn to install and conﬁgure nginx for a variety of software
platforms and how to integrate it with Apache.

Additionally, you will get involved with more advanced concepts like Load Balancing, SSL conﬁguration and Websockets prox-
ying.

Introduction to Nginx

vi

About the Author

Gabriel’s areas of expertise and interest are Linux system administration, shell scripting, database administration (SQL Server,
MySQL, Oracle 11g), object-oriented and procedural programming (Python and PHP), desktop applications (C#, Visual Basic,
Excel with VBA) and web development (jQuery, HTML5, CSS3, PHP).

He has also been working as a Level-1 TSR (Technical Support Representative) supporting onsite the startup and ongoing
operation of the WMS in a major multinational company, running Red Hat Enterprise Linux and Oracle 11g as RDBMS.

Introduction to Nginx

1 / 62

Chapter 1

Nginx installation on Linux

1.1 Introduction

Nginx (pronounced “engine x”) is - in few words- a small, powerful, and scalable web/proxy server. According to a recent survey
performed by Netcraft, Nginx powers more than 15% of the web, equating to 111,680,078 sites (Sept. 2013 Web Server Survey),
including giants like Netﬂix and Wordpress.com.

Nginx is available under the Simpliﬁed BSD License, an open source license, and can be installed either from online repositories
or from sources. In this article we will cover the installation of Nginx in Debian, Ubuntu, and CentOS using both methods.
It is important to note that the repositories are often somewhat out-of-date. If we want the latest features and bugﬁxes, it’s
recommended to build from source or use packages directly from nginx.org.

1.2 Installation of Nginx in Debian Wheezy 7.2 using online repositories

Using aptitude, the high-level interface to the Debian GNU/Linux package manager, we can check the list of packages
related to Nginx (see Fig. 1.1). However, it is advisable to run the command aptitude update ﬁrst in order to see an
updated list of available packages.

Figure 1.1: screenshot

(By the way, the letter “p” in the ﬁrst column indicates that no trace of the package currently exists on the system).

If we can’t decide which package to install, aptitude search -followed by a package name such as aptitude search
nginx- is our friend and will help us to make up our minds as to which one is right for us. Based on the description of each
package listed above, we will proceed with the installation of nginx-full (see Fig. 1.2). It is important to note that the

Introduction to Nginx

2 / 62

description of each package lists the additional modules that are available by default through the installation using repositories,
but we’ll cross that bridge when we get there - later in this tutorial.

Figure 1.2: Installation of nginx-full in Debian Wheezy 7.2 using repositories

At this point, Nginx has been installed but it is not running yet. We will set things in motion with service nginx start
and then we will be able to see its start page in a web browser (see Fig. 1.3).

Then the following command will show us the version of Nginx that we have just installed (see Fig. 1.4):

Figure 1.3: Nginx start page

Introduction to Nginx

3 / 62

Figure 1.4: Nginx v1.2.1 installed from repositories

However, as of today the latest version of Nginx is 1.5.6, while version 1.2.1 is dated June 05, 2012 (NGINX Download Page).
That goes to show that we need to install the program from sources if we want to use the latest, up-to-date version.

1.3 Installation of Nginx in Debian Wheezy 7.2 from sources

Please note that the following instructions represent the default way of building a package from scratch in Linux, so be advised
that the regular installation procedure using ./configure, make, and make install makes it harder for you to uninstall
the package later if you don’t want to use it anymore because there is no way for the system to keep track of all the ﬁles that were
added / modiﬁed during the process.

In summary, you should have a valid reason (a very good one, actually!) for compiling a package using the method mentioned
above. There are a couple of valid reasons why you may want to do so, though. The most common reason is to install a more
recent version of a certain package in your system; another reason is to compile in order to add support for a particular feature.

If you build and install a .deb or a .rpm ﬁle, then the corresponding package manager (aptitude / apt-get or yum,
respectively) will be aware of the presence of the package and it will make sure that you do not overwrite the ﬁles of a previously
installed package. On the other hand, the make install command will overwrite anything that gets in its way. We will
discuss later the other options that we have when we DO have a valid reason to compile and install a package from source.

Now that we have decided that we will not settle for less than the latest version of Nginx, we need to follow these steps to
download the compressed tarball from http://nginx.org/download/ and uncompress it before proceeding with the build per se.

• Download the tarball: wget http://nginx.org/download/nginx-1.5.6.tar.gz

• Uncompress it: tar xvzf nginx-1.5.6.tar.gz

• Go to the directory that was automatically created during the last step: cd nginx-1.5.6

And then:

• ./configure (add the --help option if you want to list all the conﬁguration options). The output of ./configure

shows the directory where Nginx will be installed (/usr/local/nginx, see Fig. 1.5)

Introduction to Nginx

4 / 62

Figure 1.5: Nginx installation path

• make

• make install

Even when the installation is complete, the directory where Nginx is located has not yet been added to the PATH environment
variable (see Fig. 1.6)

Figure 1.6: The PATH variable (before)

Now let’s add the /usr/local/nginx/sbin directory to the PATH variable and let’s check the version of Nginx that we have just
installed from sources (see Fig. 1.7).

Figure 1.7: The PATH variable (after) and Nginx v1.5.6 installed from sources

NOTE: During the conﬁgure process, it is possible that the system will complain about missing libraries (see Fig. 1.8). In that
case we can either installed the packages that provide such libraries (in our case, libpcre3-dev and zlib1g-dev) or ignore
them during conﬁgure.

Introduction to Nginx

5 / 62

Figure 1.8: Missing libraries

1.4 Installation of Nginx on Ubuntu 12.04 LTS

Even though the latest version of Ubuntu is 13.10 (codename Saucy Salamander, released on October 17th, 2013) we have chosen
to perform the installation on Ubuntu 12.04 LTS (codename Precise Pangolin) due to the extended support provided by Canonical
until April 2017.

We will proceed to update sources with sudo aptitude update and then install nginx-full from the distribution’s online
repositories. The sudo keyword must be added as in Ubuntu the root account is disabled by default (see Fig. 1.9). Other than
that, the installation will not differ signiﬁcantly than the same procedure that we just performed in Debian. The same applies to
the installation from source code.

Figure 1.9: Installing package nginx-full in Ubuntu from repositories

However, we can see that the available version is even more outdated in this case (see Fig. 1.10).

Figure 1.10: Nginx version installed in Ubuntu from repos

Introduction to Nginx

6 / 62

As before, we will remove (uninstall) all packages related to nginx before proceeding with the installation from source code (see
Fig. 1.11).

As before, after installing Nginx from source code, we have the latest and up-to-date version of the package (see Fig. 1.12):

Figure 1.11: screenshot

Figure 1.12: screenshot

However, when it comes to starting Nginx -and just as what would have happened had we tried to do so in Debian- we will most
likely get a nginx: unrecognized service error message (see Fig. 1.13).

Figure 1.13: screenshot

This is due to the fact that we have installed the package from sources and therefore the startup script has not yet been put in
place. In this case, we can either start the nginx daemon by running the main executable from the command line using its full

Introduction to Nginx

7 / 62

path (/usr/local/nginx/sbin/nginx) or by writing a script that will take care of the job for us - of course this last option represents
the best choice as we want to be able to use all of the usual arguments (start, stop, restart, reload, and so on).
Also, we can just use one of the startup scripts provided along with this tutorial (which we can also modify to better suit our
needs).

Once we have added the script in the /etc/init.d directory (and named it nginx), we need to change the DAEMON variable
to point to /usr/local/nginx/sbin/nginx and include the installation directory (/usr/local/nginx/sbin) in the
PATH variable (see Figs. 1.14 and 1.15):

Figure 1.14: Nginx startup script (before)

Figure 1.15: Nginx startup script (after)

Then we can run the script as follows (see Fig. 1.16):

Figure 1.16: Nginx listening on port 80 with PID 1931

Also, we need to make sure that the nginx.conf ﬁle “knows” where to ﬁnd the PID of Nginx. Uncomment the following line in
nginx.conf (see Fig. 1.18, most likely it will be found in /usr/local/nginx/conf) and change the path to the one that
the startup script indicates (see Fig. 1.17):

Introduction to Nginx

8 / 62

Figure 1.17: The ﬁle /var/run/$NAME.pid (where $NAME=nginx) contains the current PID of Nginx

Figure 1.18: screenshot

1.5 Use of the checkinstall package to keep track of all the ﬁles created or

modiﬁed by an installation script

The checkinstall package (see Fig. 1.19) keeps track of all the ﬁles created or modiﬁed during the installation process. It
also creates and installs a package (.deb or .rpm) that is compatible with your package manager (see Figs. 16 and 17), which
can be used later to completely remove the package if you don’t need it anymore. Check the attached man page for details on its
use.

Figure 1.19: Installation of the checkinstall package

Introduction to Nginx

9 / 62

Figure 1.20: Creating a .deb ﬁle with checkinstall and make install (I)

Figure 1.21: Creating a .deb ﬁle with checkinstall and make install (II)

When we run aptitude search nginx, the package that we just installed will appear with the comments that we added as
description (see Fig. 1.22):

Figure 1.22: screenshot

1.6 Installing Nginx in CentOS 6.4

1.6.1 From repositories

• Download and install nginx yum conﬁguration ﬁle from http://nginx.org/packages/centos/6. Make sure you select the appro-

priate architecture; “noarch” is a safe choice:

– Download: wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-

0.el6.ngx.noarch.rpm

Introduction to Nginx

10 / 62

– Install: rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm

• Install nginx: yum install nginx

• Start nginx: service nginx start

1.6.2 From sources

Follow the same procedure as for Debian and Ubuntu.

1.6.3 Enabling modules

According to its wiki (NGINX Wiki Page), Nginx modules must be selected during compile as run-time selection of modules is
not currently supported. A full summary of the compile-time options, including optional modules, can be found in the provided
conﬁgure script by running ./configure --help. Unfortunately, if we have installed Nginx installed and want to add a
certain module, we will have to uninstall it and the recompile it with support for the desired module.

For a list of the standard HTTP modules, refer to Table 1 (NGINX Modules):

Name

HTTP Core

Access

Auth Basic

Auto Index

Browser

Charset

Empty GIF

FastCGI

Geo

Gzip

Headers

Index

Limit Requests

Limit Conn

Log

Table 1.1: Standard HTTP modules

Description

Version

Control ports, locations,
error pages, aliases, and
other essentials.
Allow/deny based on IP
address.
Basic HTTP authentication.

Generates automatic
directory listings.
Interpret "User-Agent"
string.
Recode web pages.

Serve a 1x1 image from
memory.
FastCGI Support.

Set conﬁg variables using
key/value pairs of IP
addresses.
Gzip responses.

Set arbitrary HTTP
response headers.
Controls which ﬁles are to
be used as index.
Limit frequency of
connections from a client.
Limit concurrent
connections based on a
variable.
Customize access logs.

0.4.3

0.3.10

0.1.17

0.7.20

conﬁgure argument to
disable
--without-http

--without-
http_access_module
--without-
http_auth_basic_module
--without-
http_autoindex_module
--without-
http_browser_module
--without-
http_charset_module
--without-
http_empty_gif_module
--without-
http_fastcgi_module
--without-http_geo_module

--without-
http_gzip_module

--without-
http_limit_req_module
--without-
http_limit_conn_module

Introduction to Nginx

11 / 62

Map

Memcached

Proxy

Referer

Rewrite

SCGI
Split Clients

SSI
Upstream

User ID

uWSGI

X-Accel

Table 1.1: (continued)

Set conﬁg variables using
arbitrary key/value pairs.
Memcached support.

0.3.16

Proxy to upstream servers.

Filter requests based on
Referer header.
Request rewriting using
regular expressions.
SCGI protocol support.
Splits clients based on some
conditions
Server-side includes.
For load-balancing.

0.8.42
0.8.37

Issue identifying cookies.

uWSGI protocol support.

0.8.40

X-Sendﬁle-like module.

For a list of option HTTP modules, refer to Table 2:

Name

Addition

Auth Request

Degradation

Embedded Perl

FLV
GeoIP

Google Perftools

Gzip Precompression

Gunzip

Image Filter

Table 1.2: Optional HTTP modules

Description

Version

Append text to pages.

Implements client
authorization based on the
result of a subrequest.
Allow to return 204 or 444
code for some locations on
low memory condition.
Use Perl in Nginx conﬁg
ﬁles.
Flash Streaming Video
Creates variables with
information from the
MaxMind GeoIP binary
ﬁles.
Google Performance Tools
support.
Serves precompressed
versions of static ﬁles.
On-the-ﬂy decompressing
of gzipped responses.
Transform images with
Libgd

1.5.4

0.8.25

0.3.21

0.4.7
0.8.6, 0.7.63

0.6.29

0.6.23

1.3.6

0.7.54

--without-
http_map_module
--without-
http_memcached_module
--without-
http_proxy_module
--without-
http_referer_module
--without-
http_rewrite_module
--without-http_scgi_module
--without-
http_split_clients_module
--without-http_ssi_module
--without-
http_upstream_ip_hash_module
(ip_hash directive only)
--without-
http_userid_module
--without-
http_uwsgi_module

conﬁgure argument to
enable
--with-
http_addition_module
--with-
http_auth_request_module

--with-
http_degradation_module

--with-http_perl_module

--with-http_ﬂv_module
--with-http_geoip_module

--with-
google_perftools_module
--with-
http_gzip_static_module
--with-http_gunzip_module

--with-
http_image_ﬁlter_module

Introduction to Nginx

12 / 62

Table 1.2: (continued)

MP4

Random Index

Real IP
Secure Link

SSL
Stub Status

Substitution
WebDAV

XSLT

Enables mp4 streaming
with seeking ability.
Randomize directory
indexes.
For using nginx as backend
Protect pages with a secret
key.
HTTPS/SSL support.
View server statistics.

Replace text in pages
WebDAV pass-through
support.
Post-process pages with
XSLT.

For a list of mail modules, refer to Table 3:

1.1.3, 1.0.7

--with-http_mp4_module

0.7.15

0.3.8
0.7.18

0.1.18

0.3.38

0.7.8

--with-
http_random_index_module
--with-http_realip_module
--with-
http_secure_link_module
--with-http_ssl_module
--with-
http_stub_status_module
--with-http_sub_module
--with-http_dav_module

--with-http_xslt_module

Name
Mail Core
POP3
IMAP
SMTP
Auth HTTP

Proxy

SSL

Table 1.3: Mail modules

Description
Core parameters for mail module.
POP3 settings.
IMAP settings.
SMTP settings.
Use Nginx to authenticate mail
services.
Nginx can proxy IMAP, POP3, and
SMTP protocols.
This module ensures SSL/TLS
support for POP3/IMAP/SMTP.

conﬁgure argument
--with-mail
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module

--with-mail_ssl_module

As an example, we will recompile Nginx to include the Addition module (see Fig. 1.23):

Figure 1.23: Recompiling Nginx with the Addition module

1.7 Adding Nginx as a system service

• Debian / Ubuntu: update-rc.d -f nginx defaults (use sudo on Ubuntu)

Note: If any ﬁles /etc/rcrunlevel.d/[SK]??name already exist then update-rc.d does nothing. The program was written this way so
that it will never change an existing conﬁguration, which may have been customized by the system administrator. The program
will only install links if none are present, i.e., if it appears that the service has never been installed before.

Introduction to Nginx

13 / 62

• CentOS: chkconﬁg nginx on

Introduction to Nginx

14 / 62

Chapter 2

Nginx Conﬁguration Guide

2.1 Conﬁguration ﬁle syntax

According to Merriam-Websters online dictionary, the word syntax represents "the way in which words are put together to form
phrases, clauses, or sentences". Of course that deﬁnition, as it is taken from a dictionary, is aimed at English (or actually any other
language) students. So what about IT people coming from all backgrounds, languages, and countries? The change in context
does not alter the meaning of the term very signiﬁcantly. In particular, the syntax of a conﬁguration ﬁle must be correct, as seen
by the program that will parse it, in order for it to work efﬁciently.

We will learn to understand and how to write (and / or modify) a conﬁguration ﬁle for Nginx under Ubuntu 12.04 LTS. That will
be accomplished by specifying a set of values that will deﬁne the behavior of the web server.

2.2 Conﬁguration directives

By default, the behavior of Nginx is deﬁned by its main conﬁguration ﬁle, which is located (as seen before and unless we have
modiﬁed this setting) at /usr/local/nginx/conf/nginx.conf. This ﬁle is composed of a list of directives organized
in a logical structure that is very easy to follow and understand (see Fig. 2.1).

Introduction to Nginx

15 / 62

Figure 2.1: Nginx main conﬁguration ﬁle (nginx.conf)

The lines that begin with "#" are comments, or in other words, strings of text that are not interpreted by the program during
execution. You can comment out entire lines or add brief comments after a line to clarify what it is supposed to do.

The next line shows a directive, in this case worker_processes, which represents a setting to which we will append one or
more values, depending on its syntax. Actually, the worker_processes directive only accepts one argument, which needs
to be a numerical value. Another example of a directive is user (which is commented out in the previous line). The user
directive lets us add up to 2 text strings - the ﬁrst one is required and indicates the user account Nginx will run as, and the second
one is optional (user group). We will need to remember, right from the start, that each directive has its own meaning and deﬁnes
a particular feature of the application.

Each module that is part of Nginx has a speciﬁc set of directives. When a new module is activated, its speciﬁc set of directives
becomes available, and directive blocks may also be enabled.

Directive blocks are precisely that: a block of text that lets us specify a logical construction of the conﬁguration and allows us
to use inheritance, which means that conﬁguration found in a certain block is passed on to its children blocks as well. However,
inside a children block you can still change the value of a directive that was deﬁned in its parent block.

In Fig. 2.2 and 2.3 we see that all access to the root directory of the site (/) is saved to /home/gacanepa/nginx-1.5.6/
html/access_log except for the access to the directory named restricted (notice the directive access_log off) in the
corresponding directive block.

Introduction to Nginx

16 / 62

Figure 2.2: Nested directive blocks

Figure 2.3: Access logs

2.3 Organization and inclusions

Let’s focus on line 17. There is a special directive there: include. This directive is used to insert the contents of the speciﬁed
ﬁle at this exact location. The name of the ﬁle can be speciﬁed either by an absolute path or a relative path to the current directory
(as it is in this case). We can see that there’s a ﬁle named mime.types in the same directory as nginx.conf (see Fig. 2.4).

Introduction to Nginx

17 / 62

Figure 2.4: screenshot

The end result is the same that would be accomplished by actually inserting the contents of mime.types into the nginx.
conf ﬁle. Of course, if we do that, the main conﬁguration ﬁle would soon become a nightmare to read. This way the include
directive helps us to ensure that the nginx.conf ﬁle remains easy to read and understand. As a plus, it works recursively in that
an included ﬁle can reference another ﬁle and so on, and it also supports filename globbing, which means it recognizes
and expands the standard wild card characters (* and ?) and character lists in square brackets, for example. This way we can add
multiple conﬁguration ﬁles such as 20131030.conf, 20131031.conf, and 20131101.conf. If we only want to include
the ﬁles that begin with 201310, we must add the following line to the nginx.conf ﬁle (see Fig. 2.5):

Figure 2.5: Including ﬁles using wildcards

However, if we add a speciﬁc ﬁle (not by filename globbing) that doesn’t exist, Nginx will not start properly (see Fig.
2.6). Otherwise, we will be presented with a “test is successful” message (see Fig. 2.7):

Introduction to Nginx

18 / 62

Figure 2.6: Nginx fails to restart due to a missing include ﬁle

Figure 2.7: The syntax test of the conﬁguration ﬁle has completed successfully

2.4 Base modules

The base modules allow us to deﬁne the basic parameters and conﬁguration of Nginx. They are built-in into Nginx automatically
during compile time. Their directives and blocks are always available. There are three types of base modules:

• The core module contains essential directives and features. Most of its directives must be placed at the root (meaning the top)
of the conﬁguration ﬁle and are only used once. Table 1 shows some directives in the core module with a brief explanation and
context (if no context is speciﬁed, the actual context is global and the directive must be placed at the root of the conﬁguration
ﬁle).

Table 2.1: Some Events Module directives

Directive/Context
daemon

env

error_log

master_process

pid

Syntax and description
Accepted values: on / off Syntax: daemon on; Enables or
disables daemon mode (starts the program in the
background or the foreground, respectively). Useful to
troubleshoot issues.
Syntax: env MY_VARIABLE=my_value; Allows us to
deﬁne or deﬁne environment variables.
Context: main, http, server, and location
Syntax: error_log /path/to/file level (where
level can be one of the following values: debug, info,
notice, warn, error, and crit, depending on the type of errors
that we want to save in the log ﬁle). To disable error
logging, redirect the log output to /dev/null: error_log
/dev/null crit;
Accepted values: on /off Default value: on If enabled
(on) Nginx will start both the master process and worker
processes. If disabled, Nginx will work with a unique
process. This is used for debugging purposes only and will
cause that clients won’t be able to connect to the server.
Syntax: file path Default value: deﬁned at compile
time. Path of the ﬁle where the PID of Nginx will be stored.

Introduction to Nginx

19 / 62

user

worker_processes

worker_priority

Table 2.1: (continued)

Syntax: user username groupname; user
username; Lets you deﬁne the user account, and
optionally the user group used for starting the worker
processes. For security reasons, a user / group with limited
privileges must be used for this.
Syntax: numeric or auto, for example:
worker_processes 4; Deﬁnes the amount of worker
processes. If a process is blocked for some reason, future
requests can be served by other worker processes. If the
value auto is used, Nginx selects an appropriate value
(which by default it is the amount of CPU cores detected).
Syntax: Numeric worker_priority 0; Deﬁnes the
priority, as the operating system sees it, of the worker
processes from -20 (highest) to 19 (lowest).

• The events module allows us to conﬁgure the operation of the networking capabilities of Nginx. These directives must be
placed inside the events block at the root of the conﬁguration ﬁle (see Fig. 2.8). Table 2 shows two of the directives available
in this module.

Directive/Context
worker_connections

debug_connection

Figure 2.8: The events block

Table 2.2: Some Events Module directives

Syntax and description
Syntax: Numeric Deﬁnes the amount of connections that
a worker process may handle simultaneously.
Syntax: IP address or CIDR block. debug_connection
192.168.0.100 debug_connection 192.168.
0.0/24 Writes detailed log for clients matching this IP
address or CIDR block. The information is saved in the ﬁle
deﬁned in the error_log directive (it must be enabled
with the debug level, and Nginx must be compiled with the
--debug switch in order to enable this directive).

Introduction to Nginx

20 / 62

• The conﬁguration module enables ﬁle inclusions with the include directive, as discussed earlier. The directive may be placed
anywhere in the conﬁguration ﬁle and accepts one (and only one) parameter: the ﬁle’s path relative to the current working
directory (unless it is speciﬁed with the path all the way down from the / directory).

2.5 The HTTP Server

The web server itself is conﬁgured using the directives found in the HTTP Core module. This module is the essential component
of the HTTP conﬁguration and will allow us -among other things- to serve multiple websites, which are referred to as virtual
hosts. It is organized into three major blocks (see Fig. 2.9):

• http: must be placed at the root of the conﬁguration ﬁle. It is used to deﬁne directives and blocks related to the web server

functionality of Nginx.

• server: must be inserted inside the http block and is used to declare a speciﬁc website.

• location: allows us to deﬁne a group of settings to be applied to certain sections of the web site. This block can either be used

within a server block or nested inside another location block.

Figure 2.9: The http, server, and location blocks

2.6 Mail server proxy

To act as mail server (not enabled by default), Nginx must be compiled with the --with-mail (which enables mail server
proxy module with support for POP3, IMAP4, and SMTP) option in ./configure. If for some reason this will be the only
use of Nginx in our system, the HTTP Core module can be disabled using the --without-http switch.

Introduction to Nginx

21 / 62

In Fig. 2.10 we can see a portion of the mail block, which is used to set the conﬁguration of the mail server. The capabilities of
the imap, pop3, and smtp protocols and a detailed description can be found in the IANA (Internet Assigned Numbers Authority)
web site.

Figure 2.10: The mail block

2.7 Virtual hosts

As mentioned earlier, a virtual host is a certain website that is served by Nginx in a single server. The ﬁrst step to set up virtual
hosts is to create one or more server blocks in the main conﬁguration ﬁle. Those server blocks are identiﬁed either by a hostname
or through a combination or IP address and port number (see Fig. 2.11).

Introduction to Nginx

22 / 62

Figure 2.11: Creating a server block to serve a virtual host

Next, we need to set up the virtual host main conﬁguration ﬁle. The default installation of Nginx provides a sample ﬁle (located
in /etc/nginx/sites-available/default) that we will copy and name after our website:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/nginxtest.com

Then the next thing to do is edit the sample ﬁle (nginxtest.com) with basically the same information that is found in the
nginx.conf ﬁle (see Fig. 2.12).

Figure 2.12: Virtual host conﬁguration ﬁle

The virtual host must now be enabled by creating a symlink to this ﬁle in the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/nginxtest.com

To avoid conﬂicts, we can also delete the ﬁle named default in the sites-enabled directory:

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

Now let’s restart Nginx and look what happens! (Fig. 2.13)

Introduction to Nginx

23 / 62

Figure 2.13: Our ﬁrst virtual host is working

To add more virtual hosts, we can just repeat the process above step by step, with the caution to set up a new document root with
the appropriate domain name, and then creating and activating the new virtual host ﬁle as we did with the nginxtest.com website.

Introduction to Nginx

24 / 62

Chapter 3

Nginx and Apache

3.1 Introduction

Nginx and Apache can certainly work together, not necessarily replacing each other as our web server of choice. This solution
offers many advantages and solves the issues that most system administrators are familiar with, such as slowdowns and complex
conﬁgurations. You can just take a look at the Apache conﬁguration ﬁle and chances are you’ll probably agree with me!

3.2 Nginx as reverse proxy

A reverse proxy is a device or service placed between a client and a server in a network infrastructure. Incoming requests are
handled by the proxy, which interacts on behalf of the client with the desired server or service residing on the server1. In this
case, Nginx will act as reverse proxy between the client (your computer, for example) and Apache, the backend web server (see
Fig. 3.1).

Figure 3.1: Nginx acting as reverse proxy

In this above diagram, Nginx acts as reverse proxy (or in other words, as frontend server) and receives all requests from the
outside world. At this point those requests can be ﬁltered or “delivered” to Apache (acting as HTTP client) in the backend. These
two services can even be hosted in the same physical server with the caution to use different listening ports for each of them.
This whole operation is handled by the proxy module of Nginx.

The main purpose of setting up Nginx as a frontend server and giving Apache a simple backend role is to improve the serving
speed, given the fact that great amount of requests coming from clients are for static ﬁles (html pages, cascading style sheets,
regular images, and so on), and static ﬁles are served much faster by Nginx. The overall performance sharply improves both on
the client side and server side.

Introduction to Nginx

25 / 62

3.3 Nginx proxy module

Fortunately, the proxy module is enabled by default during the installation of Nginx. The main directives of the module can be
seen in Table 1.

Table 3.1: Main directives of the proxy module

Directive/Context
proxy_buffers Context: http, server,
location

proxy_method

proxy_hide_header Context: http, server,
location

proxy_pass_header Context: http, server,
location

proxy_pass_request_body
proxy_pass_request_headers Context: http,
server, location
proxy_redirect Context: http, server,
proxy_cachelocation

Description
The request is sent to the backend server by specifying its
location. Syntax: TCP sockets: proxy_pass
http://hostname:port; UNIX domain sockets:
proxy_pass http://unix:/path/to/ﬁle.socket; (https can be
used instead of http for secure trafﬁc) Examples:
proxy_pass http://localhost:8080; proxy_pass
http://127.0.0.1:8080; proxy_pass
http://unix:/tmp/nginx.sock;
Allows overriding the HTTP method of the request to be
forwarded to the backend server. Syntax:
proxy_method method; Example: proxy_method
POST;
By default, as Nginx prepares the response received from
the backend server to be forwarded back to the client, it
ignores some of the http headers4. With this directive, you
can specify an additional header line to be hidden from the
client. This directive can be inserted multiple times with
one header name for each. Syntax:
proxy_hide_header header_name; Example:
proxy_hide_header Cache-Control;
Forces some of the ignored headers to be passed on to the
client. Syntax: proxy_pass_header
header_name; Example: proxy_pass_header
Date;
Deﬁnes whether or not, respectively, the request body and
extra request headers should be passed on to the backend
server. Syntax: on or off;
Allows you to rewrite the URL appearing in the Location
HTTP header on redirections triggered by the backend
server. Syntax: off, default, or the URL of your choice
off: Redirections are forwarded as it is. default: The
value of the proxy_pass directive is used as the
hostname and the current path of the document is
appended. Note that the proxy_redirect directive
must be inserted after the proxy_pass directive as the
conﬁguration is parsed sequentially. URL: Replace a part of
the URL by another. Additionally, you may use variables in
the rewritten URL. Examples: proxy_redirect off;
proxy_redirect default; proxy_redirect
http://localhost:8080/ http://mysite.com/;

The best scenario is to limit to the extent possible the number of requests that are forwarded to the backend server. To that end,
the proxy module comes with a group of directives that will help us build a caching system as well as control buffering options
and the way Nginx deals with temporary ﬁles (see Table 2 for more information on most of these directives).

Introduction to Nginx

26 / 62

Table 3.2: Some caching / buffering directives

Directive/Context
proxy_pass Context: location, if

proxy_method

proxy_cache_key Context: http, server,
location

proxy_cache Context:http, server, location

proxy_cache_path Context: http

proxy_cache_min_uses Context: http, server,
location

Description
Sets the amount and size of buffers that will be used for
reading the response data from the backend server. If the
buffers aren’t large enough the data will be saved to disk
before being served to the user. Syntax:
proxy_buffers amount size; Default: 8 buffers,
4k or 8k each depending on platform Example:
proxy_buffers 8 4k;
Sets the size of the buffer for reading the beginning of the
response from the backend server, which usually contains
simple header data. Syntax: Numeric value (size)
Example: proxy_buffer_size 4k;
This directive deﬁnes the cache key, in other words, it
differentiates one cache entry from another. Syntax:
proxy_cache_key key; Example:
proxy_cache_key
"$scheme$host$request_uri
$cookie_user"; Note: strings beginning with “$”
(dollar sign) are variables. The proxy module offers 4
built-in variables; others can be created at the user’s will.
Deﬁnes a shared memory zone used for caching. The same
zone can be used in several places. The off parameter
disables caching inherited from the previous conﬁguration
level. Syntax: proxy_cache zone off; Default:
proxy_cache off;
Sets the path and other parameters of a cache. Cache data
are stored in ﬁles. Both the key and ﬁle name in a cache are
a result of applying the MD5 function to the proxied URL.
The levels parameter deﬁnes hierarchy levels of a cache.
Syntax: proxy_cache_path path [levels=
levels] keys_zone=name:size [inactive=
time] [max_size=size] [loader_files=
number] [loader_sleep=time]
[loader_threshold=time]; [Optional parameters
are indicated inside square brackets] Example:
proxy_cache_path /tmp/nginx_cache
levels=1:2 zone=zone1:10m inactive=10m
max_size=200M;
Deﬁnes the minimum amount of hits before a request is
eligible for caching. By default, the response of a request is
cached after one hit (next requests with the same cache key
will receive the cached response). Syntax: Numeric value
Example: proxy_cache_min_uses 1;

There are even more directives that let you deﬁne the behavior of Nginx in the case of timeouts or other limitations regarding
communications with the backend server (see Table 3):

Table 3.3: Some directives regarding communications with the backend
server (Apache)

Directive/Context

Description

Introduction to Nginx

27 / 62

Table 3.3: (continued)

proxy_connect_timeout Context: http,
server, location

proxy_read_timeout Context: http, server,
location

proxy_send_timeout Context: http, server,
location

proxy_ignore_client_abort Context: http,
server, location

Deﬁnes the backend server connection timeout. This is
different from the read/send timeout. If Nginx is already
connected to the backend server, the proxy_connect_
timeout is not applicable. It should be noted that this
timeout cannot usually exceed 75 seconds. Syntax: Time
value (in seconds) Example:
proxy_connect_timeout 15;
Deﬁnes a timeout for reading a response from the proxied
server. A timeout is set only between two successive read
operations, not for the transmission of the whole response.
If a proxied server does not transmit anything within this
time, a connection is closed. Syntax: Time value (in
seconds) Default value: 60 Example:
proxy_read_timeout 60;
This timeout is for sending data to the backend server. The
timeout isn’t applied to the entire response delay but
between two write operations instead. Syntax: Time value
(in seconds) Default value: 60 Example:
proxy_send_timeout 60;
Determines whether the connection with a proxied server
should be closed when a client closes a connection without
waiting for a response. If set to on, Nginx will continue
processing the proxy request, even if the client aborts its
request. In the other case (off), when the client aborts its
request, Nginx also aborts its request to the backend server.
Default value: off

3.4 A note on variables

The proxy module comes with the following variables that can be used as arguments for the directives listed above:

• $proxy_host: the hostname of the backend server.

• $proxy_port: the port of the backend server.

• $proxy_add_x_forwarded_for: Contains client request-header "X-Forwarded-For" with separated by comma $re-

mote_addr. If there is no X-Forwarded-For request-header, than $proxy_add_x_forwarded_for is equal to $remote_addr.

• $proxy_internal_body_length: Length of the request body (set with the proxy_set_body directive or 0).

3.5 Conﬁguring Apache

By default, web servers are conﬁgured to listen on tcp port 80. So the ﬁrst thing that we need to do is to change the settings of
Apache in order to avoid conﬂicts with Nginx (which will be running as the frontend server).

In Ubuntu 12.04, the main conﬁguration ﬁle for Apache is located in /etc/apache2 under the name ports.conf (see Fig.
3.2).

Introduction to Nginx

28 / 62

Figure 3.2: The Apache conﬁguration ﬁles

There are 3 main elements that need to be replaced in our Apache conﬁguration (see Fig. 3.3 and 3.4):

1) The Listen directive must be changed to a port other than 80, such as 8080. 2) The following conﬁguration directive is present
in the main conﬁguration ﬁle:

NameVirtualHost A.B.C.D:8080

where A.B.C.D is the IP address of the main network interface on which server communications (between the frontend and the
backend servers) go through. In this case, we use the loopback interface and its IP address since both Apache and Nginx are
installed in the same physical server. If you do not host Apache on the same server, you will need to specify the IP address of the
network interface that can communicate with the server hosting Nginx. 3) The port that was just selected must be reported in all
our virtual hosts conﬁguration sections (in /etc/apache2/sites-available/default).

Figure 3.3: screenshot

Introduction to Nginx

29 / 62

After restarting Apache, we can open a web browser and conﬁrm that it is listening on port 8080 (see Fig. 3.5):

Figure 3.4: screenshot

Figure 3.5: Apache is listening on port 8080

As a extra security measure, we can tell Apache to only serve requests coming from the frontend server. This can be performed
in 2 ways: 1) system wide or by 2) establishing per-virtual-host restrictions.

1) As discussed earlier, the Listen directive of Apache lets you specify a port, but also an IP address. However, by default, no IP
address is selected which results in communications coming from all interfaces. All you have to do is replace the Listen *:
8080 directive by Listen 127.0.0.1:8080, Apache should then only listen on the local IP address.

2) Using the allow and deny Apache directives we can deﬁne which IP addresses will be able to access each virtual host. Once
the changes are made, Apache must be restarted (or its conﬁguration reloaded) in order to reﬂect the changes that we have just
made.

Introduction to Nginx

30 / 62

3.6 Conﬁguring Nginx

The ﬁrst directive that we will use in the process of enabling proxy options is proxy_pass. Since it can’t be used at the http
or server level, we will include it in every single place that we want to be forwarded. As a preliminary example, we will have all
requests made to the restricted folder be forwarded to the Apache web directory (/var/www). See Figs. 3.6 and 3.7, 3.8 and 3.9:

Figure 3.6: The restricted folder shows a simple notice (BEFORE)

Figure 3.7: Nginx main conﬁguration ﬁle (BEFORE)

Introduction to Nginx

31 / 62

Figure 3.8: A request to view the restricted directory shows a Not Found message since there is no such page in Apache’s root
directory

Figure 3.9: Nginx main conﬁguration ﬁle (AFTER)

3.7 Separating content

In order to take better advantage of this Nginx-Apache setting, we can separate the content that each one will deliver upon
request.

Apache will serve dynamic ﬁles, that is, ﬁles that require some sort of processing before being sent to the client, such as php
ﬁles, Python scripts, and so on. Nginx will serve static ﬁles - all other content that does not require additional processing (html
pages, cascading style sheets, images, media, and so on).

To do this, add the following blocks in the nginx.conf ﬁle (see Fig. 3.10):

Introduction to Nginx

32 / 62

Figure 3.10: Separating content served by the frontend and backend servers

When we restart Nginx, we may run into the following issue (see Fig. 3.11):

Figure 3.11: Missing library

We will go ahead and install the PCRE library that is available in the libpcre3-dev package (refer to tutorial 1: Nginx
installation on Linux). See Fig. 3.12 for details on this package. Once installed, we will have to recompile Nginx.

Introduction to Nginx

33 / 62

Figure 3.12: The PCRE library comes with the libpcre3-dev package

Let’s create a sample php ﬁle in /var/www (see Fig. 3.13):

Figure 3.13: Sample php ﬁle

Now we will point our web browser to http://localhost/test.php. Please note that localhost per se points to the
frontend server, so when it receives a request for a php ﬁle, it will forward the request to Apache (see Fig. 3.14)

Introduction to Nginx

34 / 62

Figure 3.14: Request for php ﬁles are forwarded to the backend server

3.8 Download the conﬁguration ﬁles

Here you can download the conﬁguration ﬁles used in this tutorial: Conﬁg_ﬁles.zip

Introduction to Nginx

35 / 62

Chapter 4

Load balancing with Nginx

4.1 Introduction - The need for load balancing

Load balancing is a networking method for distributing workloads across multiple computing resources, such as servers, a server
cluster (a group of servers that work together in such a way that they can be viewed as a single system), network links, CPUs,
or other hardware components (http://en.wikipedia.org/wiki/Load_balancing_(computing) target=[Wikipedia entry]). This tech-
nique is aimed at increasing both the capacity (supporting a large number of concurrent users) and the reliability of the backend
applications by decreasing (or balancing, to be more accurate) the burden on individual servers or nodes. For that same reason,
load balancing also provides redundancy and disaster recovery capabilities. Using Nginx, we will set up a layer-7 load balancer
(which will distribute requests based upon data found in application layer protocols such as HTTP and FTP).

4.2 Necessary modules

In order for us to set up a load balancer, we will need 2 modules: the proxy module and the upstream module. Both are built
into Nginx by default.

As before, we will work with the nginx.conf ﬁle, which is located in the /usr/local/nginx/conf directory.

4.2.1 upstream module

Insert an upstream directive block inside http { } (see Fig. 4.1). You can name this block as you like (app_rack in the example
below).

Introduction to Nginx

36 / 62

Figure 4.1: An upstream block

The server directive assigns the name and the parameters of server. “Name” can be a domain name, an IP address, port or unix
socket. If domain name resolves to several addresses, then all are used. There are several extra parameters available, but we will
explore them later.

• 10.0.2.13 is the IP address of a Debian Wheezy 7.2 server where Apache is listening on port 8080.

• 10.0.2.14 is the IP address of a CentOS 6.4 server where Apache is listening on port 8080.

• Finally, 127.0.0.1 is the IP address of the loopback interface, where Apache is listening on port 8080 in the same physical

server where Nginx is running on port 80.

This way we have three servers - all of them running a simple PHP script that can be served by an instance of Apache (see Fig.
4.2).

As we mentioned earlier, the server directive that appears within upstream blocks accepts several parameters that inﬂuence the
backend selection by Nginx:

• weight=n: if this parameter is placed after a server name, that server will be selected “n-times” more often.

• max_fails=n: This deﬁnes the number of timed-out connections that should occur (in the time frame speciﬁed with the

fail_timeout parameter below) before Nginx considers the server inoperative.

• fail_timeout=n: If Nginx fails to communicate with the backend server max_fails times over fail_timeout seconds, the server

is considered inoperative.

• down: this server is no longer used. This only applies when the ip_hash directive is enabled.

• backup: if a backend server is marked as backup, Nginx will not make use of the server until all other servers (servers not

marked as backup) are down or inoperative.

Introduction to Nginx

37 / 62

Figure 4.2: Nginx is connected to multiple backend servers

Note: There are several other parameters to use with the upstream module. For a more comprehensive list, refer to the online
documentation of the upstream module.

That being said, if we want to ensure that all requests from the same visitor always get processed by the same backend server, we
need to enable the ip_hash option when declaring the upstream block.

Now it is time to “play around” with the parameters discussed above. Let’s take a look at our modiﬁed nginx.conf ﬁle (see Figs.
4.3 and 4.4).

Introduction to Nginx

38 / 62

Figure 4.3: The upstream block in the nginx.conf ﬁle

Figure 4.4: Redirecting requests to the servers listed in the upstream block

With the above settings, we can expect that the CentOS 6.4 server (IP 10.0.2.14) will get twice as much trafﬁc as the other two
backend servers. The presence of the ip_hash option will ensure that each request from a certain visitor -actually, from a
certain IP- will be served by the same backend server.

Oops! An error occurred (see Fig. 4.5)

Introduction to Nginx

39 / 62

Figure 4.5: Nginx returns an error page while trying to forward the request to a backend server

Since we are instructed to take a look at the error log, that’s what we’ll do (see Fig. 4.6)

Figure 4.6: screenshot

It seems as though all of the upstream servers are down!

The backend server #2 is pingable. So what else could be wrong? After running

netstat -npltu | grep :8080

in the CentOS 6.4 server we ﬁnd out that Apache is not running. We can start it with service httpd start.

Let’s see what happens now (see Fig. 4.7)

Introduction to Nginx

40 / 62

Figure 4.7: screenshot

The test.php ﬁle was served by the CentOS 6.4 server.

We will go ahead and edit the nginx.conf ﬁle again (see Figs. 4.8 and 4.9).

Figure 4.8: The requests will be served by either the Debian or the CentOS server

Figure 4.9: Since the Debian and CentOS servers have been marked as down, all the requests will be forwarded to the local
Ubuntu 12.04 server

With the upstream block show in Fig. 4.9, the php ﬁle will be served by the Ubuntu server (see Fig. 4.10)

Introduction to Nginx

41 / 62

Figure 4.10: The php ﬁle is served by the Ubuntu server, which is marked as backup

Now, say that we have an application (running on the 3 servers) that is drawing high trafﬁc. How can we know which server is
receiving each request from the frontend server? That is where the embedded variable $upstream_addr -which is built-in
into the upstream module- and the log_format directive come into play.

• Deﬁne a custom log_format (MyLogFormat in the example below) at http level in the main conﬁguration ﬁle (see Fig.

4.11)

Figure 4.11: Creating a custom log format

• Use the log format created earlier with one of the logs (see Fig. 4.12)

Introduction to Nginx

42 / 62

Figure 4.12: Adding a custom log format to the access log

Now let’s modify the upstream block once again (see Fig. 4.13) in order to forward trafﬁc to the 3 servers.

Figure 4.13: Modifying the upstream block in order to forward requests to the three backend servers

Now restart Nginx and, ﬁnally, let’s see what the access log shows (see Fig. 4.14) after refreshing the browser’s windows a
couple of times:

Figure 4.14: The access log

Introduction to Nginx

43 / 62

4.3 Download the conﬁguration ﬁle

You can download the conﬁguration ﬁle of this tutorial: LoadBalance_conf.zip

Introduction to Nginx

44 / 62

Chapter 5

Nginx SSL conﬁguration guide

5.1 Introduction

The SSL (Secure Socket Layer) protocol was created by Netscape to ensure secure transactions between web servers and browsers
(using secure pages often identiﬁed with https://). The protocol uses a third party, a Certiﬁcate Authority (CA), to identify one
end or both ends of the transactions (The Linux Documentation Project).

5.2 Adding support for SSL to Nginx

Since The Linux Documentation Project website offers a comprehensive explanation of how this procedure is performed, we will
limit this tutorial to show how to set up SSL with Nginx on Ubuntu 12.04 LTS. We will need to compile Nginx with SSL support
(see Fig. 5.1).

Figure 5.1: Compiling Nginx with SSL support

However, the ssl module requires the OpenSSL library (see Fig. 5.2) to be installed on the system beforehand. We will install
the libssl-dev package, which includes the said library (see Fig. 5.3).

Figure 5.2: The ssl module needs the OpenSSL library to be installed

Introduction to Nginx

45 / 62

Figure 5.3: Installing the libssl-dev package, which provides the OpenSSL library

5.3 Creating, signing, and using a certiﬁcate

• We will create a directory to store our public key and ssl certiﬁcate (see Fig. 5.4).

Figure 5.4: Creating a directory to store the public key and ssl certiﬁcate

• Now let’s generate the server private key (see Fig. 5.5)

Figure 5.5: screenshot

Introduction to Nginx

46 / 62

• Create a certiﬁcate signing request (see Fig. 5.6)

Figure 5.6: screenshot

• Sign your certiﬁcate (see Fig. 5.7). Please note that this certiﬁcate will only last one day (you can modify this setting by

changing the argument to the - days option).

Figure 5.7: Signing your ssl certiﬁcate

• Set up the certiﬁcate. Edit the nginx.conf ﬁle (see Fig. 5.8). Note that even though there is a ssl directive (ssl on | off)

available, it is recommended to use the ssl parameter of the listen directive instead of this directive.

Introduction to Nginx

47 / 62

Figure 5.8: screenshot

It should be kept in mind that due to the HTTPS protocol limitations virtual servers should listen on different IP addresses (see
Fig. 5.9). Refer to the nginx.conf ﬁle to perform the following modiﬁcations if needed:

Figure 5.9: screenshot

Otherwise the ﬁrst server’s certiﬁcate will be issued for the second site.

In order to allow to share a single IP address between multiple HTTPS servers is to use a certiﬁcate with a wildcard name,
for example, *.example.org. A wildcard certiﬁcate secures all subdomains of the speciﬁed domain, but only on one level.
This certiﬁcate matches www.example.org, but does not match example.org and www.sub.example.org. These two
methods can also be combined. A certiﬁcate may contain exact and wildcard names in the SubjectAltName ﬁeld, for
example, example.org and *.example.org.

If we want to allow Subject Alternative Names (SANs) for our certiﬁcates we need to enable the following options in the ﬁle
openssl.cnf ﬁle (located in /etc/ssl/openssl/):

1. Include the X509 Version 3 (RFC 2459) extension to allow an SSL certiﬁcate to specify multiple names that the certiﬁcate
should match. We need the [ req ] section to read as follows (see Fig. 5.10). This tells openssl to include the v3_req
section while generating certiﬁcate requests:

Introduction to Nginx

48 / 62

1. Edit the [ v3_req ] section as follows (see Fig. 5.11):

Figure 5.10: screenshot

Figure 5.11: screenshot

Please note that whatever we put in the ﬁle openssl.cnf will appear on all certiﬁcate requests generated from this point on: if at a
later date you want to generate a CSR with different SANs, you’ll need to edit this ﬁle by hand and change the DNS.x entries.

It is better to place a certiﬁcate ﬁle with several names and its private key ﬁle at the http level of conﬁguration to inherit their
single memory copy in all servers (see Fig. 5.12).

Introduction to Nginx

49 / 62

Figure 5.12: Using a certiﬁcate in two server blocks (nginxtest.com y www.nginxtest.com)

1. Now we can either browse to https://nginxtest.com or https://www.nginxtest.com and we’ll see the warning of a self-signed

security certiﬁcate (see Fig. 5.13):

Introduction to Nginx

50 / 62

Figure 5.13: A self-signed certiﬁcate works but presents this warning screen

1. Click on “Add Exception” (see Fig. 5.14) and then on “Conﬁrm Security Exception”:

Figure 5.14: Conﬁrming a Security Exception

If you get a Wrong Site message (see Fig. 5.15), it means that your certiﬁcate belongs to a different site, or that the necessary
SANs have not been speciﬁed.

Introduction to Nginx

51 / 62

Figure 5.15: screenshot

On the other hand, when the SANs have been deﬁned correctly, they can be viewed using the browser’s built-in certiﬁcate viewer
(see Fig. 5.16).

Figure 5.16: Subject Alternative Names

or using (see Fig. 5.17)

openssl req -text -noout -in server.csr from the command line

Introduction to Nginx

52 / 62

Figure 5.17: screenshot

As we mentioned earlier, a certiﬁcate will only be valid within the time frame that was speciﬁed when it was ﬁrst created (1 day,
in our case, refer to Figs. 5.18, 5.19, and 5.20)

Figure 5.18: screenshot

Introduction to Nginx

53 / 62

Figure 5.19: screenshot

Introduction to Nginx

54 / 62

Once that period of time is over, the certiﬁcate will expire (see Fig. 5.21).

Figure 5.20: screenshot

Introduction to Nginx

55 / 62

Figure 5.21: The SSL certiﬁcate expired

Unless you have a certiﬁcate from a trusted third party, your users will get the same warning as above in Fig. 5.13. Aside from
the fact that it looks unprofessional, it’s also a real risk due to the fact that in a large organization, it is not likely that all users
will know the difference between a legitimate key generated by your IT department, and keys generated by a malicious third
party. In this case, you need to buy a certiﬁcate from a trusted third party, such as GeoTrust (used by Google), Digicert, Comodo,
Thawte, or VeriSign (used by Facebook, for example). For other cases, especially when you want to use https for your own use
(i.e. securing your admin panel), a self-signed certiﬁcate will do just ﬁne.

5.4 Download the ﬁles

Here you can download the conﬁguration ﬁles used in this tutorial: ConﬁgFile.zip

Also you can download a useful pdf ﬁle: Openssl.pdf

Introduction to Nginx

56 / 62

Chapter 6

Nginx Websockets proxying guide

6.1 Introduction

According to the RFC (Request For Comments) #64551, issued by the IETF (Internet Engineering Task Force), WebSocket is
a protocol providing full-duplex communications channels over a single TCP connection and is designed to be implemented in
web browsers and servers, but it can be used by any client or server application.

Like TCP, WebSocket makes full-duplex communication possible in a low latency connection, but it differs from TCP in that it
enables a stream of messages instead of a stream of bytes.

In other words, there is a persistent connection between client and server, and any of them can start sending data at any time. This
way you will think about using WebSockets whenever you need a near real-time connection between the client and the server,
whether it is in a web environment or not.

Please note that Nginx supports Websockets starting in version 1.3.13 (released on February 2013) and is accessible within the
core product. Older versions DO NOT support this protocol.

In this tutorial we will also conﬁgure Upstart, a modern replacement for init.d written by the Ubuntu developers, to make sure
that our websocket application (written in Node.js) automatically restarts if it crashes, and starts up when our server boots up.

6.2 Installing Node.js

First off, let’s create a directory inside our home to download the source code for Node.js (see Fig. 6.1). Note that for the sake
of clarity, this directory is located at the same level that the one where we downloaded the source code for Nginx as explained
in Tutorial #1. We will then use the same directory to extract the contents of the tarball and to install Node.js using the regular
procedure:

(We are following the usual build process here because the checkinstall method produced an error while building the deb pack-
age.)

sudo ./configure, sudo make, sudo make install

At the time of this writing, the latest version of Node.js is v0.10.21, which can be downloaded (32-bit or 64-bit versions) from
http://nodejs.org/download/ using the following command (see Fig. 6.2, where the proxy server being used and its IP address
have been blurred for privacy reasons):

wget http://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz

Introduction to Nginx

57 / 62

Figure 6.1: screenshot

Figure 6.2: screenshot

6.3 Installing additional libraries

Next, we will install socket.io, which is a JavaScript library for real-time web applications. It has two parts:

• a client-side library that runs in the browser and

• a server-side library for node.js (Socket.io, Wikipedia) using npm (the ofﬁcial package manager for node.js, sudo npm

install socket.io, see Fig. 6.3), and express, a web application framework for node.js.

Figure 6.3: Installing the socket.io library

To download the express framework we need to deﬁne a couple of settings in a .json ﬁle (package.json) located in a directory
created for our application - ~/chatroom in this example- (see Fig. 6.4). Then we proceed to download and install it with:

npm install -d

Introduction to Nginx

58 / 62

Figure 6.4: The package.json ﬁle

Since in this tutorial we are focusing on how Nginx works with websockets instead of programming with node.js, we will not
develop a web app from scratch, but we will use an existing one which is available via GitHub (Chatroom example). We need to
download the ﬁles index.html and app.js (see Fig. 6.5) using the following command:

wget https://github.com/mmukhin/psitsmike_example_1/archive/master.zip

Figure 6.5: Downloading the chat room web app from GitHub

Then we’ll unzip the ﬁles mentioned earlier and start the app with node app.js (see Fig. 6.6). We may have to stop Apache if it
is running on port 8080.

Figure 6.6: Starting the web application

The result (see Fig. 6.7 and 6.8) is not using Nginx yet and we haven’t mentioned why we want Nginx with websockets yet,
but we’re half-way there though. So far we have a chat room-like web application that displays sent and received messages in
real-time, along with server responses as well.

Introduction to Nginx

59 / 62

Figure 6.7: screenshot

Figure 6.8: screenshot

6.4 So. . . what does Nginx has to do with all of this?

If we need to use this application on a live (production) environment, we will probably want it to listen on port 80 (most
enterprise-level ﬁrewalls allow communications through that port). But Nginx is already listening on that port. What do we do

Introduction to Nginx

60 / 62

now? As before, our robust Nginx web server has the answer. We will simply forward incoming requests on port 80 (external
connections) to another port (8080 in this case for internal connections). This way we are using Nginx as a reverse proxy and the
outside world cannot talk to the chat room application directly, but through Nginx, which acts as a frontend server. This scenario
will also allow us to use SSL certiﬁcates to encrypt trafﬁc.

We will go ahead and edit the nginx.conf ﬁle (see Fig. 6.9) adding a few directives from the proxy module.

Figure 6.9: The main conﬁguration ﬁle, nginx.conf

We will discuss each directive in detail:

• proxy_pass http://localhost:8080 : enables reverse proxying to a backend server by specifying its location (in this case, the

same host, port 8080).

• proxy_http_version 1.1: sets the HTTP version to be used for communicating with the proxy backend. HTTP 1.0 is the

default value, but if we need to enable keepalive connections, it’s best to set this directive to 1.1.

• proxy_set_header: This directive allows you to redeﬁne header values to be transferred to the backend server. As we can see,

it can be declared multiple times.

• proxy_set_header Host $host: The Host HTTP header in the request forwarded to the backend server defaults to the proxy
hostname, as speciﬁed in the conﬁguration ﬁle. This setting lets Nginx use the original Host from the client request instead.
Refer to tutorial #2 for a complete list of http headers.

Since we want the app would start automatically when the server booted up, we need to manage the Node process with an init
script or an upstart supervisor. We will choose the second option in this tutorial. First, we will install forever, a very useful
tool for running and monitoring Node.js processes (see Fig. 6.10).

Figure 6.10: Installing forever

Introduction to Nginx

61 / 62

We also need an init script (app.js.conf - found in the attached zip ﬁle, adapted from ExRatione) in the /etc/init directory to
start (see Fig. 6.11) / stop / restart our process (app.js) and if need be, display its status. This script is a generic upstart ﬁle
where we deﬁne certain environment variables that are necessary for the script to run (see Fig. 6.12).

Figure 6.11: Our web application is started

Figure 6.12: Environment variables deﬁned by the upstart script

In the meanwhile, what we write in our chat room (see Fig. 6.13) is saved in the log of our application and we can see it in
real-time, both in the web interface and from the command line using the command:

tail -f app.js.log

Notice that we are accessing our web application using Nginx as our frontend server (as opposed to the case shown in Figs. 6a
and 6b, where the web application is run by node.js exclusively running on port 8080).

Introduction to Nginx

62 / 62

Figure 6.13: screenshot

6.5 Download source ﬁles

You can download the source ﬁles of this tutorial: WebsocketsExample.zip

