Skip to content

📋 Nginx 速查表

常用命令

操作命令
检查配置语法nginx -t
重载配置nginx -s reload
停止 Nginxnginx -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_portNginx 监听端口
$schemehttp 或 https
$request_methodGET/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;