一、调整参数隐藏nginx软件版本号信息
查看nginx版本信息:
[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参数的官方说明如下:
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. #此行是参数的原文作用;
官方资料地址:
操作如下:
[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的版本信息就隐藏了:
[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源码文件:
修改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
第二步:修改后编译软件使其生效(如果是已经安装好的,需要重新编译。)
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,查看默认的配置文件,如下:
[root@nginx conf]# grep '#user' nginx.conf.default
#user nobody;
更改nginx的默认用户,操作如下:
一、为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主进程服务用户的深度安全优化与架构技巧。