nginx基本安全优化

1、调整参数隐藏nginx软件版本号信息

查看nginx版本信息:html

[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”参数便可。nginx

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.    #此行是参数的原文做用;

官方资料地址:架构

操做以下:app

[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的版本信息就隐藏了:curl

[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

 2、更改源码隐藏nginx软件名

建议在编译安装前修改,安装后修改软件名,须要从新编译。优化

第一步:依次修改3个nginx源码文件:ui

修改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

 第二步:修改后编译软件使其生效(若是是已经安装好的,须要从新编译。)url

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

 3、更改nginx服务的默认用户

 查看nginx服务对应的默认用户,通常状况下,nginx服务启动后,默认使用的用户是nobody,查看默认的配置文件,以下:spa

[root@nginx conf]# grep '#user' nginx.conf.default
#user  nobody;

 更改nginx的默认用户,操做以下:

1、为nginx服务创建新用户
[root@nginx conf]# useradd www -s /sbin/nologin -M
[root@nginx conf]# id www
uid=1002(www) gid=1002(www) 组=1002(www)

2、配置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

3、检查更改用户的效果
[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主进程服务用户的深度安全优化与架构技巧。

相关文章
相关标签/搜索