Linux Enterprise Actual Combat-Introduction to nginx + Source Code Compilation and Installation

What is nginx

Nginx is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server.
Nginx is a lightweight web server/reverse proxy server and email proxy server, and is distributed under a BSD-like protocol. Developed by Russian programmer lgor Sysoev, it is used by Russia's large-scale portal and search engine Rambler. Its characteristic is that it occupies less memory and has strong concurrency. In fact, the concurrency of nginx does perform better in the same type of web server.
Compared with Apache\lighttpd, Nginx has the advantages of less memory and higher stability, and is known for its strong concurrency, rich module library and friendly and flexible configuration. Under the Linux operating system, nginx uses the epoll event model. Thanks to this, nginx is very efficient under the Linux operating system. At the same time, Nginx adopts kqueue, an efficient event model similar to Epoll, on OpenBSD or FreeBSD operating systems.

Nginx VS Apache

The core difference between Apache and Nginx is that apache is a synchronous multi-process model, one connection corresponds to one process; while nginx is asynchronous, multiple connections (ten thousand levels) can correspond to one process.

Apache

● Apache's rewrite is more powerful than nginx. In the case of frequent rewrite, use apache

● Now that apache has developed, there are so many modules, you can find everything you can think of

● Apache is more mature and has fewer bugs, while nginx has relatively more bugs

● apache is super stable

● Apache supports PHP relatively simply, nginx needs to be used with other backends

● Apache has advantages in handling dynamic requests. Nginx is a bitch in this respect. Generally, apache is required to do dynamic requests. Nginx is suitable for static and reverse.

● Apache is still the current mainstream, with rich features, mature technology and development community

Nginx

● Lightweight, written in C, the same web service will take up less memory and resources

● Anti-concurrency, nginx uses epoll and kqueue as the development model, processing requests is asynchronous and non-blocking, and the load capacity is much higher than that of apache, while apache is blocking. Under high concurrency, nginx can maintain low resources, low consumption and high performance, while Apache is prone to soaring number of processes and denial of service when PHP processing is slow or front-end pressure is high.

● nginx handles static files well, and the static processing performance is more than three times higher than that of apache

● The design of nginx is highly modular and it is relatively simple to write modules

● nginx configuration is simple, regular configuration makes many things simple, and after changing the configuration, you can use -t to test whether there is any problem with the configuration. The apache configuration is complicated. When restarting, you will find that the configuration is wrong and it will crash.

● nginx as a load balancing server, supports 7-layer load balancing

● nginx itself is a reverse proxy server, and can be used as a very good mail proxy server

● Startup is particularly easy, and it can almost run 7*24 uninterruptedly, even if it runs for a few months, it does not need to be restarted, and the software version can be upgraded without interruption of service.

● The community is active, and various high-performance modules are produced quickly

to sum up

Generally speaking, for web services that require performance, use nginx. If you don't need performance but stability, consider apache. A more general solution is that the front-end nginx is resistant to concurrency and the back-end apache cluster will work better together.
Nginx advantages

1. As a web server, Nginx processes static files and index files, and the efficiency of automatic indexing is very high.

2. As a proxy server, Nginx can achieve cache-free reverse proxy acceleration and improve website speed

3. As a load balancing server, Nginx can directly support Rails and PHP internally, as well as an HTTP proxy server for external services. It also supports simple fault tolerance and load balancing using algorithms

4. In terms of performance, Nginx is specially developed for performance optimization, and it pays great attention to efficiency in implementation. It adopts the kernel Poll model, which can support more concurrent connections, and can support up to 50,000 concurrent connections, and only takes up very low memory resources

5. In terms of stability, Nginx adopts a staged resource allocation technology, which makes the CPU and memory usage very low. Nginx officially stated that Nginx maintains 10,000 inactive connections, and these connections only occupy 2.5MB of memory. Therefore, attacks like DOS basically have no effect on Nginx.

6. In terms of high availability, Nginx supports hot deployment, and the startup speed is very fast. Therefore, the software version or configuration can be upgraded without interruption of service. Even if it runs for several months, it does not need to be restarted. It can almost do 7x24 hours. Running
 Nginx uninterruptedly has high stability; supports hot deployment; the code quality is very high, the code is very standardized, the technique is mature, and the module extension is easy; it adopts some of the latest features provided by os, such as sendfile (Linux2.2+ ), accept-filter (FreeBSD4.1+), TCP_DEFER_ACCEPT (Linux 2.4+) support, which greatly improves performance.

Nginx multi-process mode

After nginx is started, there will be a master process and multiple worker processes. The master process is mainly used to manage the worker process, including: receiving signals from the outside world, sending signals to each worker process, monitoring the running status of the worker process, and automatically restarting the new worker process when the worker process exits (under abnormal conditions) . Basic network events are handled in the worker process. Multiple worker processes are peer-to-peer, they compete equally for requests from clients, and each process is independent of each other. A request can only be processed in one worker process, and a worker process cannot process requests from other processes. The number of worker processes can be set, generally we will set it to be the same as the number of machine CPU cores.

Nginx event handling mechanism

Nginx uses asynchronous non-blocking event processing mechanism

Synchronous and asynchronous, blocking and non-blocking:
1. Synchronous and asynchronous: mainly for the way the application interacts with the kernel:
Synchronous: After the process sends out data, it will continue to the next request after the kernel returns a response, that is, if the kernel If no data is returned, the process will wait until it is old and crashes.
Asynchronous: After the process sends data, it does not wait for the kernel to return a response, and then processes the next request. Nginx is asynchronous

2. Blocking and non-blocking
can be understood as the way the kernel interacts with the IO device. The processing method when the kernel receives a process request for IO data
can also be simply understood as whether the kernel needs to do something to get a response immediately, if not immediately To get the return, you need to wait, then it is blocked, otherwise it can be understood as non-blocking
blocking: IO calls cannot return results immediately, that is, when an IO request initiated by a process cannot be satisfied immediately, the process has to wait until the kernel responds, and the kernel needs Copying the data from the IO device to the kernel space and then returning it to the process is blocking.
Non-blocking: IO calls can return the results immediately. When the IO process initiated by a process cannot be satisfied immediately, it does not wait, but takes turns over and over again to check whether the IO is completed

Nginx source code compilation

home:
firefox http://nginx.org/en/download.html  下载nginx1.19.1
cd /root/Download
scp nginx-1.19.1.tar.gz server2:/root

server2:
pcs cluster stop --all
pcs cluster disable --now
cd /root
tar zxf nginx-1.19.1.tar.gz 
ls
cd nginx-1.19.1/
yum install gcc -y               安装依赖项
yum install pcre-devel
yum install -y openssl-devel
./configure --prefix=/usr/local/nginx --with-http_ssl_module 
make
make install
cd /usr/local/nginx/
ls
cd sbin/
ls
./nginx -v
./nginx -t
./nginx 
netstat -antlp|grep :80

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42958401/article/details/109309478