一、核心安全配置
1. 隐藏版本号
隐藏 Nginx 版本号是安全加固的第一步,可以防止攻击者针对特定版本漏洞进行攻击。
# 1. 修改 nginx.conf 全局配置
http {
# 隐藏版本号
server_tokens off;
# 自定义错误页面(可选,避免泄露信息)
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 /error.html;
}
server {
listen 80;
server_name example.com;
# 关闭版本号显示
server_tokens off;
# 自定义错误页面
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
}
# 2. 修改源码彻底隐藏(需要编译)
# 在编译前修改 src/core/nginx.h 文件
# vi src/core/nginx.h
# 修改以下行:
#define NGINX_VERSION "2.4.6"
#define NGINX_VER "Apache/" NGINX_VERSION
# 或修改为自定义字符串
#define NGINX_VER "Microsoft-IIS/10.0"
# 3. 修改 fastcgi 参数(防止 PHP 泄露)
location ~ \.php$ {
fastcgi_param SERVER_SOFTWARE "Microsoft-IIS/10.0";
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
}
# 4. 验证配置
# curl -I http://example.com
# 响应头中不应包含 Server: nginx/版本号
# 应为 Server: nginx 或自定义名称
2. 限制危险请求方法
HTTP 协议定义了多种请求方法,其中一些可能存在安全风险,需要限制使用。
# 1. 全局限制危险请求方法
server {
listen 80;
server_name example.com;
# 只允许 GET、HEAD、POST 方法
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405;
}
# 更精细的控制
location / {
limit_except GET HEAD POST {
deny all;
}
}
}
# 2. 针对 API 接口的限制
location /api/ {
# API 只允许 GET 和 POST
limit_except GET POST {
deny all;
}
# 禁止 TRACE 方法
if ($request_method = TRACE) {
return 405;
}
proxy_pass http://backend;
}
# 3. 禁止特定方法
location / {
# 拒绝 DELETE、PUT 等方法
if ($request_method ~ ^(DELETE|PUT|PATCH|TRACE|CONNECT|OPTIONS)$) {
return 405;
}
}
# 4. 只允许 GET 的静态资源
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# 静态资源只允许 GET 和 HEAD
limit_except GET HEAD {
deny all;
}
expires 30d;
access_log off;
}
# 5. OPTIONS 方法处理(CORS 预检请求)
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
}
3. 请求限制(CC 攻击防御)
3.1 连接数限制
# 1. 定义限制区域
http {
# 限制每个 IP 的连接数
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
# 限制每个 IP 的请求速率
limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s;
# 动态黑名单(结合 geo 模块)
geo $blacklist {
default 0;
192.168.1.100 1; # 手动加入黑名单的 IP
10.0.0.0/24 1;
}
server {
listen 80;
server_name example.com;
# 2. 应用连接数限制
location / {
# 每个 IP 最多同时 10 个连接
limit_conn conn_limit 10;
# 请求速率限制:平均 10r/s,突发 20
limit_req zone=req_limit burst=20 nodelay;
# 返回状态码
limit_conn_status 503;
limit_req_status 503;
# 日志级别
limit_conn_log_level warn;
limit_req_log_level warn;
proxy_pass http://backend;
}
# 3. 针对不同 URL 设置不同限制
location /api/ {
# API 接口限制更严格
limit_req zone=req_limit burst=5 nodelay;
limit_conn conn_limit 5;
proxy_pass http://api_backend;
}
location /login/ {
# 登录接口限制(防止暴力破解)
limit_req zone=req_limit burst=3 nodelay;
limit_conn conn_limit 2;
proxy_pass http://login_backend;
}
location /static/ {
# 静态资源不限制
limit_req off;
limit_conn off;
expires 30d;
}
}
}

