zoukankan      html  css  js  c++  java
  • Nginx 优化

    Nginx 安全优化

    隐藏 Nginx 版本号

    实现隐藏 Nginx 版本号的方法是:在 Nginx 配置文件 nginx.conf 中的 http 标签段内加入 “server_tokens off;”参数,如下:

    http{
    ……
    server_tokens off;
    ……
    }
    

    此参数放置在 http 标签内,作用是控制 http response header 内的 Nginx 版本号的显示,以及错误信息中 Nginx 版本号的显示。此参数的默认值为 on,表示显示 Nginx 版本号。点击查看官方中文说明。

    隐藏 Nginx 软件名及版本号

    需要修改3个 Nginx 源代码文件并重新编译安装 Nginx。

    第1个文件:nginx-1.14.0/src/core/nginx.h(13、14、22行)

    13 #define NGINX_VERSION      "1.14.0"    # 将1.14.0修改为想要显示的版本号,如1.6.2
    14 #define NGINX_VER          "nginx/" NGINX_VERSION    # 将 nginx 修改为想要修改的软件名称,如 Apache
    22 #define NGINX_VAR          "NGINX"       # 将 NGINX 修改为想要修改的软件名称,如 Apache  
    

    第2个文件:nginx-1.14.0/src/http/ngx_http_header_filter_module.c(49行)

    49 static u_char ngx_http_server_string[] = "Server: nginx" CRLF;              #把“Server: nginx”替换为“Server: Apache”
    

    第3个文件:nginx-1.14.0/src/http/ngx_http_special_response.c(22、36行)

     22 "<hr><center>" NGINX_VER "</center>" CRLF # 此行需要修改,修改为"<hr><center>" NGINX_VER "(http://www.cnblogs.com/wushuaishuai/)</center>" CRLF
     36 "<hr><center>nginx</center>" CRLF # 此行需要修改,将对外展示的 nginx 改为 Apache
    

    具体操作步骤:

    • 关闭 Nginx 进程
    /usr/local/nginx/sbin/nginx -s stop
    
    • 备份一下之前的 nginx 安装目录
    cp -Rp /usr/local/nginx-1.14.0/ /usr/local/nginx-1.14.0-$(date +%Y%m%d)
    
    • 查看一下配置参数并记录下来
    /usr/local/nginx/sbin/nginx -V
    
    • 使用 sed 命令一键修改(只测试了1.14.0版本)
    sed -i 's#"1.14.0"#"1.6.2"#g' nginx-1.14.0/src/core/nginx.h
    sed -i 's#"nginx/"#"Apache/"#g' nginx-1.14.0/src/core/nginx.h
    sed -i 's#"NGINX"#"Apache"#g' nginx-1.14.0/src/core/nginx.h
    sed -i 's#Server: nginx#Server: Apache#g' nginx-1.14.0/src/http/ngx_http_header_filter_module.c
    sed -i 's#NGINX_VER_BUILD "#NGINX_VER "(http://www.cnblogs.com/wushuaishuai/)#g' nginx-1.14.0/src/http/ngx_http_special_response.c
    sed -i 's#nginx</center>#Apache</center>#g' nginx-1.14.0/src/http/ngx_http_special_response.c
    
    • 最后重新编译安装,如果之前经过配置的 nginx 源代码删掉了,则需要重新下载 nginx 源码包并使用之前的配置参数重新配置一下再编译安装
    cd nginx-1.14.0/
    make
    make install
    
    • 启动 Nginx 进程
    /usr/local/nginx/sbin/nginx
    

    Nginx 性能优化

  • 相关阅读:
    django-3-模板变量,过滤器,静态文件的引用
    django-2-路由配置及渲染方式
    用pycharm运行django项目
    django-1-框架介绍
    django-6-数据库配置及模型创建,激活(django模型系统1)
    用Sklearn实现聚类算法并用散点图展现效果
    方差、标准差、协方差、协方差相关系数
    ARIMa--时间序列模型
    人工智能--第二天--KNN算法实现
    人工智能--第二天--KNN算法
  • 原文地址:https://www.cnblogs.com/wushuaishuai/p/9367688.html
Copyright © 2011-2022 走看看