Nginx 配置从入门到精通完全指南
Nginx(engine x)是一款高性能的 HTTP 和反向代理服务器,同时也是邮件代理服务器、TCP/UDP 代理服务器。它以高并发、低内存占用、稳定性强、配置灵活的特点,成为目前互联网行业 Web 服务、反向代理、负载均衡的首选方案,几乎是后端、运维、前端全栈开发者的必备技能。
本文从入门安装、核心配置结构,到企业级常用场景、性能优化、安全防护、排错实战,全面讲解 Nginx 配置的全链路知识,附完整可运行配置示例,新手可直接上手,老手可查漏补缺。
一、入门篇:Nginx 基础认知与安装
1. Nginx 核心优势与典型应用场景
核心优势
- 高并发:单台服务器可轻松支撑数万并发连接,性能远超 Apache
- 低资源占用:空载时内存占用仅几 MB,资源利用率极高
- 高稳定性:生产环境可连续数月无故障运行,宕机概率极低
- 热部署:不中断服务的情况下重载配置、升级版本
- 模块化设计:丰富的第三方模块,可扩展功能(缓存、限流、安全防护等)
典型应用场景
- 静态资源服务:托管前端页面、图片、视频等静态文件,性能远超后端服务
- 反向代理:隐藏后端服务,转发请求到上游服务,实现请求分发
- 负载均衡:将请求分发到多台后端服务器,实现流量分摊、高可用
- HTTPS 网关:统一管理 SSL 证书,实现 HTTPS 加密传输
- 缓存服务:缓存后端静态资源/接口数据,减少后端服务压力
- 限流/安全防护:实现接口限流、IP 黑白名单、防 CC 攻击、防盗链等
- 动静分离:静态资源由 Nginx 处理,动态请求转发到后端服务
2. Nginx 安装(3种方式)
方式1:包管理器安装(新手首选,简单快捷)
适合测试/生产环境快速部署,自动处理依赖,自带基础模块。
1 2 3 4 5 6 7 8
| yum install nginx -y
apt update && apt install nginx -y
nginx -v
|
方式2:源码编译安装(生产环境推荐,可自定义模块)
适合需要自定义模块、指定版本、开启高级功能的场景,和你之前的源码安装流程完全兼容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| yum install gcc gcc-c++ automake autoconf libtool make -y
yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
cd /usr/local/src wget http://nginx.org/download/nginx-1.26.1.tar.gz tar -zxvf nginx-1.26.1.tar.gz cd nginx-1.26.1
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/nginx.pid \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-stream
make && make install
echo "export PATH=\$PATH:/usr/local/nginx/sbin" >> /etc/profile source /etc/profile
nginx -v
|
方式3:Docker 安装(容器化场景)
1 2 3 4 5
| docker pull nginx:latest
docker run -d --name nginx -p 80:80 -v /your/conf:/etc/nginx/conf.d nginx:latest
|
3. Nginx 基础命令(必背)
所有命令均需在服务器终端执行,源码安装需确保环境变量配置正确。
| 命令 |
作用 |
适用场景 |
nginx |
启动 Nginx 服务 |
首次启动服务 |
nginx -s stop |
强制停止 Nginx(立即终止进程) |
服务异常需强制关闭 |
nginx -s quit |
优雅停止 Nginx(处理完请求后退出) |
正常停止服务 |
nginx -s reload |
平滑重载配置(不中断服务) |
修改配置后生效(生产环境首选) |
nginx -s reopen |
重新打开日志文件 |
日志切割/轮转 |
nginx -t |
检查配置文件语法是否正确 |
修改配置后、重载前必执行 |
nginx -T |
检查配置并打印完整加载的配置 |
排查配置加载问题 |
nginx -V |
查看版本、编译参数、已启用模块 |
确认模块是否安装 |
nginx -c /path/to/nginx.conf |
指定配置文件启动 |
多配置文件测试 |
二、核心篇:Nginx 配置结构与核心指令
Nginx 的配置由指令块(Block)和指令(Directive)组成,采用层级嵌套结构,核心配置文件为 nginx.conf(包管理器安装默认在 /etc/nginx/nginx.conf,源码安装在 /usr/local/nginx/conf/nginx.conf)。
1. 配置文件整体结构
Nginx 配置分为6大核心块,层级关系从外到内为:全局块 → events块 → http块 → server块 → location块 → 嵌套子块,结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;
events { use epoll; worker_connections 1024; multi_accept on; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
server { listen 80; server_name localhost; root /usr/share/nginx/html;
location / { index index.html index.htm; }
error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
|
2. 核心块详解
(1)全局块
作用于 Nginx 服务全局,核心配置项:
user:运行 Nginx 的用户,建议使用专用的 nginx 用户,避免 root 权限带来的安全风险
worker_processes:工作进程数,最佳实践设为 auto,自动匹配 CPU 核心数,最大化利用多核性能
error_log:错误日志配置,日志级别从低到高:debug → info → notice → warn → error → crit,生产环境建议 warn 级别
daemon on:是否以守护进程方式运行,默认开启,生产环境无需修改
(2)events块
控制 Nginx 与客户端的连接处理,核心配置项:
use epoll:事件驱动模型,Linux 系统强制推荐 epoll,FreeBSD 用 kqueue,是高并发的核心
worker_connections:单个工作进程的最大连接数,默认1024,生产环境可设为 20480,最大不能超过系统的最大文件打开数
worker_rlimit_nofile:单个工作进程的最大文件打开数,需配合系统配置,建议设为 65535
multi_accept on:允许进程一次性接收所有新的连接,提升并发处理能力
(3)http块
所有 HTTP 服务的全局配置,可包含多个 server 块,核心配置项:
include mime.types:引入文件类型映射,让 Nginx 识别不同后缀的文件
log_format:自定义访问日志格式,可按需添加字段(如请求耗时、响应时间、真实IP等)
access_log:访问日志路径,可关闭 access_log off; 提升静态资源性能
sendfile on:开启零拷贝传输,静态资源传输性能提升数倍,必须开启
keepalive_timeout:长连接保持时间,网站建议65s,接口服务可适当缩短
include:引入子配置文件,生产环境最佳实践:每个站点单独写一个 .conf 文件,通过 include 引入,避免主配置文件过于臃肿
(4)server块(虚拟主机)
一个 server 块对应一个站点/服务,核心配置项:
listen:监听端口,默认80,HTTPS默认443,可指定IP listen 192.168.1.100:80;
server_name:站点域名,支持精确匹配、通配符匹配、正则匹配:
- 精确匹配:
server_name www.example.com;
- 通配符匹配:
server_name *.example.com;、server_name www.example.*;
- 正则匹配:
server_name ~^(?<user>.+)\.example\.com$;
root:站点根目录,对应站点文件存放的系统路径
index:默认首页文件,优先级从左到右
error_page:自定义错误页,如404、500错误的跳转页面
(5)location块(URI匹配)
Nginx 配置的灵魂,用于匹配客户端请求的 URI,执行对应的处理逻辑,是最常用、最灵活的配置块。
① location 匹配语法
1 2 3
| location [匹配模式] 匹配规则 { }
|
② 匹配模式优先级(从高到低)
| 匹配模式 |
含义 |
示例 |
优先级 |
= |
精确匹配,完全相等才命中 |
location = /login |
1(最高) |
^~ |
前缀匹配,匹配成功后不再进行正则匹配 |
location ^~ /static/ |
2 |
~ |
正则匹配(区分大小写) |
`location ~ .(jpg |
png)$` |
~* |
正则匹配(不区分大小写) |
`location ~* .(jpg |
png)$` |
| 无修饰符 |
普通前缀匹配,优先级低于正则 |
location /api/ |
4 |
/ |
通用匹配,所有未命中的请求都会匹配 |
location / |
5(最低) |
③ 匹配示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| location = /login { return 200 "登录页面"; }
location ^~ /static/ { root /data/website; expires 30d; }
location ~* \.(jpg|png|gif)$ { root /data/website/images; expires 7d; }
location /api/ { proxy_pass http://127.0.0.1:3000; }
location / { root /data/website/html; index index.html; }
|
④ 高频踩坑:root 与 alias 的区别
这是新手最容易踩坑的点,两者都是用于指定文件路径,但逻辑完全不同:
| 指令 |
作用逻辑 |
示例 |
最终访问路径 |
root |
根路径拼接:root路径 + location匹配的URI |
location /static/ { root /data/; },访问 /static/logo.png |
/data/static/logo.png |
alias |
别名替换:alias路径 替换 location匹配的URI |
location /static/ { alias /data/; },访问 /static/logo.png |
/data/logo.png |
最佳实践:
- 站点根目录用
root,location 子路径匹配优先用 alias
alias 路径结尾必须加 /,root 可加可不加
alias 只能在 location 块中使用,root 可在 server、location、http 块中使用
三、入门实战:3个必学基础场景配置
场景1:静态资源服务(前端站点/文件托管)
这是 Nginx 最基础的用法,托管前端打包后的页面、图片、视频等静态文件,性能远超 Tomcat、Node.js 等后端服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| server { listen 80; server_name www.example.com; root /data/website/dist; index index.html index.htm;
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff2)$ { expires 30d; add_header Cache-Control "public, max-age=2592000, immutable"; access_log off; }
location / { try_files $uri $uri/ /index.html; }
location /download/ { root /data/; autoindex on; autoindex_exact_size off; autoindex_localtime on; } }
|
场景2:虚拟主机配置(一台服务器部署多个站点)
通过 server_name 区分不同站点,一台服务器可部署多个域名的网站,无需额外端口。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| server { listen 80; server_name www.example.com; root /data/website/example; index index.html; }
server { listen 80; server_name www.test.com; root /data/website/test; index index.html; }
server { listen 8080; server_name localhost; root /data/website/port8080; index index.html; }
|
场景3:反向代理(隐藏后端服务,请求转发)
反向代理是 Nginx 最核心的用法之一,隐藏后端服务地址,统一入口,同时可实现负载均衡、缓存、HTTPS 统一管理。
最经典的场景:Nginx 监听80端口,将 /api 开头的请求转发到后端 Node.js/Java/Python 服务,和你之前的需求完全匹配。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| server { listen 80; server_name www.example.com;
location / { root /data/website/dist; index index.html; }
location /api/ { proxy_pass http://127.0.0.1:3000/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 30s; proxy_read_timeout 60s; proxy_send_timeout 60s; proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 4 32k; } }
|
高频踩坑:proxy_pass 结尾加不加 / 的区别
proxy_pass http://127.0.0.1:3000/;(结尾加/):访问 /api/user 会转发到 http://127.0.0.1:3000/user
proxy_pass http://127.0.0.1:3000;(结尾不加/):访问 /api/user 会转发到 http://127.0.0.1:3000/api/user
核心逻辑:结尾加 / 会把 location 匹配的路径替换掉,不加则完整拼接。
四、进阶实战:企业级核心场景配置
场景1:负载均衡(多台后端服务流量分摊)
通过 upstream 块定义后端服务池,将请求分发到多台服务器,实现高可用、水平扩容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| upstream backend_server { server 192.168.1.101:8080 weight=2; server 192.168.1.102:8080 weight=1; server 192.168.1.103:8080 backup; server 192.168.1.104:8080 down;
}
server { listen 80; server_name www.example.com;
location /api/ { proxy_pass http://backend_server/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
proxy_next_upstream error timeout http_500 http_502 http_503 http_504; proxy_next_upstream_tries 3; }
|
场景2:HTTPS 配置(SSL证书部署+HTTP强制跳转HTTPS)
生产环境必备,实现 HTTPS 加密传输,提升网站安全性和SEO排名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| server { listen 80; server_name www.example.com example.com; return 301 https://$server_name$request_uri; }
server { listen 443 ssl http2; server_name www.example.com;
ssl_certificate /etc/nginx/ssl/www.example.com.pem; ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 1d; ssl_session_tickets off;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
root /data/website/dist; index index.html;
location / { try_files $uri $uri/ /index.html; }
location /api/ { proxy_pass http://backend_server/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
|
场景3:URL重写(rewrite):伪静态/跳转/路径重写
rewrite 指令用于修改请求的 URI,实现伪静态、域名跳转、路径重写、SEO优化等需求。
语法
1
| rewrite 正则表达式 替换内容 [flag标记];
|
flag标记(核心)
| 标记 |
作用 |
适用场景 |
last |
停止当前rewrite,用新的URI重新匹配location |
内部路径重写,伪静态 |
break |
停止rewrite,不再匹配location |
路径替换,不改变浏览器地址 |
redirect |
302临时重定向,地址栏显示新地址 |
临时页面跳转 |
permanent |
301永久重定向,地址栏显示新地址 |
永久域名/路径变更,SEO优化 |
常用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| server { listen 80; server_name example.com;
rewrite ^/(.*)$ https://www.example.com/$1 permanent;
rewrite ^/article/(\d+)\.html$ /article.php?id=$1 last;
if ($host = 'example.com') { rewrite ^/(.*)$ https://www.example.com/$1 permanent; }
if ($http_user_agent ~* (mobile|android|iphone|ipad)) { rewrite ^/(.*)$ https://m.example.com/$1 redirect; }
location ~* \.(jpg|png|gif)$ { valid_referers none blocked www.example.com; if ($invalid_referer) { rewrite ^/ https://www.example.com/403.jpg break; } } }
|
五、高级篇:性能优化与高级功能
1. 缓存配置:proxy_cache 反向代理缓存
缓存后端服务的静态资源/接口数据,减少后端请求压力,提升响应速度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| http { proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2 keys_zone=api_cache:100m max_size=10g inactive=60m use_temp_path=off;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment; proxy_no_cache $http_pragma $http_authorization; proxy_cache_min_uses 3; proxy_cache_valid 200 304 1h; proxy_cache_valid 404 1m; }
server { listen 80; server_name www.example.com;
location /api/ { proxy_pass http://backend_server/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
proxy_cache api_cache; proxy_cache_key $host$uri$is_args$args; add_header X-Cache-Status $upstream_cache_status always; } }
|
2. 限流配置:防CC攻击/接口限流
通过 limit_req(请求限流)和 limit_conn(连接限流),实现对客户端的访问频率控制,防止恶意攻击、接口刷取。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| http { limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
limit_conn_zone $binary_remote_addr zone=conn_limit:10m; }
server { listen 80; server_name www.example.com;
limit_req zone=req_limit burst=20 nodelay; limit_conn conn_limit 20;
location /api/login { proxy_pass http://backend_server/login; limit_req zone=req_limit burst=3 nodelay; limit_conn conn_limit 5; } }
|
3. 动静分离
静态资源(CSS/JS/图片/视频)由 Nginx 直接处理,动态请求转发到后端服务,最大化利用 Nginx 的高性能,减少后端服务压力。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| server { listen 80; server_name www.example.com; root /data/website;
location /api/ { proxy_pass http://127.0.0.1:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff2|mp4)$ { expires 30d; add_header Cache-Control "public, max-age=2592000"; access_log off; }
location / { index index.html; } }
|
4. Gzip/Brotli 压缩
开启压缩,减少传输文件体积,提升页面加载速度。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| http { gzip on; gzip_min_length 1k; gzip_buffers 16 64k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain text/css application/javascript text/xml application/xml application/xml+rss application/json; gzip_vary on; gzip_disable "MSIE [1-6]\.";
}
|
六、安全篇:Nginx 安全防护配置
1. 基础安全加固
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| user nginx;
http { server_tokens off;
add_header X-XSS-Protection "1; mode=block" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'" always;
location ~* \.(env|git|svn|bak|sql|conf|sh)$ { return 403; access_log off; }
location ~ \.\.\/ { return 403; } }
|
2. IP黑白名单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 80; server_name www.example.com;
deny 192.168.1.100; deny 10.0.0.0/8; allow 192.168.1.0/24; deny all;
location /admin/ { allow 192.168.1.100; deny all; proxy_pass http://backend_server/admin/; } }
|
3. 防盗链配置
防止其他网站盗用本站的图片、视频等静态资源,节省服务器带宽。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 80; server_name www.example.com;
location ~* \.(jpg|png|gif|jpeg|mp4|flv|avi)$ { valid_referers none blocked server_names *.example.com *.baidu.com *.google.com; if ($invalid_referer) { return 403; } expires 30d; access_log off; } }
|
七、排错篇:日志管理与常见错误排查
1. 日志管理
Nginx 有两个核心日志:访问日志(access_log)和错误日志(error_log),是排查问题的核心依据。
(1)访问日志
记录所有客户端的请求信息,用于统计、分析、排查访问问题,可自定义格式。
1 2 3 4 5 6 7 8
| log_format main '$remote_addr [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $request_time $upstream_response_time';
access_log /var/log/nginx/www.example.com.access.log main;
access_log off;
|
核心字段说明:
$remote_addr:客户端IP
$time_local:请求时间
$request:请求方法+URI+HTTP版本
$status:响应状态码
$request_time:Nginx处理请求的总耗时
$upstream_response_time:后端服务响应耗时
(2)错误日志
记录 Nginx 启动、运行、请求处理过程中的错误,是排查500/502/504等错误的核心,日志级别从低到高:debug → info → notice → warn → error → crit。
1 2 3 4
| error_log /var/log/nginx/error.log error;
|
2. 常见错误排查
| 错误码 |
核心原因 |
排查方向 |
| 403 Forbidden |
权限不足/访问被禁止 |
1. 站点目录权限(Nginx运行用户是否有读权限) 2. index文件不存在 3. IP黑名单/防盗链拦截 4. SELinux开启导致权限限制 |
| 404 Not Found |
资源不存在 |
1. 请求的URI对应的文件/路径不存在 2. root/alias路径配置错误 3. try_files配置错误 4. 反向代理地址错误 |
| 502 Bad Gateway |
后端服务不可达 |
1. 后端服务宕机/端口未监听 2. proxy_pass地址错误 3. 后端服务端口被防火墙拦截 4. upstream中所有节点都下线 |
| 504 Gateway Timeout |
后端服务响应超时 |
1. 后端服务执行时间过长,超过proxy_read_timeout 2. 后端服务死锁/数据库查询超时 3. 服务器CPU/内存满载,无法处理请求 4. 网络不通,Nginx无法连接后端 |
| 500 Internal Server Error |
Nginx内部错误/后端服务错误 |
1. Nginx配置语法错误(重载前用nginx -t检查) 2. rewrite规则死循环 3. 后端服务返回500 4. 文件权限错误 5. 内存不足 |
3. 配置排查必备命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| nginx -t
nginx -T
nginx -V
nginx -s reload
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
|
八、生产环境最佳实践
- 配置规范:每个站点单独创建一个
.conf 配置文件,通过 include 引入,禁止所有配置都写在 nginx.conf 主文件中,便于管理和排查。
- 权限安全:禁止用 root 用户运行 Nginx,创建专用的 nginx 用户,站点目录权限设置为
755,文件权限 644,避免过高权限。
- 版本管理:生产环境使用 Nginx 稳定版(偶数版本,如1.24.x、1.26.x),禁止使用开发版,定期更新修复安全漏洞。
- 重载规范:修改配置后必须先执行
nginx -t 检查语法,确认无误后用 nginx -s reload 平滑重载,禁止用 restart 重启服务,避免中断业务。
- 日志管理:配置日志轮转(logrotate),避免日志文件过大占满磁盘,定期归档访问日志,错误日志长期保留用于排查问题。
- 性能优化:开启
sendfile、tcp_nopush、tcp_nodelay,配置合理的 worker_processes 和 worker_connections,开启 gzip 压缩和静态资源缓存。
- 安全加固:隐藏版本号,开启 HTTPS 和 HSTS,配置 IP 黑白名单、防盗链、限流,定期扫描安全漏洞。
- 高可用:生产环境用 Keepalived + Nginx 实现主备高可用,避免单点故障,负载均衡配置健康检查和故障重试。
总结
Nginx 的核心能力在于灵活的配置、极致的性能、丰富的模块化扩展,从入门的静态资源服务、反向代理,到进阶的负载均衡、HTTPS配置,再到高级的缓存、限流、安全防护,几乎覆盖了 Web 服务的全场景需求。
本文的配置示例均为生产环境可直接复用的标准配置,新手建议先从静态服务、反向代理入手,逐步掌握 location 匹配、rewrite 重写、负载均衡等进阶内容,最终结合性能优化和安全防护,实现企业级的 Nginx 配置。