跳到主要内容

宝塔重新编译 Nginx,隐藏 Server 信息和版本信息

· 阅读需 2 分钟
大刘
一个 web 开发者

环境
系统: CentOS Linux 7.6.1810 (Core)
宝塔:免费版 5.9.1
Nginx版本:1.15

需求
隐藏 Nginx 的 server 信息和版本信息

修改配置和源码

  1. 隐藏版本信息

nginx.conf 里面添加

server_tokens off;
  1. 隐藏 server 信息(需要重新编译 Nginx)

进入 Nginx 源码目录 /www/server/nginx/src

# vi src/http/ngx_http_header_filter_module.c

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: webServer" CRLF;
static u_char ngx_http_server_full_string[] = "Server: webServer" CRLF;
static u_char ngx_http_server_build_string[] = "Server: webServer" CRLF;

重新编译 Nginx

由于使用宝塔安装 Nginx,所以可以参考宝塔安装脚本:/www/server/panel/install/nginx.sh

...
if [ "${version}" == "1.15" ];then
./configure --user=www --group=www --prefix=${Setup_Path} --with-openssl=${Setup_Path}/src/openssl --add-module=${Setup_Path}/src/ngx_devel_kit --add-module=${Setup_Path}/src/lua_nginx_module --add-module=${Setup_Path}/src/ngx_cache_purge --add-module=${Setup_Path}/src/nginx-sticky-module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt="-Wl,-E" --with-openssl-opt="enable-tls1_3 enable-weak-ssl-ciphers" --with-cc-opt="-Wno-error" ${jemallocLD}
...

${Setup_Path} 改为 /www/server/nginx
${jemallocLD} 改为 --with-ld-opt="-ljemalloc"

编译 Nginx

# cd /www/server/nginx/src
# ./configure --user=www --group=www --prefix=/www/server/nginx --with-openssl=/www/server/nginx/src/openssl --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-stream --with-stream_ssl_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt="-Wl,-E" --with-openssl-opt="enable-tls1_3 enable-weak-ssl-ciphers" --with-cc-opt="-Wno-error" --with-ld-opt="-ljemalloc"
# make && make install

重启 Nginx 即可