Nginx working principle and configuration details

Source: Internet
Author: User
Tags imap install openssl sendfile perl script nginx reverse proxy
I. nginx Introduction

Nginx (pronounced engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. igor Sysoev started development of nginx in 2002, with the first public release in 2004. nginx now hosts nearly12.18 % (22.2 m) of active sites website SS all domains. nginx is known for its high performance, stability, rich feature set, simple configuration, and low resource consumption.

Nginx is a free, open-source, high-performance HTTP server, reverse proxy server, and email proxy server. It was developed by the Russian Igor Sysoev in 2002. The first public stable version was released in 2004. So far, ngnix has accounted for 12.18% of the market. Nginx is known for its high performance, stability, rich features (supported by third-party modules), simple configuration, and low resource consumption.

Nginx workflow:

Nginx will run multiple processes at the same time as needed: one master process and several worker processes. When cache is configured, there will also be cache Loader processes (Cache loader) and cache manager process (cache manager. All processes only contain one thread and communicate with each other through the "Shared Memory" mechanism. The master process runs as the root user, while worker, cache loader, and cache manager run as non-privileged users.

The main process mainly completes the following tasks:

1. Read and verify the configuration information;

2. Create, bind, and close a socket;

3. Number of worker processes started, terminated, and maintained;

4. reconfigure the features without suspending the service;

5. Control Non-disruptive program upgrades, enable new binary programs, and roll back to the old version as needed;

6. re-open the log file to scroll the log;

7. Compile the embedded Perl script;

The main tasks completed by the worker process include:

1. Receive, pass in, and process connections from clients;

2. Provides reverse proxy and filtering functions;

3. Any other tasks that nginx can accomplish;

The cache loader process mainly completes the following tasks:

1. Check the cache objects in the cache storage;

2. Use cached metadata to create a memory database;

Main tasks of the cache manager process:

1. cache invalidation and expiration check;

For more details, refer to the official website nginx.org.

Ii. install and configure nginx installation:

Note: The operating system type used here is centos 6.5.

Rpm installation:

Download the official RPM package and install it directly. : Http://nginx.org/packages/

Source code installation:
1. Resolve dependencies # Yum groupinstall "development tools" "server platform deveopment"-yyum install OpenSSL-devel PCRE-devel-Y2, to run the nginx service process: # groupadd-r nginx # useradd-r-g nginx then start compilation and installation :#. /configure -- prefix =/usr/local/nginx \ -----> nginx installation directory -- error-log-Path =/data/applogs/nginx/error. log \ -----> nginx error log -- http-log-Path =/data/applogs/nginx/access. log \ -----> ngnix access log -- PID-Path =/var/run/nginx. PID \ -----> nginx PID file -- lock-Path =/var/lock/nginx. lock \ -----> nginx lock file location -- user = nginx \ -----> nginx process runtime user -- group = nginx \ -----> nginx process runtime group -- with-http_ssl_module \ -----> Add SSL module -- with-http_flv_module \ -----> Add FLV format module -- with-http_stub_status_module \ -----> Add stub_status module -- with-http_gzip_static_module \ -----> Add gzip_static module -- http-client-body-temp-Path =/ usr/local/nginx/client/\ -----> client entity storage location -- http-proxy-temp-Path =/usr/local/nginx/Proxy/\ -----> proxy configuration directory -- HTTP-FastCGI-temp-Path =/usr/local/nginx/fcgi/\ -----> FastCGI location (PHP) -- http-uwsgi-temp-Path =/usr/local/nginx/uwsgi \ -----> uwsgi location (Python) -- http-scgi-temp-Path =/usr/local/nginx/scgi \ -----> scgi configuration location -- With-PCRE -----> support for PCRE # Make & make install
Configuration:

Note: The RMP installation method is used here.

Installed node: 172.16.10.77

650) This. width = 650; "Title =" 1 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 1 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663465ZdKR.png "width =" 689 "Height =" 421 "/>

Brief description of the configuration file:
Nginx configuration has several different contexts: Main, HTTP, server, upstream, and location (and mail that implements reverse proxy for mail service ). The format and definition of the configuration syntax follow the so-called C style. Therefore, nesting is supported, and the logic is clear and easy to create, read, and maintain. Nginx code is composed of a core and a series of modules. The core is mainly used to provide basic functions of Web server and reverse proxy functions of Web and mail. It is also used to enable network protocols, create the necessary runtime environment and ensure smooth interaction between different modules. However, most of the protocol-related functions and specific functions of an application are implemented by the nginx module. These functional modules can be roughly divided into the event module, stage processor, output filter, variable processor, protocol, upstream and load balancing categories, which constitute the nginx HTTP function. The event module is mainly used to provide OS independent event notification mechanisms (different operating systems have different event mechanisms), such as kqueue or epoll. The protocol module establishes a session with the corresponding client through HTTP, TLS/SSL, SMTP, POP3, and IMAP. In nginx, inter-process communication is implemented through the module pipeline or chain. In other words, every function or operation is implemented by a module. For example, compression, FastCGI or uwsgi protocols are used to communicate with the upstream server, as well as to establish sessions with memcached.
1. nginx provides Web Services
[[Email protected] nginx] # Cat nginx. confuser nginx; worker_processes 1; error_log/var/log/nginx/error. log warn; PID/var/run/nginx. PID; events {use epoll; worker_connections 1024;} HTTP {include/etc/nginx/mime. types; default_type application/octet-stream; log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request" ''$ Status $ response" $ http_referer "'' "$ http_user_agent" "$ http_x_forwarded_for "'; access_log/var/log/nginx/access. log main; sendfile on; keepalive_timeout 65; server {Listen 80; add_header X-via $ server_addr; SERVER_NAME www.example.com; Location/{root/usr/share/nginx/html; index index.html index.htm; rewrite ^/IMGs /(. *) $/images/$1 break; # rewrite URL} location/status {stub_status on;} # Set User Authentication, the authentication file must use the htpasswd command to generate location/admin {index index.html; root/usr/share/nginx/html; auth_basic "Admin pass"; auth_basic_user_file/etc/nginx/htpasswd ;} location/BBS {proxy_pass http: // 172.16.10.16/;} include/etc/nginx/CONF. d /*. conf ;}

Result example:

650) This. width = 650; "Title =" 2 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 2 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663475aHsT.png "width =" 748 "Height =" 376 "/>

650) This. width = 650; "Title =" 4 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 4 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663476HYIN.png "width =" 515 "Height =" 204 "/>

650) This. width = 650; "Title =" 5 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 5 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663477oaev.png "width =" 654 "Height =" 285 "/>

650) This. width = 650; "Title =" 6 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 6 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663478q8Pl.png "width =" 479 "Height =" 150 "/>

650) This. width = 650; "Title =" 7 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 7 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663479nAVQ.png "width =" 395 "Height =" 145 "/>

650) This. width = 650; "Title =" 8 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 8 "src =" http://img1.51cto.com/attachment/201409/25/8886857_14116634807Ol0.png "width =" 538 "Height =" 196 "/>

2. nginx reverse proxy and cache provision

The reverse proxy of nginx is implemented by the proxy module. Use the upstream module to achieve simple load balancing.

Reference: http://wiki.nginx.org/Modules

[[email protected] nginx]# cat nginx.confuser  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    use epoll;    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘                      ‘$status $body_bytes_sent "$http_referer" ‘                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;    access_log  /var/log/nginx/access.log  main;    sendfile        on;    keepalive_timeout  65;    proxy_cache_path /data/cache  levels=1:2   keys_zone=web:10m max_size=512m;    upstream web {    #ip_hash;    server 172.16.10.11:80;     server 172.16.10.9:80;    server 172.16.10.16:80 weight=1;    server 172.16.10.77:8080 backup;    }    server {    listen 80;    add_header X-Via $server_addr;    add_header X-Cache-Status $upstream_cache_status;    server_name www.example.com;    location / {    root /usr/share/nginx/html;    proxy_pass http://web;            proxy_cache web;            proxy_cache_valid 200 1d;            proxy_cache_valid 301 302 10m;            proxy_cache_valid any 1m;            index  index.html index.htm;    if ($request_method ~* "PUT") {    proxy_pass http://172.16.10.11;    break;    }    }       }    include /etc/nginx/conf.d/*.conf;}


Result Display:

If no data is cached, It is scheduled to the backend Real Server Based on the scheduling algorithm.

650) This. width = 650; "Title =" 10 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 10 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663483mg2T.png "width =" 814 "Height =" 521 "/>

650) This. width = 650; "Title =" 12 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 12 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663487PAZE.png "width =" 800 "Height =" 598 "/>

4. FastCGI working mode of nginx
# Add the following to the corresponding server segment to implement FastCGI proxy location ~ \. PHP $ {root/PHP; fastcgi_pass 172.16.10.11: 9000; fastcgi_index index. PHP; script_filename/scripts $ parameters; Include fastcgi_params;} [[email protected] nginx] # Cat protected gateway_interface cgi/1.1; fastcgi_param server_software nginx; fastcgi_param QUERY_STRING $ QUERY_STRING; fastcgi_param request_method $ request_method; fastcgi_param content_type $ cont Ent_type; Response content_length $ content_length; Response script_filename $ document_root $ response; Response script_name $ response; Response response $ response; Response document_uri $ document_uri; Response document_root $ document_root; Response server_protocol $ server_protocol; fastcgi_param remote_addr $ remote_addr; fastcgi_param remote_port $ Remote_port; fastcgi_param server_addr $ server_addr; fastcgi_param server_port $ server_port; fastcgi_param SERVER_NAME $ SERVER_NAME; # configure the listening port on 172.16.10.11 in the PHP-FPM environment Yum install PHP-FPM: /etc/php-fpm.d/www. conf Listen = 172.16.10.11: 9000 allowed access port: comment indicates any; listen. allowed_clients = 127.0.0.1mkdir/phpvim/PHP/index. php <? Phpphpinfo ();?> Start PHP-FPM

Result Display:

650) This. width = 650; "Title =" 22 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 22 "src =" http://img1.51cto.com/attachment/201409/25/8886857_14116634883lCA.png "width =" 829 "Height =" 115 "/>

650) This. width = 650; "Title =" 21 "style =" border-right-width: 0px; border-bottom-width: 0px; border-top-width: 0px; "Border =" 0 "alt =" 21 "src =" http://img1.51cto.com/attachment/201409/25/8886857_1411663491UOvZ.png "width =" 780 "Height =" 523 "/>

This article from the "cold" blog, please be sure to keep this source http://guoting.blog.51cto.com/8886857/1558304

Nginx working principle and configuration details

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.