# 🚀 Nginx 使用手册
# 📋 目录
# 🛠️ 基础命令
# 启动 Nginx | |
nginx | |
# 停止 Nginx | |
nginx -s stop # 立即停止 | |
nginx -s quit # 优雅停止 | |
# 重新加载配置 | |
nginx -s reload | |
# 重新打开日志文件 | |
nginx -s reopen | |
# 检查 Nginx 版本和编译参数 | |
nginx -V | |
# 检查配置文件语法 | |
nginx -t |
# 📝 配置文件
# 基础配置示例
server { | |
listen 80; # 监听端口 | |
server_name localhost; # 服务器名称 | |
root /var/www/localhost; # 网站根目录 | |
index index.html; # 默认索引文件 | |
# 错误页面配置 | |
error_page 404 /404.html; | |
# 基础 location 配置 | |
location / { | |
add_header X-debug-uri "$uri"; | |
try_files $uri $uri/ = 404; | |
} | |
} |
# 🎯 Location 配置
# 匹配规则优先级
=
精确匹配^~
优先前缀匹配~
和~*
正则匹配~
区分大小写~*
不区分大小写
- 普通前缀匹配
# 示例配置
# 精确匹配 | |
location = / { | |
root /var/www/html; | |
} | |
# 优先前缀匹配 | |
location ^~ /images/ { | |
root /var/www/images; | |
} | |
# 正则匹配(区分大小写) | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php-fpm.sock; | |
} | |
# 正则匹配(不区分大小写) | |
location ~* \.(jpg|jpeg|png|gif)$ { | |
expires 30d; | |
add_header Cache-Control "public, no-transform"; | |
} |
# 🔄 反向代理
# 基础配置
# 定义上游服务器组 | |
upstream backend { | |
server 127.0.0.1:8080 weight=3; # 权重为 3 | |
server 127.0.0.1:8081; # 默认权重为 1 | |
server 127.0.0.1:8082; # 默认权重为 1 | |
} | |
# 负载均衡配置 | |
server { | |
listen 80; | |
server_name example.com; | |
location / { | |
proxy_pass http://backend; | |
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; | |
} | |
} |
# 负载均衡策略
round-robin
:轮询(默认)least_conn
:最少连接数ip_hash
:基于客户端 IP 的哈希hash
:基于自定义 key 的哈希
# 🔀 重定向与重写
# 重定向配置
# 临时重定向(302) | |
location /old { | |
return 302 /new; | |
} | |
# 永久重定向(301) | |
location /old { | |
return 301 /new; | |
} | |
# 重写规则 | |
location /old { | |
rewrite ^/old/(.*) /new/$1 break; | |
} |
# 📦 常见配置示例
# 静态文件服务
server { | |
listen 80; | |
server_name static.example.com; | |
root /var/www/static; | |
# 启用 gzip 压缩 | |
gzip on; | |
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; | |
# 缓存配置 | |
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { | |
expires 30d; | |
add_header Cache-Control "public, no-transform"; | |
} | |
} |
# PHP 应用配置
server { | |
listen 80; | |
server_name php.example.com; | |
root /var/www/php; | |
index index.php; | |
location ~ \.php$ { | |
fastcgi_pass unix:/var/run/php-fpm.sock; | |
fastcgi_index index.php; | |
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; | |
include fastcgi_params; | |
} | |
} |
# ⚡ 性能优化
# 基础优化配置
# 工作进程数 | |
worker_processes auto; | |
# 每个工作进程的最大连接数 | |
events { | |
worker_connections 1024; | |
multi_accept on; | |
use epoll; | |
} | |
# 开启高效传输模式 | |
http { | |
sendfile on; | |
tcp_nopush on; | |
tcp_nodelay on; | |
keepalive_timeout 65; | |
types_hash_max_size 2048; | |
} |
# 🔒 安全配置
# 基础安全配置
# 隐藏版本号 | |
server_tokens off; | |
# 安全头部 | |
add_header X-Frame-Options "SAMEORIGIN"; | |
add_header X-XSS-Protection "1; mode=block"; | |
add_header X-Content-Type-Options "nosniff"; | |
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; | |
# SSL 配置 | |
ssl_protocols TLSv1.2 TLSv1.3; | |
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; |
# 💡 实用提示
配置文件检查
- 修改配置后使用
nginx -t
检查语法 - 使用
nginx -s reload
重新加载配置
- 修改配置后使用
日志管理
- 访问日志:
/var/log/nginx/access.log
- 错误日志:
/var/log/nginx/error.log
- 使用
tail -f
实时查看日志
- 访问日志:
性能监控
- 使用
top
监控 Nginx 进程 - 使用
netstat
查看连接状态 - 使用
stub_status
模块监控状态
- 使用
故障排查
- 检查错误日志
- 检查文件权限
- 检查 SELinux 状态
- 检查防火墙配置
# 🎯 最佳实践
配置文件组织
- 使用
include
指令拆分配置文件 - 按功能模块组织配置
- 使用注释说明配置用途
- 使用
安全加固
- 及时更新 Nginx 版本
- 配置 SSL/TLS
- 限制请求频率
- 配置访问控制
性能调优
- 根据服务器配置调整工作进程数
- 启用 gzip 压缩
- 配置适当的缓存策略
- 使用 CDN 加速静态资源
监控维护
- 定期检查日志
- 监控服务器状态
- 备份配置文件
- 制定应急预案