Nginx(OpenResty) 去掉默认错误页面中的版本号

Nginx OpenResty License About 1,757 words

说明

修改Nginx的源码才能去除,所以必须通过编译安装,yumapt-get方式无法修改。

适用于OpenResty

NginxOpenResty的开源协议为BSD-2-Clause,所以修改后可以不开源修改部分的代码。

查找页面位置

错误页面是硬编码在ngx_http_special_response.c文件中。

寻找文件位置:

locate ngx_http_special_response.c

Nginx路径如下:

nginx-1.19.3/src/http/ngx_http_special_response.c

OpenResty路径如下:

openresty-1.19.3.1/bundle/nginx-1.19.3/src/http/ngx_http_special_response.c

修改源码

ngx_http_special_response.c文件的2229行,可在vim中使用:nn为行数)跳转至如下代码,或搜索NGINX_VER关键词。

NGINX_VERNGINX_VER_BUILD替换为自己想要的字符串,或者删除也可以。

修改前代码:

static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

static u_char ngx_http_error_build_tail[] =
"<hr><center>" NGINX_VER_BUILD "</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

修改后代码(修改字符串):

static u_char ngx_http_error_full_tail[] =
"<hr><center>hello world</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

static u_char ngx_http_error_build_tail[] =
"<hr><center>hello world 123</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

修改后代码(去除Nginx名称和版本号):

static u_char ngx_http_error_full_tail[] =
"</body>" CRLF
"</html>" CRLF
;

static u_char ngx_http_error_build_tail[] =
"</body>" CRLF
"</html>" CRLF
;

OpenResty还多了如下代码(未改动前第36行):

static u_char ngx_http_error_tail[] =
"<hr><center>openresty</center>" CRLF
"</body>" CRLF
"</html>" CRLF
;

去除后代码:

static u_char ngx_http_error_tail[] =
"</body>" CRLF
"</html>" CRLF
;

备注

OpenResty的错误页面源码路径如下

openresty-1.19.3.1/build/nginx-1.19.3/src/http/ngx_http_special_response.c

源码

https://github.com/nginx/nginx/blob/branches/default/src/http/ngx_http_special_response.c#L21

Views: 2,484 · Posted: 2021-05-11

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh