2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Nginx official website: http://nginx.org
Nginx is a free, open source, high-performance HTTP and reverse proxy server, mail proxy server, and TCP/UDP proxy server.
Other secondary distributions of nginx:
Tengine
A web server project initiated by Taobao.com. Based on Nginx, it adds many advanced functions and features to meet the needs of websites with large traffic. Tengine's performance and stability have been well tested on large websites such as Taobao.com and Tmall.com. Its ultimate goal is to create an efficient, stable, secure and easy-to-use web platform. Since December 2011, Tengine has become an open source project.
Official website: http://tengine.taobao.org/
OpenResty
High-performance Web platform based on Nginx and Lua language
Official website: http://openresty.org/cn/
Nginx Features
Static web resource server html, pictures, js, css, txt and other static resources
Reverse proxy for http/https protocols
Combine FastCGI/uWSGI/SCGI and other protocols to reverse proxy dynamic resource requests
TCP/UDP protocol request forwarding (reverse proxy)
Reverse proxy for imap4/pop3 protocol
Basic Features
Modular design, good scalability
High reliability
Support hot deployment: update configuration files, upgrade versions, and replace log files without stopping the server
Low memory consumption: 10,000 inactive connections in keep-alive connection mode only require 2.5M memory
event-driven,aio,mmap,sendfile
Web service related functions
Virtual host (server)
Support keep-alive and pipeline connections (using one connection to make multiple requests)
Access log (supports log buffering to improve performance)
url rewirte
Path Aliases
Access control based on IP and user
Support rate limit and concurrent number limit
Reconfigure and upgrade online without interrupting the customer's work process
Nginx process structure
Nginx is a multi-process organization model, and is composed of a Master process and a Worker process.
Functions of the master process:
对外接口:接收外部的操作(信号)
对内转发:根据外部的操作的不同,通过信号管理 Worker
监控:监控 worker 进程的运行状态,worker 进程异常终止后,自动重启 worker 进程
读取Nginx 配置文件并验证其有效性和正确性
建立、绑定和关闭socket连接
按照配置生成、管理和结束工作进程
接受外界指令,比如重启、升级及退出服务器等指令
不中断服务,实现平滑升级,重启服务并应用新的配置
开启日志文件,获取文件描述符
不中断服务,实现平滑升级,升级失败进行回滚处理
编译和处理perl脚本
Functions of the worker process:
所有 Worker 进程都是平等的
实际处理:网络请求,由 Worker 进程处理
Worker进程数量:一般设置为核心数,充分利用CPU资源,同时避免进程数量过多,导致进程竞争CPU资源,
增加上下文切换的损耗
接受处理客户的请求
将请求依次送入各个功能模块进行处理
I/O调用,获取响应数据
与后端服务器通信,接收后端服务器的处理结果
缓存数据,访问缓存索引,查询和调用缓存数据
发送请求结果,响应客户的请求
接收主程序指令,比如重启、升级和退出等
Introduction to Nginx modules
Core module: It is an essential module for the normal operation of Nginx server, providing core functions such as error logging, configuration file parsing, event-driven mechanism, process management, etc.
Standard HTTP module: provides HTTP protocol parsing related functions, such as port configuration, web page encoding settings, HTTP response header settings, etc.
Optional HTTP module: mainly used to extend standard HTTP functions, allowing Nginx to handle some special services, such as: Flash multimedia transmission, parsing GeoIP requests, network transmission compression, security protocol SSL support, etc.
Mail service module: mainly used to support Nginx's mail service, including support for POP3 protocol, IMAP protocol and SMTP protocol
Stream service module: implements reverse proxy function, including TCP protocol proxy
Third-party modules: are used to extend Nginx server applications and complete developer-defined functions, such as Json support, Lua support, etc.
Nginx version
Mainline version is the main development version, usually an odd version number, such as 1.19
Stable version The latest stable version, usually an even-numbered version, such as 1.20
Legacy versions Old stable versions, usually even-numbered versions, such as: 1.18
Nginx can be installed using yum or source code, but it is recommended to compile and install using source code
The yum version is relatively old
Compiling and installing can make it easier to customize related paths. Compiling with source code can make it easier to customize related functions, making it easier to use in business.
Run the docker container directly
[root@Rocky8 ~]#vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[root@Rocky8 ~]#yum -y install nginx
[root@Rocky8 ~]#rpm -q nginx
nginx-1.26.1-2.el8.ngx.x86_64
Nginx is written in C language, so Nginx source code installation requires the preparation of a standard compiler in advance. GCC stands for GNU Compiler collection, which was developed by GNU and licensed under GPL or LGPL. It is the standard compiler for the free UNIX-like operating system of Apple's Mac OS X. Because GCC could only process C language, it was originally named GNU C language compiler. Later, it developed rapidly and can process other languages such as C++, Fortran, Pascal, Objective-C, Java and Ada. In addition, the Automake tool is required to complete the work of automatically creating Makefile. Some modules of Nginx need to rely on third-party libraries, such as: pcre (support rewrite), zlib (support gzip module) and openssl (support ssl module).
Official source package download address: https://nginx.org/en/download.html
# 安装依赖包
[root@Ubuntu2204 ~]#apt update
[root@Ubuntu2204 ~]#apt -y install gcc make libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev
[root@Ubuntu2204 ~]#useradd -r -s /sbin/nologin nginx
[root@Ubuntu2204 ~]#cd /usr/local/src/
[root@Ubuntu2204 src]#wget https://nginx.org/download/nginx-1.22.1.tar.gz
[root@Ubuntu2204 src]#tar xf nginx-1.22.1.tar.gz
[root@Ubuntu2204 src]#cd nginx-1.22.1/
[root@Ubuntu2204 nginx-1.22.1]#./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@Ubuntu2204 nginx-1.22.1]#make && make install
[root@Ubuntu2204 nginx-1.22.1]#chown -R nginx.nginx /apps/nginx
[root@Ubuntu2204 nginx-1.22.1]#ln -s /apps/nginx/sbin/nginx /usr/sbin/
# 启动nginx
[root@Ubuntu2204 nginx-1.22.1]#cd
[root@Ubuntu2204 ~]#nginx
[root@Ubuntu2204 ~]#ss -nlt
# 关闭nginx
[root@Ubuntu2204 ~]#nginx -s stop
[root@Ubuntu2204 ~]#ss -nlt
# 创建 Nginx 自启动文件
复制同一版本的nginx的yum安装生成的service文件
[root@Ubuntu2204 ~]#vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
# 创建pid文件存放的目录
[root@Ubuntu2204 ~]#mkdir /apps/nginx/run/
# 修改配置文件
[root@Ubuntu2204 ~]#vim /apps/nginx/conf/nginx.conf
pid /apps/nginx/run/nginx.pid;
# 验证 Nginx 自启动文件
[root@Ubuntu2204 ~]#systemctl daemon-reload
[root@Ubuntu2204 ~]#systemctl enable --now nginx.service
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
[root@Ubuntu2204 ~]#ll /apps/nginx/run/
-rw-r--r-- 1 root root 6 Jul 11 09:45 nginx.pid
[root@Ubuntu2204 ~]#ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@Ubuntu2204 ~]#systemctl stop nginx.service
[root@Ubuntu2204 ~]#systemctl status nginx.service
[root@Ubuntu2204 ~]#ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
# 验证 Nginx 自启动
[root@Ubuntu2204 ~]#reboot
[root@Ubuntu2204 ~]#ss -nlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
[root@Ubuntu2204 ~]#systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-07-11 09:48:58 CST; 1min 5s ago
Docs: https://nginx.org/en/docs/