Nginx优化实战--基本安全优化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wh2691259/article/details/72814419

1.调整参数隐藏Nginx软件版本号信息

软件的漏洞都和版本有关。因此我们应尽量隐藏或消除Web服务对访问用户显示各类敏感信息(如Web软件名称及版本号等信息),这样恶意的用户就不会跟局软件版本漏洞来攻击,从而加强Web服务的安全性。

☁  ~  curl -I www.gzsteam.cn
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3   #<== 这里很清晰地暴露了Web版本号以及软件名称 
Date: Tue, 30 May 2017 13:52:23 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: https://www.gzsteam.cn/

隐藏版本号的方法:在nginx.conf的http标签段内加入server_tokens off 参数

http 
{
    ......
    server_tokens off
    ......
}

重启nginx,运行结果如下:

☁  centstead [master] curl -I www.gzsteam.cn
HTTP/1.1 301 Moved Permanently
Server: nginx    #<== 这里没有版本号了
Date: Tue, 30 May 2017 14:07:55 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://www.gzsteam.cn/

2.更改源码隐藏Nginx软件名及版本号

  1. 第一步是修改依次修改3个Nginx源码文件。
#nginx-1.12.0/src/core/nginx.h 13-17行左右

#define NGINX_VERSION   *1.12.0*  #<==修改成你想要显示版本号如2.2.0
#define NGINX_VER       *nginx/* NGINX_VERSION #<==修改成想要的软件名称,如百度的bfe 自己随便写,如hank/*
#define NGINX_VAR       *NGINX*  #<==对应上面的修改hank

#nginx-1.12.0/src/http/ngx_http_header_filter_module.c 49行左右

static char ngx_http_server_string[] = "Server:nginx " CRLF; #<==修改成Server: hank " CRLF

#nginx-1.12.0/src/http/ngx_http_special_response.c

"<hr><center>" NGINX_VER"</center>" CRLF #<==此处修改" NGINX_VER"(http://[email protected]

"<hr><center>Nginx</center>" CRLF #<==此处修改成hank

2.修改后编译软件,使其生效

3.更改Nginx服务的默认用户

nginx默认配置文件是nginx.conf.default

[root@vultr conf]# grep "#user" nginx.conf
#user  nobody;

防止被攻击者猜到这个Web服务用户,我们需要修改成特殊的用户名。如nginx

  • 为Nginx服务建立新用户
useradd nginx -s /sbin/nologin -M
  • 配置Nginx服务,让其使用刚建立的nginx用户(nginx.conf)
user nginx nginx;

另外可以在编译的时候,直接指定用户和用户组

./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  • 检查更改用户的效果

重新加载配置后,检查Nginx服务进程的对应用户

[root@vultr conf]# ps -ef|grep nginx|grep -v grep
root       911     1  0 323 ?       00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     2648   404  0 323 ?       00:00:13 php-fpm: pool www
nginx     6342   404  0 426 ?       00:00:04 php-fpm: pool www
nginx     9040   911  0 14:07 ?        00:00:00 nginx: worker process

猜你喜欢

转载自blog.csdn.net/wh2691259/article/details/72814419
今日推荐