宝塔重新编译nginx,隐藏server信息和版本信息

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

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

修改配置和源码

1.隐藏版本信息

在nginx.conf里面添加

server_tokens off;

2.隐藏server信息(需要重新编译ngnix)

进入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即可

爱生活,爱技术

You may also like...

发表评论

电子邮件地址不会被公开。 必填项已用*标注