栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

服务安全--nginx的安全优化

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

服务安全--nginx的安全优化

NGINX服务安全
禁用模块
隐藏版本信息
限制并发
拒绝非法请求
防止buff溢出

nginx是模块化设计
启用模块需要 --with 加载模块
禁用模块需要使用 --without 禁用模块
因为好多模块都是默认安装的,所以不需要的模块但是又自动安装需要禁用

安装nginx时 需要安装依赖,不同的环境需要安装不同的依赖关系

]# yum -y install gcc pcre-devel  zlib-devel
禁用模块

为了nginx的安全需要禁用http_autoindex_module 模块 和 http_ssi_module模块
禁用模块在编译安装的时候进行
autoindex 禁用自动索引文件目录模块

]# ./configure --without-http_autoindex_module  --without-http_ssi_module
]# make  &&  make install
隐藏版本信息

linux中一切皆文件,一切皆命令, nginx的版本信息就写在文件ngx_http_header_filter_module.c中 在文件中修改源代码,实现隐藏版本信息的功能

在配置文件下手动添加,

[root@localhost nginx-1.12.2]# vim /usr/local/nginx/conf/nginx.conf

html { 
       server_tokens  off;

修改源代码文件

]# vim src/http/ngx_http_header_filter_module.c 
49-51行 默认
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;

修改之后
static u_char ngx_http_server_string[] = "Server: tomcat" CRLF;
static u_char ngx_http_server_full_string[] = "Server: tomcat" CRLF;
static u_char ngx_http_server_build_string[] = "Server: tomcat"  CRLF;

需要重新编译安装,才能测试,要不然不生效
重启应用并测试

 ./configure
 make  &&  make install
]# /usr/local/nginx/sbin/nginx -s stop
]# /usr/local/nginx/sbin/nginx 

]# curl -I  http://192.168.4.88
HTTP/1.1 200 OK
Server: tomcat     软件名以经不是nginx
Date: Tue, 24 Sep 2019 12:44:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Sep 2019 11:21:24 GMT
Connection: keep-alive
ETag: "5d89fc34-264"
Accept-Ranges: bytes

限制并发量

语法:limit_reqq_zone key zone=name:size rate=rate;
将客户端ip的信息存储进one的共享内存,空间设置为10m
每秒仅接受一个请求,多余的放入漏斗 ,漏斗最对不超过五个,否则报错

http{
 limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
 server{
 limit_req zone=one burst=5;
 }
 }

测试并发量
安装并发量测试软件

]# yum -y install httpd-tools.x86_64 
]# ab -c 10 -n 10 http://192.168.4.88/test.html
Concurrency Level:      10
Time taken for tests:   5.002 seconds
Complete requests:      10
Failed requests:        4

可以看到 十个并发量接受了6个 其余失败,不管多少都只能成功四个,

拒绝非法请求

常见的http请求方法
GET 指定页面信息,并返回实体主体
HEAD 类似与get请求,只不过返回的相应中没有具体的内容,用于获取报头
POST 向指定资源提交数据进行处理请求
DELETe 请求服务器删除指定的页面
PUT 向服务器特定的位置上传资料

拒绝非法请求,只允许GET|POST请求

]# vim /usr/local/nginx/conf/nginx.conf
server { 
    if ($request_method !~ ^(GET|POST)$) {
                        return 444;
                }

使用GET访问

]# curl -i  -X GET "http://192.168.4.88" 
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 24 Sep 2019 12:25:10 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 24 Sep 2019 11:21:24 GMT
Connection: keep-alive
ETag: "5d89fc34-264"
Accept-Ranges: bytes




Welcome to nginx!

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

使用 HEAD访问

[root@localhost nginx-1.12.2]# curl -i  -X HEAD "http://192.168.4.88" 
curl: (52) Empty reply from server

可以明确的看到HRAD访问并拒绝,只有GET|POST请求被接收

防止buffer溢出

如果有人恶意攻击会导致服务器增加内存,各种缓存信息,当达到一定数量内存就会溢出,别人可以通过溢出的内存获取权限,攻击我们的服务

]# vim /usr/local/nginx/conf/nginx.conf
http{
 client_body_buffer_size       1K;
        client_header_buffer_size     1k;
        client_max_body_size         16k;
        large_client_header_buffers 4 4k;

重启服务

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268876.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号