背景
为提升线上环境的安全性,常见的做法之一是隐藏Nginx
的版本号,以减少潜在攻击者的信息收集。尽管可以通过 server_tokens off
参数来隐藏版本号,但这仍然会暴露当前使用的是Nginx
服务。为了更彻底地解决这个问题,需要深入源代码进行处理。以下介绍具体操作
下载源码
[root@C8_201 ~] [root@C8_201 ~] [root@C8_201 soft]
|
[root@C8_201 soft]# tar -zxvf nginx-1.16.1.tar.gz
|
改源文件
[root@C8_201 soft] [root@C8_201 nginx-1.16.1] [root@C8_201 core]
|
... 14 15 16 17 18 19 20 21 22
|
[root@C8_201 core] [root@C8_201 src] [root@C8_201 http]
|
49 static u_char ngx_http_server_string[] = "Server: nues" CRLF; 50 51 static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; 52 static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
|
35 static u_char ngx_http_error_tail[] = 36 "<hr><center>nues</center>" CRLF
|
编译安装
[root@C8_201 ~] [root@C8_201 ~] [root@C8_201 nginx-1.16.1]
|
Configuration summary + using threads + using system PCRE library + using system OpenSSL library + using system zlib library
nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx" nginx configuration file: "/usr/local/nginx/nginx.conf" nginx pid file: "/usr/local/nginx/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
|
启动服务
[root@C8_201 ~] [root@C8_201 sbin] nginx: [emerg] getpwnam("appadmin") failed [root@C8_201 sbin] nginx [root@C8_201 sbin] [root@C8_201 sbin]
[root@C8_201 sbin] PID TTY TIME CMD 264942 ? 00:00:00 nginx 264943 ? 00:00:00 nginx [root@C8_201 sbin] LISTEN 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=264943,fd=9),("nginx",pid=264942,fd=9))
|
关闭防火墙或放行80端口
[root@C8_201 sbin] success [root@C8_201 sbin] success
|
验证效果
[root@C8_201 sbin] HTTP/1.1 200 OK Server: nues1.00 Date: Wed, 30 Aug 2023 08:32:32 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Wed, 30 Aug 2023 08:26:07 GMT Connection: keep-alive ETag: "64eefd1f-264" Accept-Ranges: bytes
|