禁止发送server版本号
1 2 3
| http { server_tokens off; }
|
location 后缀白名单
1 2 3 4 5 6 7 8 9 10 11
| server { location / { # 这个地方是不是还应该进一步加强,恶意输入的path前缀可能不是`/`,比如`0x01` return 444; } location = / { root /data/www; } location ~ \.(html|css|js|jpe?g|png|gif|ico|txt|woff|ttf|eot|svg)$ { root /data/www; } }
|
强制https
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| server { listen 80; listen [::]:80;
server_name hostname; return 301 https://$host$request_uri; }
server { listen 443 ssl; listen [::]:443 ssl;
server_name hostname; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; # 另有always参数 }
|
请求方法白名单
1 2 3 4 5
| server { if ( $request_method !~ ^(GET|POST|HEAD)$ ) { return 405; } }
|
想继续ban掉不在白名单的请求ip
白名单带有请求参数的请求
1 2 3 4 5
| server { if ($args !~ '^(|v=[0-9]+)$') { return 403; } }
|
cgi
似乎不用配置,要显示指明fastcgi_pass
才会用到,像是和proxy_pass
类似
https优化
1 2 3 4 5 6 7
| http { server { ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # ssl_handshake_timeout 15s; # 这个配了,报 nginx: [emerg] "ssl_handshake_timeout" directive is not allowed here } }
|
参考官方文档
naxsi
禁止bot
1 2 3
| # /robots.txt User-agent: * Disallow: /
|
favicon.ico 404
1 2 3
| location = /favicon.ico { return 404; }
|
这样就不会在error.log里看到fopen错误了