Nginx修改服务名称任意名字

时间:2022-07-24
本文章向大家介绍Nginx修改服务名称任意名字,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

下载nginx

wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压源码包

tar zxf nginx-1.18.0.tar.gz

修改nginx源码

nginx服务器名称修改为END

文件一:src/core/nginx.h

14 #define NGINX_VER          "nginx/" NGINX_VERSION
修改为
14 #define NGINX_VER          "end/" NGINX_VERSION

22 #define NGINX_VAR          "NGINX"
修改为
22 #define NGINX_VAR          "END"

文件二:src/http/ngx_http_header_filter_module.c

49 static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
修改为
49 static u_char ngx_http_server_string[] = "Server: end" CRLF;

文件三:src/http/ngx_http_special_response.c

36 "<hr><center>nginx</center>" CRLF
修改为
36 "<hr><center>end</center>" CRLF

文件四:src/http/v2/ngx_http_v2_filter_module.c

480 "http2 output header: "server: nginx"");
修改为
480 "http2 output header: "server: end"");

修改上面4个文件,我们已将nginx服务器名修改为了end,如果不想去找到文件一个一个修改,也可以使用sed命令直接替换:

#执行更名操作
sed -i "s#"NGINX"#"end"#" src/core/nginx.h
sed -i "s#"nginx/"#"END/"#" src/core/nginx.h
sed -i "s#Server: nginx#Server: end#" src/http/ngx_http_header_filter_module.c
sed -i "s#"<hr><center>nginx</center>"#"<hr><center>end</center>"#" src/http/ngx_http_special_response.c
sed -i "s#server: nginx#server: end#"

编译安装

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-pcre --with-http_stub_status_module --with-http_gzip_static_module
make
make install

启动Nginx

/usr/local/nginx/sbin/nginx

测试

[root@linux /]# curl -I 127.0.0.1:80
HTTP/1.1 200 OK
Server: end
Date: Tue, 08 Sep 2020 20:14:17 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Pingback: https://127.0.0.1/action/xmlrpc
Set-Cookie: c77201da990646849f50a39e48e0b070latest_time_id=41; path=/