Nginx (engine x) is an open source WWW service and proxy service software with high performance, high concurrency and high reliability.
Developed by Igor Sysoev, a Russian, the author will open source the source code in the form of BSD like license for global use.
In 2019, F5 will acquire NGINX, with a total value of about US $670 million. F5 load balancing manufacturer.
Open source means that the source code of Nginx can be obtained directly and used for free.
High performance means very fast processing speed and low resource consumption.
High concurrency, supports concurrent connections from 10000 to 50000, and consumes low resources.
High reliability is reflected in the very stable service.
50000 concurrency refers to small static files, NGINX (50000 concurrency)+PHP (700 concurrency).
Select Nginx reason
1. Open source, free
2. Powerful and simple to use
3. High performance and high concurrency
4. High reliability and scalability (many modules, developing third-party modules)
5. The community is active (top 10000 Internet websites), ranking No.1, and growing fast.
Nginx ecological status
OpenResty? It is a high-performance Web platform based on Nginx and Lua. It integrates a large number of sophisticated Lua libraries, third-party modules and most dependencies
It is used to easily build dynamic Web applications, Web services and dynamic gateways that can handle ultra-high concurrency and have high scalability. Born in 2007
Kong Gateway is a highly available and extensible API Gateway project written based on OpenResty (Nginx+Lua module) and open source by Mashape
Based on NGINX and Apache Cassandra or PostgreSQL, Kong can provide easy-to-use RESTful APIs to operate and configure the API management system, so it can horizontally expand multiple Kong servers, distribute requests evenly to each server through the front-end load balancing configuration, and deal with large quantities of network requests
Lua is the script language, nginx+Lua is the script language, which filters and controls requests
MySQL proxy read-write separation software is developed using Lua
Tengine is a web server project initiated by Taobao( //tengine.taobao.org/ )
Nginx Enterprise Applications
As web service software
Nginx is a web service software that supports high performance and high concurrency. It has many superior features. As a web server, compared with Apache, Nginx can support more concurrent connection access, but it occupies less resources, is more efficient, and is also very powerful in function, almost no less than Apache. Moreover, Nginx updates more frequently, and the community users are more popular.
1. Nginx is a static Web service software that uses Nginx to run HTML, JS, CSS, small pictures and other static data (this function is similar to lighttpd software), apache, lighttpd, IIS, Node.js
2. Support dynamic static separation function apache, lighttpd, IIS
3. Support dynamic Web service extension PHP (fastcgipass) JAVA (proxypass) Python (uwsgipass) memcache (memcachepass)
Nginx combines FastCGI to run PHP dynamic programs (using fastcgipass mode)
Nginx and proxypass support tomcat dynamic programs (using proxypass)
Nginx and uwsgipass support Python (using uwsgipass)
4. The whole site HTTPS supporting secure Web services (https) consumes performance. In fact, https can be used for login and https can be used for payment
As reverse proxy or load balancing service software
In terms of reverse proxy or load balancing services, Nginx can be used as a proxy server for dynamic services such as Web services, PHP/Tomcat/Python, and Memcached caches. It has similar functions to professional reverse proxy software (such as Haproxy), and is also an excellent mail proxy service software (one of the purposes of developing this product first was also to serve as a mail proxy service), In terms of Nginx proxy function, it has supported TCP and UDP proxy, and Nginx proxy function has become more and more powerful
1. Similar load balancing software: haproxy, lvs, hardware: F5, netscalernginx only supported http in the early days, but now it also supports tcp/udp. Support tcp load: load mysql and application services.
2. Emphasis on the difference between reverse proxy and load balancing
Forward proxy: from inside to outside. Replace the low efficiency PC in the LAN and request external application services.
Reverse proxy: from outside to inside, replace, low efficiency replace external users to request internal application servers.
Load balancing: forward, high efficiency (LVS) shake off the shopkeeper, and throw back the request.
3. Load Balancing and the Trend of Web Services
Cache server
In terms of Web cache services, Nginx can implement the functions of professional cache software such as Squid through its proxycache module.
Common cache software: squid, varnish, nginx, ats
Summary of main application scenarios of Nginx
1. Static Web server: use Nginx to run HTML, JS, CSS, small pictures and other static data (this function is similar to lighttpd software).
2. Running a dynamic Web server together: Nginx runs PHP and other dynamic programs in combination with FastCGI (for example, using the fastcgipass mode). Nginx, together with proxypass, supports Java dynamic programs (tomcat/resin services). Nginx supports Python in combination with uwsgipass.
3. Reverse proxy/load balancing http load balancing
4. Do a Web cache server (put files into memory).
5. Support secure web services (https)
Nginx core features
Small static files are highly concurrent, occupy less resources, and the software itself is small
1. Support high concurrency: support tens of thousands of concurrent connections (especially in the static small file business environment)
2. Low resource consumption: Under 30000 concurrent connections, 10 Nginx threads will consume less than 200MB of memory
3. It can be used for HTTP reverse proxy and accelerated cache, that is, load balancing function. The built-in health check function for RS node server is equivalent to the function of professional haproxy software or lvs. With the cache function of professional cache software such as squid
Differences between Nginx epoll model and Apache select model
Nginx uses the latest epoll (Linux 2.6 kernel) and kqueue (freebsd) asynchronous network I/O models, while Apache uses the traditional select model
At present, Squid and Memcached software that can withstand high concurrent access under Linux use epoll model
Brief summary:
1. Epoll and select network IO processing model
2. Epoll asynchronous network IO model, supporting high concurrency
3. Select the traditional network IO model, with high concurrency and weak ability
4. Apache uses the traditional select model, and Nginx uses the high concurrency epoll model
Asynchronous and synchronous: and the case of asynchronous static
Overview of network IO model
Generally speaking, network IO can be abstracted as data exchange between user mode and kernel mode. One network data read operation can be divided into two steps:
1. Network card driver waits for data to be ready (kernel state)
2. Copy data from kernel space to process space (user mode)
According to the different processing methods of these two steps, we usually divide network IO into blocking IO and non blocking IO
Nginx software core composition
1. Nginx executable commands:
It is a binary file built by Nginx framework and related modules. This file is equivalent to the car itself, and all functions are provided by it
2. Nginx.conf configuration file:
It is equivalent to the driver. Although the binary executable file has provided many functions, whether these functions are enabled or what actions are defined to handle requests after they are enabled is determined by the nginx. conf file, so it is equivalent to the driver of the car, controlling the behavior of the car
3. Access log access.log:
It is equivalent to the GPS track formed when the car passes through all places. Access.log will record every HTTP request information and response information processed by Nginx
4. Error log error.log:
It is equivalent to the black box of an aircraft. When unexpected problems occur, you can locate them through error.log
Nginx software installation
Yum installation
Configure nginx source
#Method 1: # vim/etc/yum. repos. d/nginx. repo [nginx stable] name=nginx stable repobaseurl= //nginx.org/packages/centos/ $releasever/$basearch/gpgcheck=1enabled=1gpgkey= //nginx.org/keys/nginxsigning.keymodulehotfixes=true# Method 2: # Non interactive operation, pay attention to escape characters # cat>/etc/yum.repos.d/nginx.repo<
Install and Start
Yum install nginx - yrpm - qa nginx # If you start the http service on the server, you must first turn off the http service systemctl stop httpdsystemctl disable httpd # Start the nginx service systemctl start nginx systemctl enable nginx systemctl status nginx # Verify whether the final installation is correct wget 10.0.0.7 # Change the IP address to the IPcurl 10.0.0.7 # Change the IP address to the self server Server IP or browser access
Compile Install
Download source package
mkdir -p /server/toolscd /server/toolswget //nginx.org/download/nginx-1.20.1.tar.gz
Installation dependency
#Rewrite regular related pcre: URL rewriting software to achieve pseudo static URL jump, SEO optimization yum install pcre pcre devel - y # https encryption access using it yum install openssl openssl devel - y
Compile Installation Steps
Userdel - r wwwuseradd - u 1111-s/sbin/nologin www - M # Unzip the nginx compressed package tar xf nginx 1.20.1. tar. gzcd nginx 1.20.1/ln - s/application/nginx 1.20.1//application/nginx # Specify nginx user permissions and compile and install them/ Configure -- user=www -- group=www -- prefix=/application/nginx - 1.20.1/-- with httpstubstatusmodule -- with httpsslmodule -- with pcremakemake install # nginx start check lsof - i: 80/application/nginx/sbin/nginx - t/application/nginx/sbin/nginx stat - lntupgrep nginx
Verify that the final installation is correct
Wget 10.0.0.8 # Change the IP address to your own server's IP curl 10.0.0.8 # Change the IP address to your own server's IP or browser access
The function of the configure parameter
Configure nginx PATH environment variable
#Add vim/etc/profileexport PATH="/application/nginx/sbin/: $PATH" # to the/etc/profile file to make it effective/ Etc/profile # Check and test echo $PATHnginx - t
Configure systemd startup mode
# vim /usr/lib/systemd/system/nginx.service[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network-online.target remote-fs.target nss-lookup.targetWants=network-online.target[Service] Type=forkingPIDFile=/application/nginx/logs/nginx.pidExecStartPre=/usr/bin/rm -f /application/nginx/logs/nginx.pidExecStartPre=/application/nginx/sbin/nginx -tExecStart=/application/nginx/sbin/nginxExecReload=/application/nginx/sbin/nginx -s reloadKillSignal=SIGQUITTimeoutStopSec=5KillMode=processPrivateTmp=true[Install] WantedBy=multi-user.target# chmod +x /usr/lib/systemd/system/nginx.service# systemctl enable nginx# pkill nginx# systemctl start nginx# lsof -i :80
Original source: //www.oldboyedu.com/blog/4332.html