Introduction to "original" Nginx

Source: Internet
Author: User
Tags auth epoll http authentication imap sendfile wrapper

First, Introduction

Nginx (engine x) is a high-performance HTTP and reverse proxy service as well as a IMAP/POP3/SMTP service. Nginx was developed by Igor Saisoyev for the second rambler.ru site of Russian traffic (Russian: Рамблер), the first public version 0.1.0 released on October 4, 2004. Released 1.15.3 version August 28, 2018.

It publishes the source code in the form of a BSD license, which is known for its stability, rich feature set, sample configuration files, and consumption of low system resources.

II. Overview of Functions 1, HTTP basic features:

Processing static files, index files and automatic indexing;

Reverse proxy acceleration (no caching), 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 sub-requests to the same proxy or FastCGI are processed concurrently;

SSL and TLS SNI support;

2, IMAP/POP3 Agent service function:

Redirect users to the IMAP/POP3 backend using an external HTTP authentication server;

Use an external HTTP authentication server to authenticate the user after the connection is redirected to the internal SMTP backend;

Authentication method:

Pop3:pop3 User/pass, APOP, AUTH LOGIN PLAIN cram-md5;

Imap:imap LOGIN;

Smtp:auth LOGIN PLAIN cram-md5;

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 Ten i386;

MacOS X (10.4) PPC;

4. Structure and Expansion:

A master process and multiple worker processes. The work process is single-threaded and does 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;

The different features supported by Kqueue include Ev_clear, ev_disable (temporary Forbidden event), Note_lowat, ev_eof, number of valid data, error codes;

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 the inactive HTTP keep-alive connection requires only 2.5M of memory.

Minimal data copy operation;

5. Other HTTP Features:

Virtual Host service based on IP and name;

GET interface of Memcached;

Support keep-alive and pipeline connection;

Flexible and simple configuration;

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

Customizable access logs, log write cache, and fast log back volumes;

4XX-5XX error code redirection;

Rewrite rewrite module based on PCRE;

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

PUT, DELETE, and Mkcol methods;

Support for FLV (Flash video);

Bandwidth limit;

Three, installation and start-up

Command:

Yum install-y gcc gcc-c++  pcre pcre-devel zlib zlib-devel # installation dependent Cd/usr/local/srcwget Http://nginx.org/download/ngin x-1.15.3.tar.gz # Download Source codes tar-xvzf NGINX-1.15.3.TAR.GZCD nginx-1.15.3./configure--prefix=/usr/local/nginx

Make && make install #编译, install

Start:/usr/local/nginx/sbin/nginx

Test configuration file:/usr/local/nginx/sbin/nginx–t

Restart:/usr/local/nginx/sbin/nginx-s Reopen

Reload configuration file:/usr/local/nginx/sbin/nginx-s Reload

Quick close:/usr/local/nginx/sbin/nginx-s stop

Graceful Close:/usr/local/nginx/sbin/nginx-s quit

Access:

http://192.168.141.130/

Iv. Configuration Example 1), reverse proxy

Forward Proxy-forwarding agent (forward proxy):

Reverse proxy (reverse proxies):

Configuration:

    server {        listen       ;        server_name  fxdl.test.com;         LOCATION/FXDL {            proxy_redirect off;            Proxy_set_header Host $host;            Proxy_set_header x-real-ip $remote _addr;            Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;            Proxy_pass http://192.168.141.130:8080/fxdl;        }    }

  

2), Static and dynamic separation

Configuration:

    server {        listen       ;        server_name  djfl.test.com;         Location ^~/static/        {            alias/usr/local/nginx/djfl/;            break;        }         LOCATION/DJFL {            proxy_redirect off;            Proxy_set_header Host $host;            Proxy_set_header x-real-ip $remote _addr;            Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;            Proxy_pass HTTP://192.168.141.130:8080/DJFL;        }}

  

3), Load Balancing

Configuration:

Upstream fzjh{    server 192.168.141.130:8080;    Server 192.168.141.130:9090;}   server{        listen       ;        server_name  fzjh.test.com;               Location ^~/static/        {            alias/usr/local/nginx/fzjh/;            break;        }         LOCATION/FZJH {            proxy_redirect off;            Proxy_set_header Host $host;            Proxy_set_header x-real-ip $remote _addr;            Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;            Proxy_pass http://fzjh/fzjh;        }    }

  

Load Balancing algorithm:

Nginx upstream currently supports distribution in the following ways

(1), polling (default)

Each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.

(2), weight

Specifies the polling probability, proportional to the weight and access ratios, for situations where 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 has fixed access to a back-end server that resolves the session issue.

(4), Fair (third party)

The response time of the back-end server is allocated to the request, and the response time is short of priority allocation.

(5), Url_hash (third party)

Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.

4), FastCGI

FastCGI is a scalable, high-speed interface for communicating between HTTP servers and dynamic scripting languages (the FastCGI interface is a socket (which can be a file socket or an IP socket) under Linux. The main advantage is separating the dynamic language from the HTTP server. Most popular HTTP servers support FASTCGI, including Apache, Nginx, and LIGHTPD.

At the same time, FASTCGI is supported by many scripting languages, and one of the more popular scripting languages is PHP. The FastCGI interface uses a C/s architecture that separates the HTTP server from the script resolution server and initiates one or more script parsing daemons on the script resolution server. When the HTTP server encounters a dynamic program each time, it can be delivered directly to the fastcgi process execution, and the resulting structure is returned to the browser. This approach allows the HTTP server to handle 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 HTTP servers and dynamic scripting languages.

2, fastcgi advantage is to separate dynamic language parsing and HTTP server.

3, Nginx, Apache, lighttpd and most dynamic languages support fastcgi.

4. The FastCGI interface adopts C/s architecture, which is divided into client (HTTP server) and server (Dynamic language resolution servers).

5, PHP Dynamic language server can start a number of fastcgi daemon.

6. The HTTP server communicates through the FASTCGI client and the dynamic language fastcgi service side.

Operation Principle of Nginx fastcgi

Nginx does not support direct invocation or parsing of external dynamic programs, and all external programs (including PHP) must be called through the FastCGI interface. The FastCGI interface is a socket (which can be a file socket or an IP socket) under Linux. In order to invoke the CGI program, a fastcgi wrapper is also required, which is bound to a fixed socket, such as a port or a file socket. When Nginx sends the CGI request to the socket, through the FastCGI interface, the wrapper receives the request, and then derives a new thread, which invokes the interpreter or the external program to process the script and reads the return data; Wrapper the returned data through the FastCGI interface, along the fixed socket to nginx; Finally, Nginx sends the returned data to the client, which is the whole process of nginx+fastcgi.

The main advantage of fastcgi is that the dynamic language and the HTTP server are separated, it is nginx to handle the static request and forward the dynamic request, while the PHP/PHP-FPM server exclusively resolves the PHP dynamic request.

5), rewrite

Look at the following two pictures:

Configuration:

    server {        listen       ;        server_name  old.test.com;       Rewrite ^ (. *) $  http://new.test.com$1 permanent;       #return 301 Http://new.test.com$request_uri;    }    server {        listen       ;        server_name  new.test.com;          Location/{            root   html;            Index  index.html index.htm;        }    }

  

Five, Nginx and Apache httpd server comparison

Server

Apache

Nginx

Proxy Agent

Very good

Very good

Rewriter

Good

Very good

Fcgi

Not good

Good

Hot deployment

Not supported

Support

System pressure Comparison

Very big

Very small

Stability

Good

Very good

Security

Good

So so

Technical support

Very good

Rarely

Static file Processing

So so

Very good

Vhosts Virtual Host

Support

Not supported

Reverse Proxy

So so

Very good

Session Sticky

Support

Not supported

Comparison between Apache and Nginx compare features

Nginx, like Apache, are HTTP Server software, in the implementation of the modular structure of the design, all support common Language interface, such as PHP, Perl, Python, etc., but also support forward and reverse proxy, virtual host, URL rewriting, compression transmission, SSL encryption transmission.

    1. In the implementation of the function, all Apache modules support dynamic, static compilation, and Nginx modules are statically compiled,
    2. Support for FASTCGI, Apache support for fcgi is not good, and nginx support for fcgi very well;
    3. In the process of connection, Nginx support Epoll, but Apache does not support;
    4. In space use, Nginx installation package is only hundreds of k, and Nginx compared to Apache is absolutely leviathan.
The advantages of Nginx versus Apache
    • Lightweight, same Web service, consumes less memory and resources than Apache
    • Static processing, Nginx static processing performance than Apache 3 times times higher than
    • Anti-concurrency, Nginx processing requests are asynchronous non-blocking, and Apache is blocking type, in high concurrency, nginx can keep low resource consumption and high performance. In apache+php (prefork) mode, if the PHP process is slow or the front-end pressure is very high, it is easy to get a spike in the number of Apache processes, thereby denying service.
    • Highly modular design, relatively simple to write modules
    • Community active, a variety of high-performance modules produced quickly AH
The advantages of Apache relative Nginx
    • Rewrite, stronger than Nginx's rewrite
    • The module is too much, the basic thought can be found
    • Less Bug,nginx bugs are relatively large
    • Ultra-Stable
    • Apache support for PHP is relatively simple, nginx needs to cooperate with other backend
The advantage of choosing Nginx
    1. As a Web server: Nginx processing static files, index files, automatic indexing is very efficient.
    2. As a proxy server, Nginx can achieve non-cached reverse proxy acceleration, improve the speed of the site.
    3. As a load balancer server, Nginx can support both rails and PHP internally, as well as HTTP proxy servers for external service, while also supporting simple fault tolerance and load balancing using algorithms.
    4. In terms of performance, Nginx is specially developed for performance optimization and is highly efficient in implementation. It uses the Kernel poll model (Epoll and Kqueue), can support more concurrent connections, can support the response to 50 000 concurrent connections, and consumes only a very low amount of memory resources.
    5. In terms of stability, Nginx adopts the phased resource allocation technology, which makes the CPU and memory occupancy rate very low. Nginx official said, Nginx maintained 10 000 inactive connections, and these connections only occupy 2.5MB of memory, so the DOS-like attacks on Nginx basically does not have any effect.
    6. In terms of high availability, Nginx supports hot deployment and starts up very quickly, so you can upgrade your software version or configuration with uninterrupted service, even if it runs for months without restarting, almost 24x7.
Use both Nginx and Apache

Due to the advantages of Nginx and Apache, many people now choose to let the two coexist in the server. On the server side let Nginx in front, Apache in the rear. by Nginx Load balancer and reverse proxy, and processing static files, say dynamic requests (such as PHP application) to Apache to deal with.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.