-
Introduction to Nginx
-
Installation Methods
-
I/O Models and Zero-Copy Technology
-
Process Architecture
-
Configuration Optimization
-
Smooth Upgrade Procedures
-
Introduction to Nginx
Nginx is a high-performance web server developed in 2002 by Russian engineers for Rambler.ru. Acquired by F5 in 2019 for $670M, it's used by major internet companies like Taobao, JD.com, and Xiaomi. Key features include:
- Reverse proxy and load balancing
- Support for 20k+ concurrent connections
- HTTP/HTTPS/SMTP/POP3/IMAP protocol handling
- FastCGI/URL rewriting/Gzip support
- Stream module for TCP/UDP load balancing (1.9+)
- Installation Methods
2.1 Yum Installation
yum install epel-release -y
yum install nginx -y
2.2 Repository Configuration
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
2.3 Source Compilation
yum install gcc pcre-devel openssl-devel zlib-devel -y
./configure --prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module
make && make install
- I/O Models and Zero-Copy Technology
3.1 I/O Model Classification
- Blocking I/O: Process waits until I/O completes
- Non-blocking I/O: Immediate response with status checks
- I/O Multiplexing: select/poll/epoll for multiple sockets
- Signal-driven I/O: SIGIO signal notifications
- Asynchronous I/O: Kernel completes I/O before notification
3.2 Zero-Copy Implementation
Reduces data copying between kernel/user space through:
- sendfile() system call
- Memory mapping (mmap)
- Kernel-space data transfer
- Process Architecture
Nginx uses a master-worker process model:
- Master process: Manages configuration, socket binding, and worker processes
- Worker processes: Handle actual client requests
- Supports graceful shutdown (-s quit) and hot reloading (-s reload)
- Configuration Optimization
5.1 Core Configuration Parameters
worker_processes auto;
worker_cpu_affinity 00000001 00000010;
worker_rlimit_nofile 65536;
pid /opt/nginx/run/nginx.pid;
5.2 Performance Tuning
- CPU affinity binding for worker processes
- File descripotr limits configuration
- Epoll event model for Linux
- Keep-alive connnection optimizations
- Smooth Upgrade Procedures
# Download new version
wget https://nginx.org/download/nginx-1.20.2.tar.gz
tar xf nginx-1.20.2.tar.gz
cd nginx-1.20.2
# Recompile without installing
./configure --prefix=/opt/nginx ...
make
# Perform rolling upgrade
mv /opt/nginx/sbin/nginx /opt/nginx/sbin/nginx.old
cp objs/nginx /opt/nginx/sbin/
# Send USR2 signal to master process
kill -USR2 `cat /opt/nginx/logs/nginx.pid`
# Gracefully shut down old workers
kill -WINCH `cat /opt/nginx/logs/nginx.pid.oldbin`
6.1 Rollback Procedure
# Restore previous version
mv /opt/nginx/sbin/nginx.old /opt/nginx/sbin/nginx
# Restart with old binary
kill -HUP `cat /opt/nginx/logs/nginx.pid.oldbin`