上一篇文章我们搭建了Nginx,请求响应头如下
|
1 2 3 4 5 6 7 8 9 10 |
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090 HTTP/1.1 200 OK Server: nginx/1.9.9 Date: Fri, 11 Nov 2022 14:56:38 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 11 Nov 2022 12:47:59 GMT Connection: keep-alive ETag: "636e447f-264" Accept-Ranges: bytes |
可看到这么一行 Server: nginx/1.9.9,暴露了服务为Nginx并且还知道了具体版本号,如果有人想要攻击我们网站,那么他们就会通过这种方式来获取我们网站的一些信息。比如 知道了是Nginx,并且如果恰好发现该版本是有一些漏洞的,那么攻击者就能够很轻松的找到攻击我们的方案,所以隐藏一些信息是很有必要的。
Nginx它考虑到了这方面的问题。给我们提供了一个配置 server_tokens。将该配置放到http快中就可以隐藏版本号了。
修改 nginx.conf,添加server_tokens,配置如下
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
worker_processes 1;
events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream; server_tokens off; sendfile on;
keepalive_timeout 65;
server { listen 8090; server_name localhost;
location / { root html; index index.html index.htm; } }
} |
重启nginx
版本号已隐藏
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[nginx@node01 sbin]$ ./nginx -s reload
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090 HTTP/1.1 200 OK Server: nginx Date: Fri, 11 Nov 2022 15:08:55 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 11 Nov 2022 12:47:59 GMT Connection: keep-alive ETag: "636e447f-264" Accept-Ranges: bytes |
如果搭建的是 php-fpm 服务器的话,还得修改 fastcgi.conf
在该配置中有这么一行
|
1 2 3 |
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; 修改后 fastcgi_param SERVER_SOFTWARE nginx; |
经过上面的修改,版本号就已经隐藏了,如果连Server信息都不想让别人知道,那就只能修改源码了
修改C文件 src/http/ngx_http_header_filter_module.c
大概在50行左右,将nginx修改为 其它名字
|
1 2 3 4 |
//static char ngx_http_server_string[] = "Server: nginx" CRLF; //static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; static char ngx_http_server_string[] = "Server: juan" CRLF; static char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF; |
重新编译前记得停掉Nginx 备份自己的nginx.conf
编译完后替换之前备份的文件,启动Nginx
|
1 2 3 4 5 6 7 8 9 10 |
[nginx@node01 sbin]$ curl -I 127.0.0.1:8090 HTTP/1.1 200 OK Server: juan Date: Fri, 11 Nov 2022 15:34:27 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 11 Nov 2022 15:30:46 GMT Connection: keep-alive ETag: "636e6aa6-264" Accept-Ranges: bytes |
这时Server已经变成自己定义的名字了,nginx的加固就介绍到这。