Linux-nginx-1

Source: Internet
Author: User

Nginx
Machine Environment: Web host 192.168.2.18 centos7 test host 192.168.2.29
Nginx ("Engine X") is a high-performance HTTP and reverse proxy server, is also a IMAP/POP3/SMTP proxy server, with less memory, concurrency and other advantages.
Official website: www.nginx.org
Official Document: http://nginx.org/en/docs/
English Document: http://tengine.taobao.org/nginx_docs/cn/docs/
Second, the function and characteristics of nginx
1. Basic functions and characteristics
① as a static resource of the Web server, can cache open file descriptors;
② as a reverse proxy server, can do cache, load balance;
③ Support fastcgi
④ Modular, non-DSO mechanism (unable to load dynamically), filter gzip,ssi and image resizing, etc.
⑤ Support SSL
2. Extended function:
① Virtual host based on name and IP
② Support KeepAlive
③ supports smooth configuration updates or program version upgrades
④ custom Access logs to support the use of log caching for improved performance
⑤ Support URL Rewrite
⑥ support for Path aliases
⑦ support IP and user-based authentication;
⑧ support rate limit, concurrency limit, etc.;

Three, the process model of Nginx


Nginx runs a master process (master) and several worker processes (workers) that are forked, and the cache loader process (cache loader) and the cache manager process (cache manager) are configured as well. All processes contain only one thread, and the process of interprocess communication is achieved primarily through the "shared memory" mechanism. Connection requests are handled by a handful of worker processes that contain only one thread in an efficient loopback (RUN-LOOP) mechanism, which reduces overhead by causing a lot of process/thread context switching. The socket is managed asynchronously within each thread, using the Epoll event-driven mechanism to manage a large number of socket descriptors, and when describing than characters, it only consumes more memory, without causing a significant CPU time. This is the reason that nginx concurrency ability is strong. Each worker can process thousands of concurrent connections and requests in parallel.
Each worker process is equal, and when a connection request comes in, which worker is processed? After the master process has established a socket that needs to be listen, fork out several worker processes, all worker processes grab the mutex, grab the receive and process the connection request, return the data to the client, and finally disconnect, and a request is processed only by one worker.
The number of workers can be set: If the load is CPU-intensive, such as SSL or compression applications, the number of workers should be the same as the number of CPUs, if the load is mainly IO-intensive, such as responding to a large number of content to the client, the number of workers should be 1.5 or twice times the number of CPUs.
The main process runs as root, and the worker, cache loader, and cache manager should run as non-privileged users.
The main process is to complete the following tasks:
① Read and verify the positive configuration information;
② Create, Bind, and close sockets;
③ the number of worker processes to start, terminate, and maintain;
④ reconfiguration of operating features without interruption of service;
⑤ Control non-disruptive program upgrades, enable new binaries and roll back to old versions when needed;
⑥ re-open the log file to achieve log scrolling;
⑦ compiling embedded Perl scripts;
The main tasks that the worker process accomplishes include:
① receives, passes in and processes the connection from the client;
② provides reverse proxy and filtering function;
③nginx any other tasks that can be accomplished;
The main tasks that the cache loader process accomplishes include:
① Check cache objects in the cache store;
② using cached metadata to establish an in-memory database;
Key tasks for the cache manager process:
① cache invalidation and expiration test;

     四、nginx的模块和工作原理

Nginx code is composed of the kernel and a series of modules, the Nginx kernel is mainly used to provide the basic functions of Web server, as well as the web and mail reverse proxy functions, but also to enable network protocols, create the necessary runtime environment and ensure smooth interaction between different modules. However, most of the functions associated with the protocol and the functionality specific to an application are implemented by Nginx's modules.
The Nginx module is structurally divided into:
Core modules
HTTP Module
HTTP standard module
HTTP Optional Modules
such as Http_stub_status_module,http_ssl_module,http_gzip_static_module
Mail Module
Third-party expansion modules
The Nginx module is functionally divided into:
Handlers: This type of module processes requests directly and generates content
Filter: This type of module changes the content generated by other handlers, and finally the Nginx output
Proxies: This type of module interacts primarily with back-end services such as fastcgi to implement functions such as Service Broker and load balancing.

Nginx 1.9.11 began to add support for loading dynamic modules, and no longer need to replace nginx files to increase third-party extensions. At present, only a few of the official modules support dynamic loading, third-party modules need upgrade support to compile into modules.
Here you can refer to this article https://www.cnblogs.com/tinywan/p/6965467.html
Five, Nginx installation
First, you must install the development environment first
Yum-y Groupinstall "Development Tools" "Server Platform Development"
Nginx Rewrite module and HTTP core module will use the PCRE regular expression syntax, so to install the Pcre-devel
Yum-y install gcc pcre-devel openssl-devel zlib-devel
We use Yum installation here, need to have Epel source, configure Epel Source:
Yum Install Epel-release.noarch
Yum Install Nginx
Add an example of a compiled installation
Create Nginx users and groups first
Groupadd Nginx
Useradd-r-s/sbin/nologin Nginx
Download the source Package
wget http://nginx.org/download/nginx-1.14.0.tar.gz
Tar XF nginx-1.14.0.tar.gz
CD nginx-1.14.0
./configure \
--prefix=/usr/local/nginx
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \ #指定以什么身份运行worker进程
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \ #流媒体模块
--with-http_stub_status_module \ #监控运行状态的模块
--with-http_gzip_static_module \
--http-client-body-temp-path=/usr/local/nginx/client \ #临时包体的暂存路径
--http-proxy-temp-path=/usr/local/nginx/proxy \
--HTTP-FASTCGI-TEMP-PATH=/USR/LOCAL/NGINX/FCGI \
--HTTP-UWSGI-TEMP-PATH=/USR/LOCAL/NGINX/UWSGI \
--HTTP-SCGI-TEMP-PATH=/USR/LOCAL/NGINX/SCGI \
--with-pcre
Make && make install

Linux-nginx-1

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.