📋 Nginx 速查表
常用命令
| 操作 | 命令 |
|---|---|
| 检查配置语法 | nginx -t |
| 重载配置 | nginx -s reload |
| 停止 Nginx | nginx -s stop |
| 优雅关闭 | nginx -s quit |
| 重新打开日志 | nginx -s reopen |
| 查看版本 | nginx -v |
| 查看编译参数 | nginx -V |
Location 匹配规则
| 语法 | 含义 | 示例 |
|---|---|---|
| = | 精确匹配 | location = /api {} |
| ^~ | 前缀匹配(不检查正则) | location ^~ /static {} |
| ~ | 正则匹配(区分大小写) | location ~ .php$ {} |
| ~* | 正则匹配(不区分大小写) | location ~* .(jpg|png)$ {} |
| / | 前缀匹配(最长匹配) | location /api/ {} |
💡 提示: 💡 优先级:=>^~>~/~*(按顺序)> 最长前缀
常见变量
| 变量 | 含义 |
|---|---|
| $host | 请求的主机名 |
| $request_uri | 完整的原始请求 URI(含参数) |
| $uri | 当前的 URI(不含参数) |
| $remote_addr | 客户端 IP |
| $remote_port | 客户端端口 |
| $server_port | Nginx 监听端口 |
| $scheme | http 或 https |
| $request_method | GET/POST/PUT/DELETE |
| $content_type | 请求的 Content-Type |
| $upstream_cache_status | 缓存状态(HIT/MISS/EXPIRED) |
安全头配置模板
nginx
# 一键安全加固(推荐放在 http 块)
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Content-Security-Policy "default-src 'self'" always;
# 隐藏 Nginx 版本号
server_tokens off;