NGINX High Performance - Sample Chapter
NGINX High Performance - Sample Chapter
NGINX High Performance - Sample Chapter
NGINX High
Performance
C o m m u n i t y
$ 39.99 US
26.99 UK
P U B L I S H I N G
pl
Rahul Sharma
Sa
m
ee
D i s t i l l e d
NGINX High
Performance
Optimize NGINX for high-performance, scalable web applications
E x p e r i e n c e
Rahul Sharma
experience in building and designing applications on the Java and J2EE platforms. He
is an open source enthusiast and has contributed to a variety of open source projects,
such as Apache Crunch, Apache Provisionr, Apache HDT, and so on. In his career, he
has worked with companies of various sizes, from enterprises to start-ups, and has
used NGINX along the way. He often speaks at various technical meetups and shares
his knowledge on his personal blog at https://devlearnings.wordpress.com/.
Preface
NGINX is one of the most widely used web servers on the Internet. The server
is often named when one is looking to deliver better performance with the same
hardware. The server has a state-of-the-art event-based architecture, which enables it
to deliver hundreds of thousands of concurrent connections on standard hardware.
As a first step, adopting NGINX leads to better results. However, as with any piece of
software, NGINX can also be optimized to serve content faster. This book provides ways
to optimize NGINX for last-mile performance. It also aims to provide insights into the
NGINX architecture for you to understand it better. The book is not an NGINX learning
book and is intended for people with some experience with NGINX. The book only
explains those parts of the NGINX configuration that have an impact on performance.
Besides NGINX optimization, the book also talks about the process of benchmarking
and about baselines to quantify the gains made. This is an end-to-end book that helps
you to tweak the NGINX server's performance.
Preface
Chapter 4, Controlling Buffers, Timeouts, and Compression, lists the features: keeplive,
send timeouts, client buffers, gzip compression, and controlling logs.
Chapter 5, Configuring the Network Stack, lists details about tweaking the TCP options
to achieve better network utilization. This chapter also talks about various server
defaults that need to be customized.
Chapter 6, Using NGINX Cache, shows that NGINX can cache static content, as well
as dynamic content. The server provides various directives to cache content. The
chapter lists ways to use caches, namely FastCGI Cache, NGINX Proxy Cache, and
Memcache, to render content.
Chapter 7, Extending NGINX, provides details of HttpLuaModule, which can be used
to extend NGINX for all kinds of activities. The module enables support for Lua,
which is a powerful, fast, lightweight, embeddable scripting language. The chapter
aims to build an SEO check using Lua.
[1]
When compared with the thread-based architecture, EBA gives better performance
output. In EBA, there are a fixed number of threads performing tasks and no new
threads are formed. Thus, we achieve better utilization of the CPU and an improved
memory footprint. There is also no overhead of excessive context switching and
no need for a thread stack for each connection. Ideally, the CPU becomes the only
apparent bottleneck of an event-driven application.
NGINX runs one master process and several worker processes. The master process
reads/evaluates the configuration and maintains the worker processes. All request
processing is done by the worker processes. NGINX does not start workers for every
request. Instead, it has a fixed pool of workers for request processing. Each worker
accepts new requests from a shared listen queue. The worker then uses the available
event-notification interfaces, such as epoll and kqueue, to process each connection
in an efficient event loop. The idea is to optimize the utilization of the server's
resources using nonblocking/asynchronous mechanisms when possible. By doing
so, each worker is able to process thousands of connections. The following diagram
shows this:
[2]
Chapter 1
for every kind of processing that NGINX offers, for instance an HTTP module
(ngx_http_core_module) for HTTP processing, an e-mail module (ngx_mail_core_
module) for e-mail processing, a proxy module (ngx_http_proxy_module), and
so on. The modular system is quite extensible and allows third-party developers
to extend NGINX beyond its existing capabilities. Each worker loads a chain of
modules as specified in the NGINX configuration. Every request that a worker
handles goes through the loaded chain of modules.
[3]
Build requirements
Before we start building NGINX, we need to make sure that your system meets the
requirements mentioned in the following pages.
--with-cc-opt: This specifies the additional options that are passed to the
C compiler. You can pass options to include libraries, for example, -I /usr/
local/include.
in PATH.
[4]
Chapter 1
[5]
NGINX, by default, does not enable the SSL module. It can be enabled using the
with-http_ssl_module configuration.
Configuring NGINX
Download and extract the NGINX src package .tar.gz archive from
http://nginx.org/en/download.html. Use the following command:
$ wget http://nginx.org/download/nginx-1.7.9.tar.gz
[6]
Chapter 1
The configuration step provides loads of options to alter default paths and
enable/disable all kinds of modules available with NGINX. Using these, you can
configure NGINX as per your requirements. This step requires some thought as,
once NGINX binaries are built for a configuration, they cannot be altered for default
paths or for the support of disabled modules.
[7]
configuration can specify a PID file, the location to store the process ID of
the main process. If the pid directive is missing from the configuration file,
NGINX stores the information at the location specified by this option.
shared access to resources. The runtime configuration can specify the lock
file. If the lock directive is missing from the configuration file, NGINX stores
the information at the location specified by this option.
--error-log-path : prefix/logs/error.log: An error log path can be
specified in the NGINX runtime configuration. If the error_log directive
is missing in the specified configuration, then NGINX writes the log at the
location specified by this option.
--build: This option assigns a name to the generated binary. The name
would be available in the NGINX -v command.
[8]
Chapter 1
configuration variables based on the client IP address. The variables can then
be used in other modules for certain actions.
is, setting the correct request headers to enable content caching. This can be
used when NGINX is set up as a proxy to an upstream content provider.
The option can disable the cache, but it is not advisable as the cache is quite
handy in most setups.
--without-http_limit_conn_module: This module allows us to control the
number of concurrent connections from a single IP. Post limits, it sends the
503 error response.
[9]
SSI commands
--without-http_upstream_ip_hash_module --without-http_upstream_
keepalive_module --without-http_upstream_least_conn_module
keep-alive parameters
number of connected clients
[ 10 ]
Chapter 1
--without-http_userid_module
--without-http_uwsgi_module
the 444 error or the 204 error in low-memory conditions. This module is not
available on Linux platforms.
your NGINX configuration files and to make Perl calls from SSI.
media files.
[ 11 ]
randomly select and serve any file in a directory as the index file.
protocol.
statistics.
[ 12 ]
Chapter 1
The following output summary lists the libraries used and the NGINX defaults
generated. Makefile, which is generated as a result of the preceding code, can be
used to build and install the NGINX binary. Here's the output summary of the
preceding command:
Configuration summary
+ using PCRE library: ../pcre-8.36
+ using OpenSSL library: ../openssl-1.0.2a
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using system zlib library
+ using libatomic_ops library: ../libatomic_ops-7.2
nginx
nginx
nginx
nginx
nginx
nginx
nginx
nginx
nginx
Chapter 1
The preceding command shows the output listing the locations of installation:
test -d '/etc/nginx' || mkdir -p '/etc/nginx'
test -d '/etc/nginx/sbin'
|| mkdir -p '/etc/nginx/sbin'
test ! -f '/etc/nginx/sbin/nginx'
|| mv
'/etc/nginx/sbin/nginx'
'/etc/nginx/sbin/nginx.old'
cp objs/nginx '/etc/nginx/sbin/nginx'
test -d '/etc/nginx/conf'
|| mkdir -p '/etc/nginx/conf'
cp conf/koi-win '/etc/nginx/conf'
cp conf/koi-utf '/etc/nginx/conf'
cp conf/win-utf '/etc/nginx/conf'
test -f '/etc/nginx/conf/mime.types'
conf/mime.types '/etc/nginx/conf'
|| cp
cp conf/mime.types '/etc/nginx/conf/mime.types.default'
test -f '/etc/nginx/conf/fastcgi_params'
conf/fastcgi_params '/etc/nginx/conf'
cp conf/fastcgi_params
|| cp
'/etc/nginx/conf/fastcgi_params.default'
test -f '/etc/nginx/conf/fastcgi.conf'
conf/fastcgi.conf '/etc/nginx/conf'
|| cp
After installation, NGINX is available in the specified prefix directory. You can go to
the specified location and run it with the sudo command. Alternatively, you can add
it to PATH using update-alternatives and then run it. The following command
shows this:
$ sudo update-alternatives --install /usr/bin/nginx nginx
/etc/nginx/sbin/nginx 1
$ sudo nginx
[ 15 ]
You can check this by loading http://localhost in your browser. It should display
the following NGINX page:
Deploying in NGINX
Now that we have successfully installed NGINX, we want to try out some
deployments in NGINX.
In this section, we have a Hello world! web page that we will deploy in NGINX.
The code uses the Bootstrap library, which needs to be packaged with the code.
The complete package has the following structure:
[ 16 ]
Chapter 1
In this HTML markup, we will display the heading "Hello, world!" and a bit of text
on the web page, which has the title Using Nginx. The Bootstrap CSS has been used to
style the content. You can download it from http://getbootstrap.com/gettingstarted/ or use the wget command from a CDN and add it to the css folder:
$ wget https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.
css
Details of the HTML source code will not be covered here as they are beyond the
scope of the book. The purpose of this code is to deploy a simple web page in NGINX.
Deploying NGINX
We want to load the page created previously at the location http://localhost/hello.
In order to do so, we need to write a configuration block using the following directives:
location: This defines a configuration for a URI. The location URI can be a
prefix or a regular expression.
alias: This defines the directory path for the specified location directive.
include: This allows the inclusion of the configuration blocks defined in one
This is the location from where all the files are served.
[ 17 ]
Here, we have defined the /hello location and set it to serve from index.html at
$ABSOLUTE_PATH_TO_CODE. When the page is loaded, it tries to load the CSS from
/hello/css/bootstrap.min.css. The location directive successfully matches the
/hello prefix and serves the CSS from the $ABSOLUTE_PATH_TO_CODE/css path.
The /hello prefix needs to available in the localhost server's name; thus, we need
to include nginx-localhost-server.conf in the block, which defines the localhost
server in nginx.conf. The following code shows this:
server_name localhost;
# include the hello location
include nginx-localhost-server.conf;
location / {
root html;
index index.html index.htm;
}
Now, all that is left for us to do is reload NGINX. It is always a good idea to test the
NGINX configuration before loading. Testing can flag up possible errors arising from
invalid configurations, which would be discovered while loading NGINX. Use the -t
switch to test the configuration, as shown in the following code:
$ sudo nginx -t
[ 18 ]
Chapter 1
If the test is successful, the command will print the following output; otherwise, it
will print the errors found, if any:
nginx: the configuration file /etc/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/conf/nginx.conf test is
successful
Once we have verified the configuration, reload NGINX with the following
configuration:
$ sudo nginx -s reload
Summary
This chapter gave insights into the components of the performance-oriented
NGINX architecture and the EBA model. NGINX was compiled from source with
components that suit our requirements. The custom build package was installed and
a sample web page was deployed to it.
The purpose of this chapter was to give you a crash course in NGINX. In the
subsequent chapters, we will see ways to measure your NGINX website's
performance and optimize it for the last mile.
[ 20 ]
www.PacktPub.com
Stay Connected: