Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 小程序 Arthas JVM AQS juc Kubernetes Docker 诊断工具


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

Nginx OpenResty License 大约 1757 字

说明

修改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

阅读 1299 · 发布于 2021-05-12

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

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

扫描二维码关注我
昵称:
随便看看 换一批