Nginx is a high-performance Web server, reverse proxy server and e-mail (IMAP/POP3) proxy server, with the advantages of low memory consumption and strong concurrency.
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.;
Second, nginx process model
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/7F/FA/wKioL1czUGKTDTSKAAK7c_RkVf8516.png "title=" 2016-05-11_232630.png "width=" "height=" 303 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:600PX;HEIGHT:303PX; " alt= "Wkiol1czugktdtskaak7c_rkvf8516.png"/>
Traditionally, a Web service based on a process or threading model schema processes concurrent connection requests through each process or per thread, generating a new process/thread that requires its runtime environment to be prepared, including allocating heap memory and stack memory to it, and creating a new execution context for it. These operations require CPU usage, and too many processes/threads can cause thread jitter or frequent context switching, which can further degrade the system performance.
Nginx runs a master process (master) and several worker processes (workers)that are forked out, and the cache loader process is configured with the cache ( Cache loader) and the caching manager process (caches manager). 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, which only consumes more memory when describing than characters. , and does not cause a high CPU time, which is the reason for nginx concurrency capability . 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 supports sendfile and mmap
sendfile ():
In the traditional way of network file transfer, the file data is actually four copy operation:
Hard disk, kernel buffer-> user Buffer->socket related buffers---Protocol engine
The Sendfile system call provides a way to reduce the number of copy above and improve file transfer performance:
Hard disk---Kernel buffer->socket correlation buffer---Protocol engine
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/80/01/wKiom1c0AOvBZ_U6AABghDB67iU500.png "title=" 2016-05-12_120433.png "width=" "height=" 215 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:215px; " alt= "Wkiom1c0aovbz_u6aabghdb67iu500.png"/>
After kernel version 2.4, Sendfile implemented a simpler way, the system is still called the same way, the details and the 2.1 version of the difference is that when the file data is copied to the kernel buffer, no longer copy all the data to the socket-related buffer, but only the record data location and length-related data saved to so Cket related caches, and the actual data is sent directly by the DMA module to the protocol engine, reducing the copy operation once again.
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7F/FE/wKioL1c0BbmhMS2WAAAw3qYlLbo030.png "title=" 2016-05-12_122123.png "width=" "height=" 233 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:500PX;HEIGHT:233PX; " alt= "Wkiol1c0bbmhms2waaaw3qyllbo030.png"/>
Mmap (): Memory -mapped
The traditional way for a process to access a file is to initiate a system call to the kernel by the process, the kernel loads the file from disk into the kernel buffer, then copies a copy to the process space (user space), and if more than one process accesses the same file, each process contains a copy of the file in its own address space. This unnecessarily wastes storage space.
mmap () system calls enable shared memory between processes by mapping the same common file. After a normal file in the kernel buffer is mapped to the process address space, the process can access the file as it would access the normal memory without having to call read (), write (), and so on.
Three, the Nginx module and working principle
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
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.
The Nginx kernel simply maps HTTP requests to a location block by looking for a configuration file, and each instruction configured in this location launches different modules to complete the work.
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/7F/FB/wKioL1czZpHi4tpWAADuN9QhaQ8768.png "title=" 2016-05-12_010217.png "width=" "height=" 385 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:385px; " alt= "Wkiol1czzphi4tpwaadun9qhaq8768.png"/>
Modular design is an important feature of Nginx. Note that: Nginx module is static, add and delete modules are to recompile the nginx, which is different from the Apache dynamic module
Iv. installation of Nginx compiler
1. Solve the dependency relationship
Yum-y Groupinstall "Development Tools" "Server Platform Development"
Yum-y install gcc pcre-devel openssl-devel zlib-devel
Nginx's rewrite module and HTTP core module use the PCRE regular expression syntax, so installing Pcre-devel,pcre (Perl Compatible Regular Expressions) is a lightweight Perl function library
2, add user Nginx, realize to run Nginx service process
Useradd-r Nginx
3, to the official website download source package, unzip and compile, install
Tar XF nginx-1.9.15.tar.gz
CD nginx-1.9.15
./configure \
--prefix=/usr/local/nginx \
--error-log-path=/data/applogs/nginx/error.log \
--http-log-path=/data/applogs/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
Description
①nginx can use Tmalloc (fast, multi-threaded malloc libraries and excellent performance analysis tools) to speed up memory allocation, use this feature to install Gperftools beforehand, and then add--with-google_perftools_ to the compiler Nginx Module option.
② If you want to use the Nginx Perl module, you can do this by adding the--with-http_perl_module option to the Configure script, but this module is still in the experimental use phase and may be unexpected in the run
4. Provide service script and add boot auto-start
Vim/etc/rc.d/init.d/nginx
...
chmod +x/etc/rc.d/init.d/nginx
Chkconfig--add Nginx
Chkconfig on Nginx
Other operations:
vim/etc/profile.d/nginx.sh
Export path= $PATH:/usr/local/nginx/sbin #更新PATH环境变量
Ln-s/usr/local/nginx/conf/etc/nginx #创建配置文件目录的软链接 for easy access
5. Start the service
Service Nginx Start
[[email protected] ~]# yum -y groupinstall "Development tools" "Server platform development "... [[Email protected] ~]# yum -y install pcre-devel ... [[email protected] ~]# useradd -r nginx[[email protected] ~]# id nginxuid=495 (Nginx) gid=492 (nginx) groups=492 (nginx) [[email protected] ~]# tar xf nginx-1.9.15.tar.gz [[email protected] ~]# cd nginx-1.9.15[[email protected] nginx-1.9.15]# lsauto changes changes.ru conf configure contrib html LICENSE man README src[[email protected] nginx-1.9.15]# ./configure --prefix=/usr/local/nginx > --error-log-path=/data/applogs/nginx/error.log > --http-log-path=/data/applogs/nginx/ Access.log >&nbsP;--pid-path=/var/run/nginx/nginx.pid > --lock-path=/var/lock/nginx.lock > --user= Nginx > --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 ... [[Email protected] nginx-1.9.15]# make && make install ... [[email protected] nginx-1.9.15]# cd /usr/local/nginx/[[email protected] nginx]# lsconf html sbin[[email protected] nginx]# ls conffastcgi.conf &Nbsp; fastcgi_params koi-utf mime.types nginx.conf scgi_params uwsgi_params win-utffastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default[[email protected] nginx]# ls sbinnginx[[email protected] nginx]# ls html/50x.html Index.html[[email protected] nginx]# vim /etc/profile.d/nginx.shexport path= $PATH:/ usr/local/nginx/sbin[[email protected] nginx]# ln -s /usr/local/nginx/conf /etc/ Nginx[[email protected] nginx]# vim /etc/rc.d/init.d/nginx ... [[email protected] nginx]# chmod +x ! $chmod +x /etc/rc.d/init.d/nginx[[ email protected] nginx]# chkconfig --add nginx[[email protected] nginx]# chkconfig nginx on[[email protected] nginx]# service nginx startstarting nginx: [ ok ][[email protected] nginx]# ss -tnlState Recv-Q Send-q local address:port peer addRESS:PORT&NBSP, ..... listen 0 128 *:80 *:*
One of Nginx Learning notes: Nginx Introduction and its compilation and installation