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:
- hiding the existence and characteristics of the actual origin server
- distributing the load
- compressing and caching content
- doing A/B testing
- 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.
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:
- Client sends HTTP request.
- Nginx core chooses the appropriate phase handler based on the configured location matching the request.
- If configured to do so, a load balancer picks an upstream server for proxying.
- Phase handler does its job and passes each output buffer to the first filter.
- First filter passes the output to the second filter.
- Second filter passes the output to the third (and so on).
- 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 is located in text files, typically at
/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.
#error_log logs/error.log notice;
#error_log logs/error.log info;
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.