NGINX优化

Nginx配置与安全优化
本文介绍如何通过配置隐藏Nginx版本信息、自定义软件名称、实施日志管理和目录访问控制等措施来增强服务器安全性,并提供具体的配置示例。

目录

1    版本号隐藏    1

2    软件名更改    1

3    日志轮滚    4

4    关闭指定日志    4

5    日志权限设置    4

6    目录访问控制    4

7    限制IP访问    6

 

  1. 版本号隐藏

    配置文件添加配置:

    http {

        ···

        server_tokens off;

        ···

    }

     

    官方参数资料:http://nginx.org/en/docs/http/ngx_http_core_module.html

  2. 软件名更改

    要实现软件名及版本号的修改,需要对源码文件进行改动后,编译安装;

    所需修改文件分别是:

    nginx-1.12.1/src/core/nginx.h

    nginx-1.12.1/src/http/ngx_http_header_filter_module.c

    nginx-1.12.1/src/http/ngx_http_special_response.c

     

    源码文件如下:

    [root@c1 ~/nginx-1.12.1/src/core]# sed -n '13,17p' nginx.h

    #define NGINX_VERSION "1.12.1"

    #define NGINX_VER "nginx/" NGINX_VERSION

     

    #ifdef NGX_BUILD

    #define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"

    #else

    #define NGINX_VER_BUILD NGINX_VER

    #endif

     

    #define NGINX_VAR "NGINX"

    #define NGX_OLDPID_EXT ".oldbin"

     

    修改后如下:

    #define NGINX_VERSION "5.1.8"

    #define NGINX_VER "KZH/" NGINX_VERSION

     

    #ifdef NGX_BUILD

    #define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"

    #else

    #define NGINX_VER_BUILD NGINX_VER

    #endif

     

    #define NGINX_VAR "KZH"

    #define NGX_OLDPID_EXT ".oldbin"

     

    源码文件如下:

    [root@c1 ~/nginx-1.12.1/src/http]# sed -n '49p' ngx_http_header_filter_module.c

    static u_char ngx_http_server_string[] = "Server: nginx" CRLF;

     

    修改后如下:

    static u_char ngx_http_server_string[] = "Server: KZH" CRLF;

     

    源码文件如下:

    [root@c1 ~/nginx-1.12.1/src/http]# sed -n '21,38p' ngx_http_special_response.c

    static u_char ngx_http_error_full_tail[] =

    "<hr><center>" NGINX_VER "</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

    ;

     

     

    static u_char ngx_http_error_build_tail[] =

    "<hr><center>" NGINX_VER_BUILD "</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

    ;

     

     

    static u_char ngx_http_error_tail[] =

    "<hr><center>nginx</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

     

    修改后如下:

    static u_char ngx_http_error_full_tail[] =

    "<hr><center>" NGINX_VER "(http://cnblogs.com/kazihuo)</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

    ;

     

     

    static u_char ngx_http_error_build_tail[] =

    "<hr><center>" NGINX_VER_BUILD "</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

    ;

     

     

    static u_char ngx_http_error_tail[] =

    "<hr><center>KZH</center>" CRLF

    "</body>" CRLF

    "</html>" CRLF

     

    [root@c1 ~/nginx-1.12.1]# ./configure --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --prefix=/usr/local/kzh/

    [root@c1 ~/nginx-1.12.1]# make && make install

    [root@c1 /usr/local/kzh/sbin]# ./nginx

     

    源码文件显示:

    [root@c1 ~]# curl -I 192.168.10.11:80

    HTTP/1.1 200 OK

    Server: nginx

    Date: Thu, 07 Dec 2017 01:12:27 GMT

    Content-Type: text/html

    Content-Length: 612

    Last-Modified: Wed, 06 Dec 2017 19:15:30 GMT

    Connection: keep-alive

    ETag: "5a2841d2-264"

    Accept-Ranges: bytes

     

    修改后页面显示:

    [root@c1 ~]# curl -I 192.168.10.11:88

    HTTP/1.1 200 OK

    Server: KZH/5.1.8

    Date: Thu, 07 Dec 2017 01:12:31 GMT

    Content-Type: text/html

    Content-Length: 612

    Last-Modified: Thu, 07 Dec 2017 00:44:19 GMT

    Connection: keep-alive

    ETag: "5a288ee3-264"

    Accept-Ranges: bytes

     

  3. 日志轮滚

    # cat /server/scripts/cut_log.sh

    #/bin/bash

    cd /usr/local/nginx/logs/ && /bin/mv access.log access-$(date -d "-1 day" +%F).log

    /usr/local/nginx/sbin/nginx -s reload

    # crontab -l

    1 * * * * /bin/sh /server/scripts/cut_log.sh

  4. 关闭指定日志

    实际工作中,日志写入消耗磁盘I/O,对于负载均衡器健康节点检查或特定文件日志不需记录;

    用location标签匹配不记录日志的元素扩展名,关闭其日志:

  5. 日志权限设置

    日志目录权限给了NGINX用户,会成为安全隐患:

    # chown -R root.root /usr/local/nginx/logs/

    # chown -R 700 /usr/local/nginx/logs/

  6. 目录访问控制

    eg1:配置Nginx,禁止解析指定目录下的指定程序:

     

        对上述目录的限制必须写在Nginx处理PHP服务配置的前面,如下:

     

        eg2:禁止访问*.txt和*.doc文件:

     

        eg3:禁止访问单个目录:

     

        禁止访问多个目录:

     

        eg4:禁止访问目录并返回指定HTTP状态码:

        应用场景:对于集群的共享存储,一般是存放静态资源文件,所以可以禁止执行指定扩展名的程序,例:.php .sh .pl .py

  7. 限制IP访问

    eg1:只允许指定IP访问该目录,且支持PHP解析:

        eg2:限制指定IP或IP段访问:

     

-------------------------------------------------------------

作者: 罗穆瑞

转载请保留此段声明,且在文章页面明显位置给出原文链接,谢谢!

------------------------------------------------------------------------------

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

------------------------------------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值