**需求背景:** 我们在使用nginx作为应用服务器的时候,nginx会在请求的响应头里添加`Server: Nginx 1.19.9`这样的版本信息。 这样就存在了安全隐患,攻击者可以根据特定版本的服务器漏洞展开攻击。 如果能隐藏这个信息,肯定是能提高一些安全性的。也有部分人提出了更变态的要求,不仅隐藏版本号,而且也不要让人知道我用的是Nginx。 使用编译好的二进制文件可以隐藏版本信息,但是不能隐藏服务器是nginx。想完全更改nginx的响应信息需要编译源码。 下面就跟着我的步骤来一起了解一下如何编译Nginx,以及修改Nginx的响应信息吧。 ### 前提条件 我使用CentOS 7系统作为演示环境。 执行下面的命令,安装编译所需依赖。 ``` yum install gcc gcc-c++ openssl-devel pcre-devel zlib-devel ``` 执行下面的名义,检查依赖包是否正确安装。 ``` rpm -qa gcc gcc-c++ openssl-devel pcre-devel zlib-devel ``` ### 开始编译 进入系统源码目录。 ``` cd /usr/local/src ``` 下载编译所需要的源码。 ``` wget https://nginx.org/download/nginx-1.19.9.tar.gz tar xzvf nginx-1.19.9.tar.gz cd nginx-1.19.9 ``` 修改`src/core/nginx.h`。 ``` #define nginx_version 1019009 #define NGINX_VERSION "1.0.0" #define NGINX_VER "SIMAEK/" NGINX_VERSION ``` 修改:src/http/ngx_http_header_filter_module.c ``` static u_char ngx_http_server_string[] = "Server: SIMAEK" 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; ``` 编译配置。 ``` ./configure --prefix=/opt/nginx \ --with-stream \ --with-http_ssl_module ``` > 对于无法使用YUM安装依赖的机器,可以下载好需要的源码包,并在编译的时候指定使用的依赖库。`--with-pcre=/usr/local/src/pcre-8.44 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.1.1k` > > 相关下载地址: > > https://zlib.net/zlib-1.2.11.tar.gz > https://ftp.pcre.org/pub/pcre/pcre-8.44.zip > https://www.openssl.org/source/openssl-1.1.1k.tar.gz 结果。 ``` Configuration summary + using PCRE library: /usr/local/src/pcre-8.44 + using OpenSSL library: /usr/local/src/openssl-1.1.1k + using zlib library: /usr/local/src/zlib-1.2.11 nginx path prefix: "/opt/nginx" nginx binary file: "/opt/nginx/sbin/nginx" nginx modules path: "/opt/nginx/modules" nginx configuration prefix: "/opt/nginx/conf" nginx configuration file: "/opt/nginx/conf/nginx.conf" nginx pid file: "/opt/nginx/logs/nginx.pid" nginx error log file: "/opt/nginx/logs/error.log" nginx http access log file: "/opt/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" ``` 编译安装。 ``` make && make install ``` 配置`/opt/nginx/conf/nginx.conf`。 ``` http { ... server_tokens off ... } ``` Loading... **需求背景:** 我们在使用nginx作为应用服务器的时候,nginx会在请求的响应头里添加`Server: Nginx 1.19.9`这样的版本信息。 这样就存在了安全隐患,攻击者可以根据特定版本的服务器漏洞展开攻击。 如果能隐藏这个信息,肯定是能提高一些安全性的。也有部分人提出了更变态的要求,不仅隐藏版本号,而且也不要让人知道我用的是Nginx。 使用编译好的二进制文件可以隐藏版本信息,但是不能隐藏服务器是nginx。想完全更改nginx的响应信息需要编译源码。 下面就跟着我的步骤来一起了解一下如何编译Nginx,以及修改Nginx的响应信息吧。 ### 前提条件 我使用CentOS 7系统作为演示环境。 执行下面的命令,安装编译所需依赖。 ``` yum install gcc gcc-c++ openssl-devel pcre-devel zlib-devel ``` 执行下面的名义,检查依赖包是否正确安装。 ``` rpm -qa gcc gcc-c++ openssl-devel pcre-devel zlib-devel ``` ### 开始编译 进入系统源码目录。 ``` cd /usr/local/src ``` 下载编译所需要的源码。 ``` wget https://nginx.org/download/nginx-1.19.9.tar.gz tar xzvf nginx-1.19.9.tar.gz cd nginx-1.19.9 ``` 修改`src/core/nginx.h`。 ``` #define nginx_version 1019009 #define NGINX_VERSION "1.0.0" #define NGINX_VER "SIMAEK/" NGINX_VERSION ``` 修改:src/http/ngx_http_header_filter_module.c ``` static u_char ngx_http_server_string[] = "Server: SIMAEK" 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; ``` 编译配置。 ``` ./configure --prefix=/opt/nginx \ --with-stream \ --with-http_ssl_module ``` > 对于无法使用YUM安装依赖的机器,可以下载好需要的源码包,并在编译的时候指定使用的依赖库。`--with-pcre=/usr/local/src/pcre-8.44 --with-zlib=/usr/local/src/zlib-1.2.11 --with-openssl=/usr/local/src/openssl-1.1.1k` > > 相关下载地址: > > https://zlib.net/zlib-1.2.11.tar.gz > https://ftp.pcre.org/pub/pcre/pcre-8.44.zip > https://www.openssl.org/source/openssl-1.1.1k.tar.gz 结果。 ``` Configuration summary + using PCRE library: /usr/local/src/pcre-8.44 + using OpenSSL library: /usr/local/src/openssl-1.1.1k + using zlib library: /usr/local/src/zlib-1.2.11 nginx path prefix: "/opt/nginx" nginx binary file: "/opt/nginx/sbin/nginx" nginx modules path: "/opt/nginx/modules" nginx configuration prefix: "/opt/nginx/conf" nginx configuration file: "/opt/nginx/conf/nginx.conf" nginx pid file: "/opt/nginx/logs/nginx.pid" nginx error log file: "/opt/nginx/logs/error.log" nginx http access log file: "/opt/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" ``` 编译安装。 ``` make && make install ``` 配置`/opt/nginx/conf/nginx.conf`。 ``` http { ... server_tokens off ... } ``` 最后修改:2023 年 08 月 02 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏