一、Nginx基本安全优化
1.调整参数隐藏Nginx版本号信息
一般来说,软件的漏洞和版本号有关,我们应该尽量隐藏或消除Web服务对访问用户显示各类敏感信息,
加强Web服务的安全性。
查询Nginx的版本号:curl -I URL
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3
Date: Sun, 30 Sep 2018 01:01:30 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="hty training"
编辑配置文件增加参数,实现隐藏版本号
在Nginx配置文件下nginx.conf中的http标签段加入“server_tokens off;”
http{
...
server_tokens off;
...
}
重启Nginx并查看
/application/nginx/sbin/nginx -s reload
curl -I 182.61.48.89
结果如下:
HTTP/1.1 401 Unauthorized
Server: nginx ##隐藏了版本号
Date: Sun, 30 Sep 2018 01:18:49 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="hty training"
2.更改源码隐藏Nginx软件名及版本号
1)一次修改3个Nginx源码文件
第一个文件为nginx-1.6.3/src/core/nginx.h
操作命令集:
cd /home/hty/tools/nginx-1.6.3/src/core
#define NGINX_VERSION "1.6.3" #<==修改为想要显示的版本号,如2.2.23
#define NGINX_VER "nginx/" NGINX_VERSION #<==将nginx修改为想要修改的软件名称,如“OWS”
#define NGINX_VAR "NGINX" #<==将nginx修改为想要修改的软件名称,如“OWS”
#define NGX_OLDPID_EXT ".oldbin"
第二个文件是nginx-1.6.3/src/http/ngx_http_header_filter_mudule.c的第49行
cd /home/hty/tools/nginx-1.6.3/src/http
grep -n 'Server:nginx' ngx_http_header_filter_mudule.c #显示第49行内容
sed -i 's#Server: nginx#Server: OWS#g' ngx_http_header_filter_mudule.c #<==将Server:nginx 替换为Server: OWS
第三个文件nginx-1.6.3/src/http/ngx_http_special_response.c
cd /home/hty/tools/nginx-1.6.3/src/http
sed -n '21,30p' ngx_http_special_response.c
将其中的内容"<hr><center>NGINX_VER</center>"CRLF修改为"<hr><center>NGINX_VER(http://oldboy.blog.51cto.com)</center>"CRLF,
"<hr><center>Nginx</center>"CRLF修改"<hr><center>OWS</center>"CRLF
2)修改编译后,使其生效
修改后,编译安装软件
3.更改Nginx的默认用户
为了软件安全,尽可能改到所有配置,包括端口、用户
1)更改默认用户
在conf目录下,查看默认配置文件
grep '#user' nginx.conf.default
(1)为Nginx服务建立新用户
useradd nginx -s /sbin/nologin -M #不需要有系统登录权限,禁止其登录能力
id nginx
(2)配置Nginx服务,让其使用刚建立的nginx用户
方法一、直接更改配置文件参数,将默认#user nobody;改为
user nignx nginx
方法二、直接在编译Nginx软件时指定编译的用户和组
./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.3 --with-http_stub_status_module --with-http_ssl_module
(3)检查更改用户的效果
ps -ef|grep nginx|grep -v grep
结果如下:
[root@instance-yf0xzby9 conf]# ps -ef|grep nginx|grep -v grep
root 1768 1 0 Sep26 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 2396 1768 0 Sep26 ? 00:00:00 nginx: worker process
root 2428 1 0 Sep26 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx 87285 87157 0 Sep29 ? 00:00:02 php-fpm: pool www
nginx 87308 87157 0 Sep29 ? 00:00:01 php-fpm: pool www
nginx 87309 87157 0 Sep29 ? 00:00:01 php-fpm: pool www
nginx 88530 2428 0 09:17 ? 00:00:00 nginx: worker process