Getting started with nginx

1. Introduction
Nginx (engine x) is a high-performance HTTP and reverse proxy service, as well as an IMAP/POP3/SMTP service. Nginx was developed by Igor Sysoyev for the second most visited site in Russia (Russian: Рамблер). The first public version 0.1.0 was released on October 4, 2004. Released version 1.15.3 on August 28, 2018.

It releases its source code under a BSD-like license and is known for its stability, rich feature set, sample configuration files, and low system resource consumption.

2. Function overview
1. HTTP basic functions:
Handle static files, index files, and automatic indexing;

Reverse proxy acceleration (no cache), simple load balancing and fault tolerance;

FastCGI, simple load balancing and fault tolerance;

Modular structure. Filters include gzipping, byte ranges, chunked responses, and SSI-filter. In the SSI filter, multiple subrequests to the same proxy or FastCGI are processed concurrently;

SSL and TLS SNI support;

2. IMAP/POP3 proxy service function:
redirect users to an IMAP/POP3 backend using an external HTTP authentication server;

After authenticating the user with an external HTTP authentication server, the connection is redirected to the internal SMTP backend;

Authentication method:




SSL support;

STARTTLS and STLS support in IMAP and POP3 modes;

3. Supported operating systems:
FreeBSD 3.x, 4.x, 5.x, 6.x i386; FreeBSD 5.x, 6.x amd64;

Linux 2.2, 2.4, 2.6 i386; Linux 2.6 amd64;

Solaris 8 i386; Solaris 9 i386 and sun4u; Solaris 10 i386;

MacOS X (10.4) PPC;

4. Structure and extension:
One master process and multiple worker processes. Worker processes are single-threaded and do not require special authorization to run;

kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), rt signals (Linux 2.2.19+), /dev/poll (Solaris 7 11/99+), select, and poll support;

Different functions supported by kqueue include EV_CLEAR, EV_DISABLE (temporarily disable events), NOTE_LOWAT, EV_EOF, number of valid data, error code;

sendfile (FreeBSD 3.1+), sendfile (Linux 2.2+), sendfile64 (Linux 2.4.21+), and sendfilev (Solaris 8 7/01+) support;

Input filtering (FreeBSD 4.1+) and TCP_DEFER_ACCEPT (Linux 2.4+) support;

10,000 inactive HTTP keep-alive connections require only 2.5M of memory.

Minimized data copy operations;

5. Other HTTP functions:
IP and name based virtual hosting services;

GET interface of Memcached;

Support keep-alive and pipe connection;

Flexible and simple configuration;

Reconfiguration and online upgrade without interrupting the customer's work process;

Customizable access logs, log write cache, and fast log rollback;

4xx-5xx error code redirection;

PCRE-based rewrite module;

Access control based on client IP address and HTTP basic authentication;

PUT, DELETE, and MKCOL methods;

Supports FLV (Flash Video);

bandwidth limitation;

3. Installation and start

yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel # Installation dependencies

cd /usr/local/src

wget # Download source code

tar -xvzf nginx-1.15.3.tar.gz

cd nginx-1.15.3

./configure --prefix=/usr/local/nginx

make && make install #Compile and install

Load balancing algorithm:

Nginx's upstream currently supports the following distribution methods

(1), polling (default)

Each request is allocated to different backend servers one by one in chronological order. If the backend server is down, it can be automatically eliminated.

(2), weight

Specify the polling probability, the weight is proportional to the access ratio, and it is used when the performance of the backend server is uneven.

(3), ip_hash

Each request is allocated according to the hash result of the access ip, so that each visitor accesses a backend server regularly, which can solve the session problem.

(4), fair (third party)

Requests are distributed according to the response time of the backend server, and those with short response times are given priority.

(5), url_hash (third party)

Allocate requests according to the hash result of the access url, so that each url is directed to the same backend server, and it is more effective when the backend server is a cache.

4), FastCGI
FastCGI is a scalable and high-speed communication interface between HTTP server and dynamic scripting language (the FastCGI interface is a socket under Linux (it can be a file socket or an ip socket)). The main advantage is that it combines dynamic language and HTTP The servers are separated. Most popular HTTP servers support FastCGI, including Apache, Nginx, and lightpd.

At the same time, FastCGI is also supported by many scripting languages, one of the more popular scripting languages ​​is PHP. The FastCGI interface adopts the C/S architecture, which can separate the HTTP server from the script analysis server, and at the same time start one or more script analysis daemons on the script analysis server. When the HTTP server encounters a dynamic program every time, it can be directly delivered to the FastCGI process for execution, and then the obtained structure is returned to the browser. This method allows the HTTP server to process static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

Important features of FastCGI:

1. FastCGI is an interface or tool for communication between an HTTP server and a dynamic scripting language.

2. The advantage of FastCGI is that it separates dynamic language parsing from the HTTP server.

3. FastCGI is supported by Nginx, Apache, Lighttpd and most dynamic languages.

4. The FastCGI interface adopts C/S architecture, which is divided into client (HTTP server) and server (dynamic language analysis server).

5. The PHP dynamic language server can start multiple FastCGI daemon processes.

6. The HTTP server communicates with the dynamic language FastCGI server through the FastCGI client.

How Nginx FastCGI works

Nginx does not support direct calling or parsing of external dynamic programs, all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket under Linux (it can be a file socket or an ip socket). In order to call the CGI program, a FastCGI wrapper is also needed, and this wrapper is bound to a fixed socket, such as a port or a file socket. When Nginx sends a CGI request to this socket, the wrapper receives the request through the FastCGI interface, and then spawns a new thread, which calls the interpreter or an external program to process the script and read the returned data; then, the wrapper sends The returned data is passed to Nginx along the fixed socket through the FastCGI interface; finally, Nginx sends the returned data to the client, which is the entire operation process of Nginx+FastCGI.

The main advantage of FastCGI is that it separates the dynamic language from the HTTP server. Nginx only handles static requests and forwards dynamic requests backwards, while the PHP/PHP-FPM server only parses PHP dynamic requests.

Comparison between Apache and Nginx

function comparison

Nginx, like Apache, is an HTTP server software. It adopts a modular structure design in terms of function realization, and supports common language interfaces, such as PHP, Perl, Python, etc., and also supports forward and reverse proxy, virtual host, URL rewriting, compressed transmission, SSL encrypted transmission, etc.

In terms of function realization, all modules of Apache support dynamic and static compilation, while Nginx modules are statically compiled.
For FastCGI support, Apache does not support Fcgi well, but Nginx supports Fcgi very well;
In terms of connection processing, Nginx supports epoll, but Apache does not;
In terms of space usage, the Nginx installation package is only a few hundred K. Compared with Nginx, Apache is definitely a monster.

Advantages of Nginx over Apache

Lightweight, also serves as a web service, occupying less memory and resources than apache
Static processing, Nginx static processing performance is more than 3 times higher than Apache
Anti-concurrency, nginx handles requests asynchronously and non-blocking, while Apache is blocking. Under high concurrency, nginx can maintain low resource consumption and high performance. In the Apache+PHP (prefork) mode, if the PHP processing is slow or the front-end pressure is high, it is easy for the number of Apache processes to soar, resulting in denial of service.

Highly modular design, relatively simple to write modules
The community is active, and various high-performance modules are produced quickly.

Advantages of apache over nginx

rewrite, more powerful than nginx's rewrite
There are so many modules, you can find almost everything you think of
Less bugs, nginx has relatively more bugs
super stable
Apache's support for PHP is relatively simple, and Nginx needs to cooperate with other backends
The advantage of choosing Nginx
As a web server: Nginx processes static files, index files, and the efficiency of automatic indexing is very high.
As a proxy server, Nginx can realize cache-free reverse proxy acceleration and improve the running speed of the website.
As a load balancing server, Nginx can not only directly support Rails and PHP internally, but also support HTTP proxy server for external services, and also supports simple fault tolerance and load balancing using algorithms.

In terms of performance, Nginx is specially developed for performance optimization, and it pays great attention to efficiency in its implementation. It adopts the kernel Poll model (epoll and kqueue), can support more concurrent connections, can support the response to 50,000 concurrent connections at most, and only occupies very low memory resources.

In terms of stability, Nginx adopts staged resource allocation technology, which makes the CPU and memory usage very low. Nginx officially stated that Nginx maintains 10,000 inactive connections, and these connections only occupy 2.5MB of memory. Therefore, attacks like DOS basically have no effect on Nginx.

In terms of high availability, Nginx supports hot deployment, and the startup speed is particularly fast. Therefore, the software version or configuration can be upgraded without interrupting the service. Even if it runs for several months, it does not need to be restarted, and it can be done almost 7×24 hours Run without interruption.

Using Nginx and Apache at the same time

Due to the respective advantages of Nginx and Apache, many people now choose to let the two coexist on the server. On the server side, put Nginx in front and Apache in the back. Nginx is used for load balancing and reverse proxy, and static files are processed, and dynamic requests (such as PHP applications) are handed over to Apache for processing.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us