一、Nginx 是什么
Nginx 是一个高性能的 HTTP 和反向代理服务器,以其高并发、低内存占用和丰富的功能特性成为目前最流行的 Web 服务器之一。常用于静态资源托管、反向代理、负载均衡、HTTPS 配置和 API 网关。
Nginx vs Apache
| 特性 | Nginx | Apache |
|---|
| 并发模型 | 事件驱动(异步非阻塞) | 进程/线程驱动 |
| 内存占用 | 低(1 万连接约 2.5MB) | 高(每个连接一个进程) |
| 静态文件 | 极快 | 快 |
| 动态内容 | 通过代理转发(不原生支持) | 内置模块(mod_php) |
| 配置 | 简洁 | 复杂 |
二、安装与基本命令
1 2 3 4 5 6 7 8 9
| sudo apt update sudo apt install nginx -y
sudo dnf install nginx -y
brew install nginx
|
常用命令
1 2 3 4 5 6 7 8 9 10
| nginx -t sudo systemctl start nginx sudo systemctl stop nginx sudo systemctl restart nginx sudo systemctl reload nginx sudo systemctl enable nginx sudo systemctl status nginx
nginx -s reload nginx -s quit
|
三、配置文件结构
1 2 3 4 5 6
| /etc/nginx/ ├── nginx.conf # 主配置文件 ├── sites-available/ # 可用站点配置 ├── sites-enabled/ # 已启用的站点配置(软链接) ├── conf.d/ # 额外配置片段 └── modules-enabled/ # 已启用的模块
|
nginx.conf 基本结构
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
| user www-data; worker_processes auto; pid /run/nginx.pid;
events { worker_connections 1024; multi_accept on; use epoll; }
http { include /etc/nginx/mime.types; default_type application/octet-stream;
sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65;
gzip on; gzip_types text/plain text/css application/json application/javascript;
access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
|
四、静态文件服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 80; server_name example.com; root /var/www/html;
index index.html;
location / { try_files $uri $uri/ /index.html; }
location ~* \.(js|css|png|jpg|svg|ico)$ { expires 30d; add_header Cache-Control "public, immutable"; } }
|
五、反向代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 80; server_name api.example.com;
location / { proxy_pass http://localhost: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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }
|
常见代理目标
| 后端 | 端口 |
|---|
| Node.js (Express/Koa) | 3000 |
| Nuxt.js | 3000 |
| Next.js | 3000 |
| Python (Django/Flask) | 8000 |
| Java (Spring Boot) | 8080 |
| PHP-FPM | Unix Socket |
六、HTTPS 配置
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
| server { listen 443 ssl http2; server_name example.com;
ssl_certificate /etc/ssl/certs/example.com.pem; ssl_certificate_key /etc/ssl/private/example.com.key;
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;
location / { proxy_pass http://localhost:3000; } }
server { listen 80; server_name example.com; return 301 https://$host$request_uri; }
|
使用 Let’s Encrypt 免费证书:
1 2
| sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d example.com
|
七、负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| upstream backend {
server 192.168.1.10:3000 weight=3; server 192.168.1.11:3000 weight=2; server 192.168.1.12:3000 backup; }
server { listen 80; server_name app.example.com;
location / { proxy_pass http://backend; proxy_set_header Host $host; } }
|
八、URL 重写与重定向
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 { location /old-page { return 301 /new-page; }
location /promo { return 302 https://another-site.com; }
rewrite ^/articles/(\d+)$ /post?id=$1 permanent;
location ~ /\. { deny all; }
location ~* \.(env|git|log|sql)$ { deny all; } }
|
九、安全配置
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
| server { limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s; location /api/ { limit_req zone=api burst=20 nodelay; proxy_pass http://localhost:3000; }
location /admin { allow 192.168.1.0/24; deny all; }
client_max_body_size 10m;
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; }
|
十、日志
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| log_format json escape=json '{' '"time":"$time_iso8601",' '"remote_addr":"$remote_addr",' '"request":"$request",' '"status":$status,' '"body_bytes":$body_bytes_sent,' '"request_time":$request_time,' '"upstream_addr":"$upstream_addr",' '"http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent"' '}';
access_log /var/log/nginx/access.log json; error_log /var/log/nginx/error.log warn;
|
十一、性能调优
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
| worker_processes auto; worker_rlimit_nofile 65535;
events { worker_connections 4096; use epoll; multi_accept on; }
http { sendfile on; tcp_nopush on; tcp_nodelay on;
proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k;
keepalive_timeout 65; proxy_connect_timeout 10; proxy_read_timeout 30;
open_file_cache max=2000 inactive=20s; open_file_cache_valid 60s; open_file_cache_min_uses 2; }
|
十二、常见问题
Q1: 配置修改后未生效
1 2
| nginx -t sudo systemctl reload nginx
|
Q2: 403 Forbidden
1 2 3 4
|
sudo chown -R www-data:www-data /var/www/html sudo chmod -R 755 /var/www/html
|
Q3: 502 Bad Gateway
1 2 3 4
| systemctl status node-server
ss -tlnp | grep 3000
|
Q4: 反向代理获取不到真实客户端 IP
1 2 3
| proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
Q5: 端口转发
1 2 3 4 5 6 7 8
| server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; } }
|
十三、推荐学习路径
- 掌握静态文件服务和反向代理配置
- 配置 HTTPS(Let’s Encrypt)
- 理解 location 匹配规则和 rewrite
- 配置负载均衡和速率限制
- 调优 worker 数量和缓存策略
- 阅读
/var/log/nginx/access.log 排查问题