通过加载第三方动态库隐藏Nginx Server字段
        【摘要】 nginx加载动态库
    
    
    
    本文介绍如果通过加载第三方库实现隐藏Nginx服务器的软件信息。直接上干货。
第三方动态库地址:https://github.com/GetPageSpeed/ngx_security_headers
1. 下载ngx_security_headers源码
wget https://github.com/GetPageSpeed/ngx_security_headers/archive/refs/tags/0.0.11.tar.gz
2. 下载Nginx源码
nginx官方下载地址:https://nginx.org/en/download.html
打开页面后,找到对应的版本(编译出来的so文件不能跨nginx版本使用)。我这里用的是1.23.1,直接下载1.23.1对应的包。
wget https://nginx.org/download/nginx-1.23.1.tar.gz
3. 编译第三方库
软件依赖:
pcre
zlib
分别解压nginx代码和第三方库代码。解压完成后,进入nginx代码目录执行编译
./configure --with-compat --add-dynamic-module=../ngx_security_headers-0.0.11   #../ngx_security_headers-0.0.11 替换为第三方库代码的实际路径
make -f objs/Makefile modules
编译完成后,会在objs目录下生成编译好的so文件,将so文件放到nginx的modules目录下或者nginx容器内即可调用

4. 配置第三方库
在nginx配置文件nginx.conf中进行修改
- 在最外层加载第三方库:load_module modules/ngx_http_security_headers_module.so;
 - 在http配置块下增加:hide_server_tokens on;
完整的配置如下: 
user  nginx;
worker_processes  auto;
worker_rlimit_nofile  10240;
load_module modules/ngx_http_security_headers_module.so;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
events {
    worker_connections  10240;
}
http {
    server_tokens off;
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"$proxy_host" "$upstream_addr"';
    access_log  /var/log/nginx/access.log  main;
    hide_server_tokens on;
    sendfile        on;
    keepalive_timeout  65;
    proxy_buffer_size  256k;
    proxy_buffers  4 256k;
    proxy_busy_buffers_size 256k;
    client_body_buffer_size 256k;
    client_header_buffer_size 256k;
    client_max_body_size 0;
    large_client_header_buffers 4 32k;
    charset utf-8;
    gzip on;
    sendfile_max_chunk 1m;
    tcp_nopush on;
    tcp_nodelay on;
    aio threads;
    directio 4m;
    include /etc/nginx/conf.d/*.conf;
}
5. 在docker-compose中使用
直接上文件
version: '3'
services:
  nginx:
    image: nginx:1.23.1
    restart: always
    hostname: nginx
    environment:
      TZ: Asia/Shanghai
    volumes:
      - './nginx.conf:/etc/nginx/nginx.conf:ro'
      - './conf.d:/etc/nginx/conf.d:ro'
      - './modules/ngx_http_security_headers_module.so:/etc/nginx/modules/ngx_http_security_headers_module.so'
    ulimits:
      nproc: 65535
      nofile:
        soft: 65535
        hard: 65535
    network_mode: host
            【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
                cloudbbs@huaweicloud.com
                
            
        
        
        
        
        
        
        - 点赞
 - 收藏
 - 关注作者
 
            
           
评论(0)