Nginx 服务器从入门到实践 —— 完整学习笔记
🎯 目标读者:想要系统学习 Nginx 的运维工程师、后端开发者 📦 内容覆盖:基础概念 → 安装部署 → 核心配置 → 反向代理 → 负载均衡 → HTTPS → 实战案例 ⏱️ 预计阅读时间:30 分钟
目录
- 一、Nginx 是什么?
- 二、Nginx 的架构之美
- 三、安装与基础命令
- 四、配置文件全景解析
- 五、静态 Web 服务器
- 六、虚拟主机
- 七、反向代理
- 八、Location 匹配规则
- 九、URL 重写
- 十、负载均衡
- 十一、HTTPS 与 SSL/TLS
- 十二、Basic 认证
- 十三、PHP 集成(FastCGI)
- 十四、常用变量与日志
- 十五、常见问题排查
一、Nginx 是什么?
Nginx(发音 “engine-x”)是一款由俄罗斯工程师 Igor Sysoev 开发的高性能 HTTP 和反向代理服务器。它以高并发、低内存消耗、事件驱动著称,一台服务器即可支撑数万并发连接。
🚀 Nginx 的三大核心能力
| Web 服务器 | 直接提供静态文件(HTML/JS/CSS/图片) | 前端项目部署 |
| 反向代理 | 作为"中间人"转发请求到后端服务 | 前后端分离架构 |
| 负载均衡器 | 将流量分发到多台后端服务器 | 高可用集群 |
💡 为什么 Nginx 这么牛?
Apache 模型:每个请求 → fork 一个进程/线程 → 处理完销毁
↓
1万并发 = 1万个进程 = 💥内存爆炸
Nginx 模型:1 个 Master + N 个 Worker(每个 Worker 单线程异步处理)
↓
1万并发 = 几个 Worker 轻松应对 = ✅稳如泰山
核心秘诀是 epoll/kqueue 网络 I/O 模型:
- 同步非阻塞,一个 Worker 可以同时处理成千上万个连接
- 不像 Apache 那样"一个请求一个进程",而是像餐厅里一个服务员同时服务多张桌子
- worker_processes auto; 自动匹配 CPU 核数
二、Nginx 的架构之美
Nginx 采用经典的 Master-Worker 多进程架构:
┌─────────────────────────────────────┐
│ Master 进程 │
│ (以 root 运行,负责管理) │
│ • 读取配置 │
│ • 绑定端口 │
│ • 创建/管理 Worker │
│ • 处理 reload 信号 │
└──────────┬──────────┬────────────────┘
│ │
┌──────▼──┐ ┌────▼──────┐
│ Worker1 │ │ Worker2 │ …
│ (处理请求)│ │ (处理请求) │
│ 单线程 │ │ 单线程 │
│ 异步IO │ │ 异步IO │
└─────────┘ └──────────┘
热重载(Graceful Reload)原理
执行 nginx -s reload 时发生了什么?
1. Master 收到 reload 信号
2. Master 加载新配置,创建一批新的 Worker
3. 旧 Worker 继续处理完手头的请求
4. 旧 Worker 处理完毕后优雅退出
5. 新 Worker 开始接收新请求
↓
整个过程不中断服务!✨
📌 关键配置:worker_processes auto; 自动设置为 CPU 核心数,不多不少刚刚好。
三、安装与基础命令
3.1 实验环境
| nginx-server | 10.1.8.10/24 | Nginx 服务器 |
| nginx-client | 10.1.8.11/24 | 测试客户端 |
3.2 网络配置
# === nginx-server ===
hostnamectl set-hostname nginx-server
nmcli connection modify ens33 ipv4.method manual \\
ipv4.addresses 10.1.8.10/24 ipv4.gateway 10.1.8.2 ipv4.dns 10.1.8.2 autoconnect yes
nmcli connection up ens33
# === nginx-client ===
hostnamectl set-hostname nginx-client
nmcli connection modify ens33 ipv4.method manual \\
ipv4.addresses 10.1.8.11/24 ipv4.gateway 10.1.8.2 ipv4.dns 10.1.8.2 autoconnect yes
nmcli connection up ens33
3.3 安装 Nginx
# 添加 EPEL 仓库
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装
yum -y install nginx
# 启动并设置开机自启
systemctl enable nginx –now
# 开放防火墙
firewall-cmd –add-service=http –permanent
firewall-cmd –reload
3.4 Hello World
# 备份默认首页
mv /usr/share/nginx/html/index.html{,.ori}
# 写一个测试页面
echo "Hello World From Nginx" > /usr/share/nginx/html/index.html
配置本地 hosts:
# Windows: C:\\Windows\\System32\\drivers\\etc\\hosts
# Linux/Mac: /etc/hosts
10.1.8.10 www.laogao.cloud
测试:
curl http://www.laogao.cloud
# 输出: Hello World From Nginx 🎉
3.5 常用命令速查
| nginx | 启动 Nginx |
| nginx -s stop | 快速停止(强制) |
| nginx -s quit | 优雅退出(等请求处理完) |
| nginx -s reload | 热重载配置(不中断服务) |
| nginx -t | 测试配置文件语法 |
| nginx -V | 查看编译参数和版本 |
四、配置文件全景解析
Nginx 的主配置文件位于 /etc/nginx/nginx.conf,结构层次如下:
main(全局)
├── events(事件驱动配置)
└── http(HTTP 服务)
├── server(虚拟主机 1)
│ └── location(URL 路由)
├── server(虚拟主机 2)
│ └── location
└── upstream(负载均衡池)
4.1 全局配置段
user nginx; # Worker 进程运行用户
worker_processes auto; # 自动匹配 CPU 核数
error_log /var/log/nginx/error.log; # 错误日志
pid /run/nginx.pid; # 进程 ID 文件
include /usr/share/nginx/modules/*.conf; # 加载模块配置
4.2 Events 事件段
events {
worker_connections 1024; # 每个 Worker 最大连接数
use epoll; # Linux 下推荐 epoll 模型
multi_accept on; # 一次接受多个新连接
}
📐 并发计算:最大并发 = worker_processes × worker_connections 例如:4 核 × 1024 = 4096 并发
4.3 HTTP 段(核心配置)
http {
include /etc/nginx/mime.types; # MIME 类型映射
default_type application/octet-stream; # 默认 MIME
# 访问日志格式
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; # 配合 sendfile,减少网络包数量
tcp_nodelay on; # 禁用 Nagle 算法,降低延迟
keepalive_timeout 65; # HTTP 长连接超时
types_hash_max_size 4096; # MIME 类型哈希表大小
# 引入子配置文件
include /etc/nginx/conf.d/*.conf;
# 默认虚拟主机
server {
listen 80;
listen [::]:80;
server_name _; # 匹配所有域名
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html { }
error_page 500 502 503 504 /50x.html;
location = /50x.html { }
}
}
五、静态 Web 服务器
最基础的 Nginx 用法——直接提供静态文件。
server {
listen 80;
server_name static.test.com;
root /data/static;
index index.html;
}
🎯 最佳实践:sendfile on; 利用零拷贝技术,静态文件传输效率极高。
六、虚拟主机
一台服务器跑多个网站?虚拟主机来搞定!
6.1 基于域名的虚拟主机
server {
server_name web1.laogao.cloud;
root /usr/share/nginx/web1;
}
server {
server_name web2.laogao.cloud;
root /usr/share/nginx/web2;
}
mkdir /usr/share/nginx/web{1,2}
echo "web1.laogao.cloud" > /usr/share/nginx/web1/index.html
echo "web2.laogao.cloud" > /usr/share/nginx/web2/index.html
systemctl restart nginx
# 测试
curl http://web1.laogao.cloud/ # → web1.laogao.cloud
curl http://web2.laogao.cloud/ # → web2.laogao.cloud
6.2 基于端口的虚拟主机
server {
listen 8081;
server_name www.laogao.cloud;
root /usr/share/nginx/8081;
}
server {
listen 8082;
server_name www.laogao.cloud;
root /usr/share/nginx/8082;
}
curl http://www.laogao.cloud:8081 # → 8081
curl http://www.laogao.cloud:8082 # → 8082
七、反向代理
7.1 什么是反向代理?
用户 → Nginx(代理)→ 后端服务器(Tomcat/SpringBoot/Node/PHP)
↑
"中间商赚差价"——隐藏后端、统一入口、安全加层
类比:你给公司前台打电话,前台帮你转接到具体部门。你只知道前台号码,不关心内部怎么转。前台就是反向代理。
7.2 基础配置
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $http_host; # 传递原始域名
proxy_set_header X-Real-IP $remote_addr; # 传递客户端真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 传递代理链
}
7.3 proxy_pass 的 / 玄学——必须搞懂!
这是最容易踩坑的地方!
# 场景一:proxy_pass 末尾带 /
location /api/ {
proxy_pass http://192.168.1.102:9090/;
}
# 请求: http://localhost/api/user/list
# 转发: http://192.168.1.102:9090/user/list ← /api 被"剪掉"了
# 场景二:proxy_pass 末尾不带 /
location /api/ {
proxy_pass http://192.168.1.102:9090;
}
# 请求: http://localhost/api/user/list
# 转发: http://192.168.1.102:9090/api/user/list ← /api 被"保留"
🔑 口诀:带 / 就"剪掉"前缀;不带 / 就"保留"前缀。
八、Location 匹配规则
Location 是 Nginx 的路由核心,决定了"哪个 URL 走哪个处理逻辑"。
8.1 匹配优先级(从高到低)
优先级 1 (最高): location = /uri # 精确匹配
优先级 2: location ^~ /uri # 前缀匹配(不检查正则)
优先级 3: location ~ regex # 区分大小写的正则
优先级 4: location ~* regex # 不区分大小写的正则
优先级 5: location /uri # 普通前缀匹配
优先级 6 (最低): location / # 通用匹配(兜底)
8.2 各类型实战
# 1️⃣ 精确匹配:只匹配 /login,/login?a=1 和 /login/ 都不匹配
location = /login {
proxy_pass http://backend_login:8080;
}
# 2️⃣ 前缀优先:/static/ 开头全部命中,不检查正则
location ^~ /static/ {
proxy_pass http://backend_static:80;
}
# 3️⃣ 正则匹配(区分大小写):匹配 .jpg/.png/.gif 结尾
location ~ \\.(jpg|png|gif)$ {
proxy_pass http://backend_img:80;
}
# 4️⃣ 正则匹配(不区分大小写):匹配 .JPG 也匹配 .jpg
location ~* \\.(jpg|png|gif)$ {
proxy_pass http://backend_img:80;
}
# 5️⃣ 普通前缀:/api/ 开头即可
location /api/ {
proxy_pass http://backend_api:9090;
}
# 6️⃣ 兜底匹配:上面都没命中时
location / {
proxy_pass http://backend_main;
}
8.3 完整示例
upstream backend_main { server 192.168.1.200:8080; }
upstream backend_api { server 192.168.1.201:9090; }
upstream backend_static { server 192.168.1.202:80; }
upstream backend_login { server 192.168.1.203:8080; }
server {
listen 80;
server_name localhost;
location = /login { # 精确匹配登录
proxy_pass http://backend_login;
proxy_set_header Host $host;
}
location ^~ /static/ { # 静态资源前缀
proxy_pass http://backend_static/;
proxy_set_header Host $host;
}
location ~* \\.(jpg|png|gif)$ { # 图片正则
proxy_pass http://backend_static;
proxy_set_header Host $host;
}
location /api/ { # API 前缀
proxy_pass http://backend_api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location / { # 兜底
proxy_pass http://backend_main;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
九、URL 重写
rewrite 指令可以在 Nginx 层面改写 URL。
9.1 基本语法
rewrite regex replacement [flag];
| last | 重写后重新走一遍 location 匹配 |
| break | 重写后不再匹配后续规则 |
| redirect | 返回 302 临时重定向 |
| permanent | 返回 301 永久重定向 |
9.2 实战案例
# 将 /nginx1/xxx 和 /nginx2/xxx 剥离前缀后转发
location ~ /nginx[12].* {
rewrite ^/nginx[12](.*)$ $1 break; # 去掉 /nginx1 或 /nginx2 前缀
proxy_pass http://nginx2.laogao.cloud;
index index.html;
}
# 请求: http://www.laogao.cloud/nginx1/index.html
# 转发: http://nginx2.laogao.cloud/index.html
十、负载均衡
10.1 基本概念
负载均衡 = 把流量均匀分发到多台后端服务器,既提升承载能力,又实现高可用。
┌─────────────┐
用户 → Nginx│ 负载均衡器 │→ nginx1 (10.1.8.21)
│ │→ nginx2 (10.1.8.22)
│ │→ nginx3 (10.1.8.23)
└─────────────┘
10.2 upstream 基础配置
upstream backends {
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
server {
listen 80;
server_name www.laogao.cloud;
location / {
proxy_pass http://backends/;
}
}
10.3 六种负载均衡算法
1️⃣ 轮询(Round Robin)—— 默认
一人一次,绝对公平:
upstream backends {
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
# 测试 90 次请求
for i in {1..90}; do curl http://10.1.8.20 -s; done | sort | uniq -c
# 结果:
# 30 Welcome to nginx1
# 30 Welcome to nginx2
# 30 Welcome to nginx3
2️⃣ 加权轮询(Weight)—— 能者多劳
给性能强的服务器分配更多流量:
upstream backends {
server nginx1.laogao.cloud:80 weight=10;
server nginx2.laogao.cloud:80 weight=20;
server nginx3.laogao.cloud:80 weight=30;
}
# 测试 60 次请求
# 10 nginx1(10/60 ≈ 16.7%)
# 20 nginx2(20/60 ≈ 33.3%)
# 30 nginx3(30/60 ≈ 50%)
3️⃣ IP Hash —— 会话保持
同一个客户端 IP 始终路由到同一台后端,解决 Session 问题:
upstream backends {
ip_hash;
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
# 测试 60 次请求 —— 全部落在同一台!
# 60 Welcome to nginx2
⚠️ 注意:如果用户经过 NAT 或代理,IP 可能相同,导致流量倾斜。
4️⃣ Generic Hash —— 自定义哈希键
根据 URL、Cookie 等任意变量做哈希:
upstream backends {
hash $request_uri; # 同一个 URL 始终落到同一台后端
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
5️⃣ Least Connections —— 最小连接数
把请求发给当前连接最少的那台:
upstream backends {
least_conn;
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
📊 适合请求处理时间不均衡的场景(有的请求快、有的请求慢)。
6️⃣ Least Time(Nginx Plus 商业版)
根据响应时间分配,智能但需要付费:
upstream backends {
least_time header; # 或 last_byte
server nginx1.laogao.cloud:80;
server nginx2.laogao.cloud:80;
server nginx3.laogao.cloud:80;
}
10.4 upstream 完整参数
upstream backends {
keepalive 32; # 保持到后端的空闲连接数
server nginx1.laogao.cloud:80 max_fails=3 fail_timeout=30s;
server nginx2.laogao.cloud:80 max_fails=3 fail_timeout=30s weight=2;
server nginx3.laogao.cloud:80 max_fails=3 fail_timeout=30s backup;
server nginx4.laogao.cloud:80 max_fails=3 fail_timeout=30s down;
}
| weight=N | 权重,默认 1 |
| max_fails=N | 失败 N 次后标记为不可用 |
| fail_timeout=T | 失败超时时间窗口 |
| backup | 备用服务器,只有其他都挂了才启用 |
| down | 手动下线,不参与负载 |
| keepalive N | Worker 到后端的长连接数 |
10.5 算法选择决策图
需要会话保持?
├── 是 → ip_hash(最简单)或 generic hash(更灵活)
└── 否 → 服务器性能不同?
├── 是 → weight(加权轮询)
└── 否 → 请求耗时差异大?
├── 是 → least_conn
└── 否 → round-robin(默认即可)
十一、HTTPS 与 SSL/TLS
11.1 自签名证书生成(三步走)
mkdir certs && cd certs
# Step 1: 生成私钥
openssl genrsa -out www.key 2048
# Step 2: 生成证书签名请求 (CSR)
# CN= 后面填你的域名!
openssl req -new -key www.key -out www.csr \\
-subj "/C=CN/ST=JS/L=NJ/O=LM/OU=DEVOPS/CN=www.laogao.cloud/emailAddress=admin@laogao.cloud"
# Step 3: 自签名颁发证书(有效期 3650 天)
openssl x509 -req -days 3650 -in www.csr -signkey www.key -out www.crt
生成的文件说明:
| www.key | 私钥 | 服务器端保密,切勿泄露 |
| www.csr | 证书请求 | 提交给 CA 签发 |
| www.crt | 证书 | 部署到 Nginx |
11.2 配置 HTTPS
# 将证书移动到安全目录
mkdir /etc/ssl/certs/www.laogao.cloud
mv www* /etc/ssl/certs/www.laogao.cloud
# 创建 HTTPS 配置
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.laogao.cloud;
root /usr/share/nginx/html;
# SSL 证书
ssl_certificate /etc/ssl/certs/www.laogao.cloud/www.crt;
ssl_certificate_key /etc/ssl/certs/www.laogao.cloud/www.key;
# SSL 优化
ssl_protocols TLSv1.2 TLSv1.3; # 只启用安全协议
ssl_ciphers HIGH:!aNULL:!MD5; # 强加密套件
ssl_prefer_server_ciphers on; # 优先服务器端加密顺序
ssl_session_cache shared:SSL:10m; # 会话缓存
ssl_session_timeout 10m; # 会话超时
}
# HTTP → HTTPS 自动跳转
server {
listen 80;
listen [::]:80;
server_name www.laogao.cloud;
return 301 https://$host$request_uri; # 301 永久重定向
}
开放 HTTPS 端口:
firewall-cmd –add-service=https –permanent
firewall-cmd –reload
11.3 测试验证
curl http://www.laogao.cloud/
# → 301 Moved Permanently(自动跳转)
curl -k https://www.laogao.cloud
# → Hello World From Nginx(-k 忽略自签名证书警告)
curl -Lk http://www.laogao.cloud
# → Hello World From Nginx(-L 跟随重定向)
十二、Basic 认证
给敏感页面加个简单的密码保护。
12.1 安装工具并创建密码文件
yum -y install httpd-tools
# 创建用户 laogao,密码 123456
htpasswd -b -c /etc/nginx/.htpasswd laogao 123456
12.2 Nginx 配置
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.laogao.cloud;
root /usr/share/nginx/html;
ssl_certificate /etc/ssl/certs/www.laogao.cloud/www.crt;
ssl_certificate_key /etc/ssl/certs/www.laogao.cloud/www.key;
# Basic 认证保护
location /auth-basic/ {
auth_basic "Basic Auth"; # 提示信息
auth_basic_user_file "/etc/nginx/.htpasswd"; # 密码文件
}
}
12.3 测试
# 无需密码 → 401 Unauthorized
curl -k https://www.laogao.cloud/auth-basic/
# 带密码访问
curl -ku laogao:123456 https://www.laogao.cloud/auth-basic/
# → Test Page for Basic Authentication ✅
十三、PHP 集成(FastCGI)
Nginx 本身不解析 PHP,需要通过 FastCGI 协议转发给 PHP-FPM 处理。
请求 .php → Nginx → FastCGI → PHP-FPM(监听 9000 端口)→ 执行 PHP → 返回结果
13.1 安装 PHP-FPM
yum install -y php php-fpm
yum install -y php-gd php-common php-pear php-mbstring php-mcrypt
systemctl enable php-fpm –now
# 验证
php -v
php -r "echo 'Hello PHP';"
13.2 配置 Nginx 解析 PHP
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name www.laogao.cloud;
root /usr/share/nginx/html;
ssl_certificate /etc/ssl/certs/www.laogao.cloud/www.crt;
ssl_certificate_key /etc/ssl/certs/www.laogao.cloud/www.key;
# PHP 处理:匹配所有 .php 结尾的请求
location ~ \\.php$ {
try_files $uri =404; # 文件不存在返回 404
fastcgi_pass 127.0.0.1:9000; # PHP-FPM 地址
fastcgi_index index.php; # 默认索引文件
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 脚本路径
include fastcgi_params; # 加载 FastCGI 通用参数
}
}
# HTTP 跳转 HTTPS
server {
listen 80;
listen [::]:80;
server_name www.laogao.cloud;
return 301 https://$host$request_uri;
}
13.3 最佳实践:独立 PHP 配置文件
把 PHP 配置抽出来,避免在每个 server 块中重复:
# /etc/nginx/default.d/php.conf
location ~ \\.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
然后在 server 块中引入:
server {
# …
include /etc/nginx/default.d/*.conf; # 自动加载 php.conf
}
13.4 测试 PHP
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
systemctl restart nginx
# 浏览器访问或 curl
curl -k https://www.laogao.cloud/info.php
# → 看到 PHP 信息页 🎉
十四、常用变量与日志
14.1 Nginx 内置变量
Nginx 提供了丰富的内置变量,方便在配置中动态引用:
| $uri | 当前请求的 URI(不含参数) | /api/user/list |
| $args | URL 参数 | ?a=1&b=2 |
| $request_uri | 原始 URI(含参数) | /api/user/list?a=1 |
| $remote_addr | 客户端 IP | 192.168.1.100 |
| $http_host | 请求头中的 Host | www.example.com |
| $http_user_agent | 浏览器 UA | Mozilla/5.0… |
| $scheme | 协议 | http / https |
| $server_name | 匹配的 server_name | www.example.com |
| $request_method | 请求方法 | GET / POST |
| $status | 响应状态码 | 200 / 404 / 502 |
| $document_root | 当前请求的 root 路径 | /usr/share/nginx/html |
14.2 日志配置
# 定义日志格式(main)
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;
一条典型日志:
192.168.1.100 – – [07/Aug/2026:14:30:00 +0800] "GET /api/user HTTP/1.1"
200 1234 "https://www.example.com/" "Mozilla/5.0…" "10.1.8.10"
十五、常见问题排查
🔴 502 Bad Gateway
原因:Nginx 收到了来自上游服务器的无效响应。 排查:后端服务是否启动?端口是否正确?防火墙是否放行?
🔴 504 Gateway Timeout
原因:后端处理超时,Nginx 等不及了。 排查:后端响应是否过慢?增加 proxy_read_timeout。
🔴 499 Client Closed Request
原因:客户端在 Nginx 返回响应前主动断开连接。 排查:通常是页面加载太慢,用户刷新或关闭了页面。
🔴 403 Forbidden
原因:没有权限访问。 排查:文件权限是否正确?目录是否有 index 文件?autoindex 是否开启?
🛠 性能优化检查清单
# ✅ 静态文件高性能配置
sendfile on; # 零拷贝
tcp_nopush on; # 减少包数量
tcp_nodelay on; # 降低延迟
keepalive_timeout 65; # 长连接
# ✅ Gzip 压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;
# ✅ 安全防护
location /admin {
allow 192.168.1.0/24; # IP 白名单
deny all;
}
# ✅ 并发连接数
worker_processes auto; # 匹配 CPU
worker_connections 1024; # 每个 Worker 连接数
# 最大并发 = cpu 核数 × 1024
📚 总结
本文从零开始系统讲解了 Nginx 的全部核心知识点:
Nginx 学习路线图:
┌─────────────┐
│ 1. 基础概念 │ → 什么是 Nginx、为什么高性能
├─────────────┤
│ 2. 安装部署 │ → yum 安装、防火墙配置
├─────────────┤
│ 3. 配置文件 │ → main/events/http/server/location 五层结构
├─────────────┤
│ 4. 静态站点 │ → root / index
├─────────────┤
│ 5. 虚拟主机 │ → 域名/端口 两种方式
├─────────────┤
│ 6. 反向代理 │ → proxy_pass + / 号玄学
├─────────────┤
│ 7. Location │ → 六种匹配 + 优先级 = 精确 > 前缀 > 正则 > 通用
├─────────────┤
│ 8. 负载均衡 │ → 六种算法(rr/weight/ip_hash/hash/least_conn/least_time)
├─────────────┤
│ 9. HTTPS │ → OpenSSL 自签名 + 301 跳转
├─────────────┤
│10. 高级功能 │ → Basic认证 / PHP-FPM / URL重写 / 日志
└─────────────┘
💡 学习建议:建议一边看一边动手敲命令,把本文的每个示例都在你的环境中跑一遍。 🔗 官方文档:nginx.org/en/docs/ 📅 学习日期:2026-08-07
Happy Learning! 🚀
网硕互联帮助中心




评论前必须登录!
注册