Nginx 技术全解析:从基础架构到高级配置实践

Nginx 技术全解析:从基础架构到高级配置实践

一、Nginx 简介与核心能力

Nginx 是一款 高性能、事件驱动模型的 Web 服务器 / 反向代理服务器 / 负载均衡器,最初由 Igor Sysoev 设计,用于解决 C10K 问题。

1.1 常见发行版本图片

版本 说明
Nginx 官方版 稳定、通用场景
Tengine 阿里出品,适合大流量场景
OpenResty Nginx + Lua,API 网关 / WAF 常用

1.2 核心功能图片

  • 静态资源服务(HTML / CSS / JS / 图片)
  • HTTP / HTTPS 反向代理
  • TCP / UDP 四层代理
  • 负载均衡(多算法)
  • FastCGI(PHP-FPM)
  • URL Rewrite / 防盗链 / 鉴权
  • 高并发、低内存占用

二、Nginx 架构原理

2.1 Master / Worker 架构图片

Nginx 采用 多进程、事件驱动模型

1
2
3
4
Master Process
├── Worker Process 1
├── Worker Process 2
└── Worker Process N

Master 进程职责图片

  • 读取、校验配置文件
  • 管理 Worker 进程(创建 / 重启 / 回收)
  • 处理信号(reload / stop / upgrade)
  • 支持平滑升级、不中断服务

Worker 进程职责图片

  • 处理真实客户端请求
  • 基于 epoll 事件模型
  • Worker 数量 ≈ CPU 核心数

2.2 事件驱动模型图片

  • Linux:epoll
  • BSD:kqueue
  • 高并发、非阻塞 IO

三、Nginx 编译安装(生产推荐)

3.1 依赖环境图片

1
dnf install -y gcc pcre-devel zlib-devel openssl-devel

3.2 编译安装图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 创建用户
useradd -s /sbin/nologin -M nginx

# 解压
cd /usr/local/src
tar zxf nginx-1.26.1.tar.gz
cd nginx-1.26.1

# 配置
./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream

# 编译安装
make && make install

3.3 systemd 服务文件图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Nginx Server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID

[Install]
WantedBy=multi-user.target

四、Nginx 核心配置结构

4.1 nginx.conf 基本结构图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
user nginx;
worker_processes auto;

error_log logs/error.log warn;
pid logs/nginx.pid;

events {
use epoll;
worker_connections 65535;
accept_mutex on;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
keepalive_timeout 65;

include conf.d/*.conf;
}

五、虚拟主机与站点配置

5.1 基础 Web 站点图片

1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name www.example.com;

root /data/www/example;
index index.html index.htm;

location / {
try_files $uri $uri/ =404;
}
}

5.2 root 与 alias 区别图片

1
2
3
4
5
6
7
8
9
location /img/ {
root /data;
}
# /img/a.jpg -> /data/img/a.jpg

location /static/ {
alias /data/static/;
}
# /static/a.jpg -> /data/static/a.jpg

六、location 匹配规则(面试高频)

1
2
3
4
5
location = /index.html {}
location ^~ /static/ {}
location ~ \.php$ {}
location ~* \.(jpg|png)$ {}
location / {}

优先级:

= > ^~ > ~ / ~* > 普通前缀


七、Rewrite 与跳转控制

7.1 Rewrite Flags图片

flag 说明
last 内部重写,重新 location 匹配
break 停止 rewrite
redirect 302
permanent 301

7.2 HTTP 跳转 HTTPS(推荐写法)图片

1
2
3
4
5
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}

八、HTTPS 配置(生产级)

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 443 ssl http2;
server_name www.example.com;

ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/privkey.pem;

ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;

root /data/www/example;
index index.html;
}

九、反向代理配置

9.1 HTTP Proxy图片

1
2
3
4
5
location /api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

十、负载均衡 upstream

1
2
3
4
5
6
7
8
9
10
11
12
upstream web_backend {
least_conn;
server 10.0.0.1:80 weight=2;
server 10.0.0.2:80 max_fails=3 fail_timeout=30s;
}

server {
listen 80;
location / {
proxy_pass http://web_backend;
}
}

十一、Nginx 缓存配置

1
2
3
4
5
6
proxy_cache_path /data/cache levels=1:2 keys_zone=cache:50m max_size=5g;

location / {
proxy_cache cache;
proxy_cache_valid 200 10m;
}

十二、状态监控 stub_status

1
2
3
4
5
location /nginx_status {
stub_status;
allow 127.0.0.1;
deny all;
}

十三、四层代理(TCP / UDP)

1
2
3
4
5
6
7
8
9
10
stream {
upstream mysql_backend {
server 10.0.0.10:3306;
}

server {
listen 3306;
proxy_pass mysql_backend;
}
}

十四、FastCGI(PHP-FPM)

1
2
3
4
5
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

十五、生产优化建议

  • worker_processes auto
  • 开启 sendfile / gzip
  • 合理 keepalive
  • 隐藏版本号:server_tokens off;
  • 日志分离、定期轮转