目录
说明: 由于某些 Nginx 漏洞只存在于特定的版本,隐藏版本号可以提高安全性。
Nginx隐藏版本信息配置示例:
- [root@localhost ~]# curl -I 192.168.6.116
- HTTP/1.1 200 OK
- Server: nginx/1.20.1
-
- [root@localhost ~]# vim /etc/nginx/nginx.conf
- http {
- server_tokens off;
- [root@localhost ~]# systemctl restart nginx.service
- [root@localhost ~]# curl -I 192.168.6.116
- HTTP/1.1 200 OK
- Server: nginx
- http {
- ...
- error_page 401 /401.html;
- error_page 402 /402.html;
- error_page 403 /403.html;
- error_page 404 /404.html;
- error_page 405 /405.html;
- error_page 500 /500.html;
- ...
- }
说明:
Nginx添加Header示例:
- location / {
- add_header X-Frame-Options "SAMEORIGIN";
- add_header X-XSS-Protection "1; mode=block";
- add_header X-Content-Type-Options "nosniff";
- }
说明:
Nginx校验referer配置示例:
- location /images/ {
- valid_referers none blocked <domain_name> <domain_name>;
- if ($invalid_referer) {
- return 403;
- }
- }
说明:禁止一些爬虫的扫描
Nginx拒绝User-Agent配置示例:
- if ($http_user_agent ~* LWP::Simple|BBBike|wget|curl){
- return 444;
- }
说明:$request_method能获取到请求时所使用的method,应该配置只使用GET/POST方法访问,其他的method返回405
Nginx限制HTTP请求方式示例:
- if ($request_method !~ ^(GET|POST)$ ){
- return 405;
- }
- server {
- listen 443;
- server_name
; -
- # 开启https
- ssl on;
- # 配置nginx ssl证书的路径
- ssl_certificate
; - # 配置nginx ssl证书key的路径
- ssl_certificate_key
; - # 指定客户端建立连接时使用的ssl协议版本
- ssl_protocols TLSv1.2;
- # 指定客户端连接时所使用的加密算法
- ssl_ciphers HIGH:!aNULL:!MD%
- }
说明:
Nginx限制并发数配置示例:
- http {
- limit_conn_zone $binary_remote_addr zone=ops:10m;
- limit_conn_zone $server_name zone=coffee:10m;
-
- server {
- listen 80
- server_name
; - ...
- location / {
- limit_conn opos 10; # 限制单一IP来源的连接数为10
- limit_conn coffee 2000; # 限制单一虚拟服务器的总连接数为2000
- limit_rate 500k; # 限制单个连接使用的带宽
- }
- }
- }