nginx基本安全优化
一、调整参数隐藏nginx软件版本号信息
查看nginx版本信息:
1 2 3 4 5 6 7 8 9 10 | [root@nginx conf]# curl -I 192.168.200.102 HTTP/1.1 200 OK Server: nginx/1.8.1 #这里显示了nginx的版本号即软件名称; Date: Fri, 31 Aug 2018 09:20:47 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 31 Aug 2018 08:41:19 GMT Connection: keep-alive ETag: "5b88ff2f-264" Accept-Ranges: bytes |
隐藏nginx版本号只需要在nginx.conf文件中的http标签段内加入“server_tokens off”参数即可。
server_tokens参数的官方说明如下:
1 2 3 4 5 | syntax: server_tokens on | off; #此行为参数语法, on 为开启状态,off为关闭状态; default : server_tokens on ; #此行意思是不配置改参数,软件默认情况的结果; context: http,server,location #此行为server_tokens参数可以放置的位置; 参数作用:激活或禁止nginx的版本信息显示在报错信息和server的响应首部位置中; Enables or disables emitting of nginx version in error messages and inter "Server" response header field. #此行是参数的原文作用; |
官方资料地址:
操作如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | [root@nginx conf]# cat nginx.conf worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on ; server_tokens off; #加入这一行即可; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } |
再次查看,nginx的版本信息就隐藏了:
1 2 3 4 5 6 7 8 9 10 11 12 | [root@nginx conf]# systemctl reload nginx [root@nginx conf]# curl -I 192.168.200.102 HTTP/1.1 200 OK Server: nginx Date: Fri, 31 Aug 2018 09:34:16 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 31 Aug 2018 08:41:19 GMT Connection: keep-alive ETag: "5b88ff2f-264" Accept-Ranges: bytes |
二、更改源码隐藏nginx软件名
建议在编译安装前修改,安装后修改软件名,需要重新编译。
第一步:依次修改3个nginx源码文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 修改nginx.h文件 路径为:nginx源码包下面的 src/core/nginx.h 修改后如下: ... #define NGINX_VERSION "1.0" #define NGINX_VER "Web" NGINX_VERSION ... #define NGINX_VAR "Web" ... 修改ngx_http_header_filter_module.c文件 路径为:nginx源码包下面的 src/http/ngx_http_header_filter_module.c 修改第49行,修改后如下: static char ngx_http_server_string[] = "Server: Web" CRLF; 也可以 通过sed替换修改,命令如下: sed -i '49 $#nginx#Web#g' ngx_http_header_filter_module.c 修改ngx_http_header_filter_module.c文件 路径为:nginx源码包下面的 src/http/ngx_http_header_filter_module.c 修改后如下: 第22行: "<hr><center>Web</center>" CRLF 第29行: "<hr><center>Web</center>" CRLF |
第二步:修改后编译软件使其生效(如果是已经安装好的,需要重新编译。)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | systemctl stop nginx ./configure --user=www -- group =www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module make && make install systemctl start nginx [root@nginx nginx-1.8.1]# curl -I 192.168.200.102 HTTP/1.1 200 OK Server: Web #服务名称和版本号已隐藏; Date: Fri, 31 Aug 2018 15:00:46 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Fri, 31 Aug 2018 08:41:19 GMT Connection: keep-alive ETag: "5b88ff2f-264" Accept-Ranges: bytes |
三、更改nginx服务的默认用户
查看nginx服务对应的默认用户,一般情况下,nginx服务启动后,默认使用的用户是nobody,查看默认的配置文件,如下:
1 2 | [root@nginx conf]# grep '#user' nginx.conf. default #user nobody; |
更改nginx的默认用户,操作如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 一、为nginx服务建立新用户 [root@nginx conf]# useradd www -s /sbin/nologin -M [root@nginx conf]# id www uid=1002(www) gid=1002(www) 组=1002(www) 二、配置nginx服务,让其使用刚建立的nginx用户 第一种为直接更改配置文件参数,将默认的#user nobody;改为如下内容: user nginx nginx 第二种方法为直接在编译nginx软件是指定编译的用户和组,命令如下: ./configure --user=www -- group =www --prefix=/application/nginx-1.8.1 --with-http_stub_status_module --with-http_ssl_module 三、检查更改用户的效果 [root@nginx conf]# ps -ef |grep nginx|grep -v frep root 7360 1 0 23:00 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx www 7362 7360 0 23:00 ? 00:00:00 nginx: worker process root 7416 1271 0 23:16 pts/0 00:00:00 grep --color=auto nginx |
通过查看更改后的nginx进程,可以看到worker processes进程对应的用户都变成了nginx。当然,nginx的主进程还是以root身份运行的,后面的文章中会有更改root主进程服务用户的深度安全优化与架构技巧。
******************************我也想难过的时候到海边走走,可是我的城市没有海。******************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY