Advertisement
Scroll to top
This post is part of a series called Nginx Guide.
Nginx Guide: Essentials

Concurrency and faster loading times have always been a challenge for any high traffic website. In order to load sites faster, browsers open multiple connections to a web server to load content in parallel. Combined with persistent connections, the web server needs to be really robust and be able to scale non-linearly with the number of requests.

It becomes very important that the website is based on a number of loosely coupled but highly cohesive building blocks, and nginx really is an important piece to be able to achieve that. 

Nginx (pronounced engine x) is a lightweight, free, open-source HTTP and reverse proxy server, as well as a mail proxy server.

Nginx can be used as a full replacement for other HTTP servers like Apache, or can also stand in front of the existing infrastructure you have, to work as a reverse-proxy.

Reverse Proxy Server

Making use of a reverse proxy is very important as it helps in:

  1. hiding the existence and characteristics of the actual origin server
  2. distributing the load
  3. compressing and caching content
  4. doing A/B testing
  5. request / response manipulation

HTTP Server (vs. Apache)

How does nginx differ from Apache? Nginx is event-based and asynchronous, while Apache is entirely dependent on threads. On a relatively high traffic webserver, threads are bound to get memory exhausted. An event based on an asynchronous and non-blocking model (of nginx) is an obvious winner in such a case.

Architecture and Internals

It has a single master process and several 'worker' processes run under an unprivileged user. Within each worker, nginx can handle several thousand connections.

Nginx ArchitectureNginx ArchitectureNginx ArchitectureSource: The Architecture of Open Source Applications

The nginx worker code includes the core and the functional modules. Nginx's modular architecture allows you to extend the web server features without modifying the core. Modules read and write to the network and to storage, transform content, pass the requests to origin servers, include server-side actions and other things. Modules primarily constitute the application layer and presentation functionality. 

Nginx modules come in different forms: core modules, event modules, phase handlers, protocols, variable handlers, filters, upstreams and load balancers. At this time, nginx doesn't support dynamically loaded modules, so modules have to be compiled along with the core during build.

Nginx processes connections as modules piped to each other, as a chain. For every operation there's a module which is doing the relevant work, e.g. compression, modifying content, executing server-side includes, and communicating to the upstream application servers. A typical HTTP request processing cycle looks like the following:

  1. Client sends HTTP request.
  2. Nginx core chooses the appropriate phase handler based on the configured location matching the request.
  3. If configured to do so, a load balancer picks an upstream server for proxying.
  4. Phase handler does its job and passes each output buffer to the first filter.
  5. First filter passes the output to the second filter.
  6. Second filter passes the output to the third (and so on).
  7. Final response is sent to the client.

This layered and loosely coupled but highly cohesive approach makes it really good at what it does.

Download and Installation

Nginx Configuration

Nginx configuration is located in text files, typically at /usr/local/etc/nginx or /etc/nginx. The main configuration file is called nginx.conf. The Nginx configuration file is essentially a list of directives organized in a logical structure. The entire behavior of the application is defined by the values that you give to those directives. 

nginx.conf

1
#user nobody;
2
worker_processes 1;
3
#error_log logs/error.log; 
4
#error_log logs/error.log notice;
5
#error_log logs/error.log info;
6
#pid logs/nginx.pid;
7
8
events {
9
     worker_connections 1024;
10
}
11
12
http {
13
 ...

Above, you see a small extract of a default nginx conf (annotated with explanation). The entire file can also be located at /usr/local/etc/nginx/nginx.conf.default. The gist tries to explain in detail about each directive.

This article introduced what nginx is and the important role it plays in making websites faster. It also touched upon the directives in the configuration file. The second part of the article will go on to explain and give some recipes for using nginx in different contexts and also talking about must-use modules of nginx.  

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.