如何在 CentOS 中从源代码编译后自定义 Nginx 服务器名称

介绍


作为关于 如何从源编译 nginx的文章的后续内容,本教程可以帮助您定制您的主机上的服务器名称. 通常,公司会为了安全原因修改服务器名称。

定制 nginx 服务器的名称需要修改源代码(本教程将逐步指导您),并需要从前一篇文章中重新编译。

查找您的服务器版本


1curl -I http://example.com/

HTTP/1.1 200 OK 服务器: nginx/1.5.6 # <-- 这是你目前使用的 nginx 的版本 日期: 十一月十一日,2013年十一月十一日, 20:40:18 GMT 内容类型: 文本/html 内容长度: 612 最后修改: 十一月十一日, 2013年十一月十一日, 20:37:02 GMT 连接: 保持活着 ETag: "51f18c6e-264" 接受范围: 字节

更改 Nginx 服务器序列


您应该在下载源代码部分后查看以前的教程 从源编译

1cd ~/src/nginx/
2vi +49 src/http/ngx_http_header_filter_module.c

找出线条:

1static char ngx_http_server_string[] = "Server: nginx" CRLF;
2static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;

更改为:

1static char ngx_http_server_string[] = "Server: the-ocean" CRLF;
2static char ngx_http_server_full_string[] = "Server: the-ocean" CRLF;

重新编译 Nginx 用新选项


您需要按照 此指南查看配置选项或从命令行历史搜索:

1./configure ... 
2make
3make install

停止在配置中显示服务器版本


1vi +19 /etc/nginx/nginx.conf

在 http 配置下添加行. 重复为 https 如果您有部分

1http {
2...
3server_tokens off;
4....

重启 Nginx 服务


我們需要重新啟動 nginx 因為 nginx 檔案已經變更:

1service nginx restart

检查结果


让我们看看我们现在是否看到服务器信息:

1curl -I http://example.com/

HTTP/1.1 200 OK Server: the-ocean 日期: 十一月17日 2013 20:50:17 GMT 内容类型: text/html 内容长度: 612 最后修改: 十一月17日 2013 20:37:02 GMT 连接: keep-alive ETag: "51f18c6e-264" 接受范围:字节

Published At
Categories with 技术
comments powered by Disqus