一系列nginx安全配置,


一、模块

1.查看所有模块:

[root@proxy nginx-1.12.2]# ./configure --help

2.选择适合的模块:
pcre:开启正则表达式支持
http_autoindex_module:自动索引模块
ssi_module:SSI 脚本
http_ssl_module:ssl 支持 http 模块
http_v2_module:http_v2 协议
http_realip_module:获取客户端真实 ip
http_stub_status_module:查看服务器工作状态
http_charset_module:编码模块,例如 utf-8
http_gzip_module:压缩模块
http_auth_basic_module:用户认证
http_rewrite_module:地址重写模块
http_proxy_module:代理服务器模块
mail_pop3_module:邮件服务
mail_imap_module:邮件服务
mail_smtp_module:邮件服务
http_upstream_ip_hash_module:哈希轮询

[root@proxy nginx-1.12.2]# ./configure --without-http_autoindex_module # 禁止访问时文件索引列表
[root@proxy nginx-1.12.2]# make 
[root@proxy nginx-1.12.2]# make install

二、版本号隐藏

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
http {
server_tokens off; 
...}

或者直接删除 CRLF 参数

三、隐藏软件信息(需要重编)

[root@proxy nginx-1.12.2]# vim src/http/ngx_http_header_filter_module.c
static u_char ngx_http_server_string[] = "Server: Jacob" CRLF; #简洁信息 
static u_char ngx_http_server_full_string[] = "Server: Jacob" CRLF; #完整信息 
static u_char ngx_http_server_build_string[] = "Server: Jacob" CRLF;# 构造信息

四、重写40x.html和50x.html

自写页面,这边就不举例了

五、限制并发量

DDOS 攻击者会发送大量的并发连接,占用服务器资源,导致正常用户处于等待或 无法访问服务器的状态。
Nginx 默认安装模块中提供了一个 ngx_http_limit_req_module 模块,可以有效降 低 DDOS 攻击的风险。
语法格式如下:

limit_req_zone 关键字 zone=名字:容量 rate=接收请求速度。
$binary_remote_addr:系统内置变量,表示远程连接地址。
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 
http { 
...
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; 
  server { 
  ...
  limit_req zone=one burst=5; 
  ...
}

将客户端 IP 信息存储在名称为 one 的内存中,内存空间 10M,能存储 8 万个主机 连接状态,每秒仅接受 1 个请求,多余的放入漏斗,漏斗中的请求超过 5 个后则拒绝接下来的所有请求

六、拒绝非法的请求

HTTP 协议中定义了很多方法,可以让用户连接服务器,获得需要的资源。但实际 应用中一般仅用到 GET 和 POST:
GET:请求指定的页面信息,并返回实体主体
HEAD:类似于 GET,但是只返回报头
POST:提交数据并进行处理
DELETE:请求服务器删除指定页面
PUT:向服务器特定位置上传资料
[root@proxy ~]# curl -i -X GET http://192.168.4.5 #-i 显示头部信 息,-X 指定请求方法

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 
server { 
...
if ($request_method !~ ^(GET|POST)$ ) {return 444;} #当访问类型不是 GET 和 POST 时,返回 444 错误 
}

七、防止缓存溢出

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf 
http { 
client_body_buffer_size 8K; #主体缓存空间 
client_max_body_size 16k; #主体最大缓存 
client_header_buffer_size 1k; #默认请求包头信息的缓存 
large_client_header_buffers 4 4k; #大请求包头信息的缓存个数,每个 缓存的容量 
...}

相关内容