为了防止nginx出现软件漏洞,我们要对nginx软件服务加强一些安全性,下面就介绍一下基本的安全优化

1、隐藏nginx版本号:

  想要隐藏,首先我们要了解所使用软件的版本号,我们可以在Linux中查看这个版本号,方法如下:

[root@Nginx ~]# curl -I 127.0.0.1          # 查看方法
HTTP/1.1 401 Unauthorized
Server: nginx/1.6.3 # 版本信息:为nginx/1.6.3
Date: Fri, 23 Mar 2018 02:42:46 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="brian training"

  当我们在windows上面访问一个不存在的地址就会抛出下面的404错误,也直接的暴露了web服务的版本信息

  这样肯定是不安全的,我们就要把敏感信息隐藏起来

  修改nginx.conf主配置文件(添加红色标记):

worker_processes  ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens off;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout ;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  server_tokens参数说明:

语法:server_tokens   on | off;    on为开启,off关闭
默认值:server_tokens on; 为开启状态
位置:http、server、location ; 为server_tokens 参数可存放的位置

  修改完成后检查语法:

[root@Nginx conf]# ../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启:

[root@Nginx conf]# ../sbin/nginx -s reload

  测试结果:

[root@Nginx conf]# curl -I 127.0.0.1
HTTP/1.1 401 Unauthorized
Server: nginx                # 很明显敏感版本号已经隐藏
Date: Fri, 23 Mar 2018 03:01:54 GMT
Content-Type: text/html
Content-Length: 188
Connection: keep-alive
WWW-Authenticate: Basic realm="brian training"

2、修改nginx的版本信息:

  我们上面刚刚只是对敏感的版本号做了隐藏 为了更加的安全我们可以把剩下的nginx也隐藏或者修改,这个就需要去修改nginx的源码了(这个修改是没有参数和入口的),修改方法如下:

  1、首先我们要依次的修改三个源码文件:(注:这里所说的源码文件是没有编译过的文件,就是我们把安装包解压后的原始文件)

  文件路径在:

nginx.h文件:路径:/home/nginx/tools/nginx-1.6.3/src/core/nginx.h

ngx_http_header_filter_module.c文件: 路径:/home/nginx/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c

ngx_http_special_response.c文件:路径:/home/nginx/tools/nginx-1.6.3/src/http/ngx_http_special_response.c

  2、下面就对每个文件进行修改:

  nginx.h文件原始内容:(只取我们要修改的信息)

[root@Nginx core]# sed -n "13,17p" /home/nginx/tools/nginx-1.6.3/src/core/nginx.h    # 对nginx.h文件取出我们想要的信息
#define NGINX_VERSION "1.6.3"                               # 版本号
#define NGINX_VER "nginx/" NGINX_VERSION                      # 软件名
  
#define NGINX_VAR "NGINX"                                # 软件名
#define NGX_OLDPID_EXT ".oldbin"

  nginx.h文件修改后的内容:

[root@Nginx core]# sed -n "13,17p" /home/nginx/tools/nginx-1.6.3/src/core/nginx.h
#define NGINX_VERSION "10.10.10"
#define NGINX_VER "Brian/" NGINX_VERSION #define NGINX_VAR "Brian"
#define NGX_OLDPID_EXT ".oldbin"

  ngx_http_header_filter_module.c文件原始内容:(只取我们要修改的内容)

[root@Nginx core]# grep -n 'Server: nginx' /home/nginx/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c
49:static char ngx_http_server_string[] = "Server: nginx" CRLF; # 修改最后一个nginx,为我们想要修改的内容

  ngx_http_header_filter_module.c文件修改后内容:

[root@Nginx core]# sed -i  's#Server: nginx#Server: Brian#g' /home/nginx/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c    # 修改
[root@Nginx core]# grep -n 'Server: Brian' /home/nginx/tools/nginx-1.6.3/src/http/ngx_http_header_filter_module.c              # 查看结果
49:static char ngx_http_server_string[] = "Server: Brian" CRLF;

  ngx_http_special_response.c文件原始内容:(只取我们要修改的内容)

[root@Nginx core]# sed -n "21,30p" /home/nginx/tools/nginx-1.6.3/src/http/ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "</center>" CRLF            # 此处要修改
"</body>" CRLF
"</html>" CRLF
; static u_char ngx_http_error_tail[] =
"<hr><center>nginx</center>" CRLF                 # 此处要修改
"</body>" CRLF

  ngx_http_special_response.c文件修改后内容:

[root@Nginx core]# sed -n "21,30p" /home/nginx/tools/nginx-1.6.3/src/http/ngx_http_special_response.c
static u_char ngx_http_error_full_tail[] =
"<hr><center>" NGINX_VER "(http://www.cnblogs.com/brianzhu/)</center>" CRLF
"</body>" CRLF
"</html>" CRLF
; static u_char ngx_http_error_tail[] =
"<hr><center>Brian</center>" CRLF
"</body>" CRLF

  3、修改完成后,我们就可以编译安装了(之前已经编译好的,可以重新编译安装,过程详情:点击这里)

  4、编译完成后,我们就可以检测语法、启动nginx 、测试了:

[root@Nginx nginx]# sbin/nginx -t                                            # 语法检查
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful
[root@Nginx nginx]# sbin/nginx                             # 启动
[root@Nginx nginx]# netstat -lntup | grep nginx                   # 检查启动状态
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 31719/nginx: master

  5、测试:(看最后的显示结果,已经改成我们在源码文件中 修改后的样子了)

 Linux测试:

[root@Nginx conf]# curl -I 127.0.0.1
HTTP/1.1 401 Unauthorized
Server: Brian/10.10.10                    # 已经修改成功
Date: Fri, 23 Mar 2018 06:12:59 GMT
Content-Type: text/html
Content-Length: 231
Connection: keep-alive
WWW-Authenticate: Basic realm="brian training" 

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

  这里简单的说一下更改默认用户的方法,其目的也是为了保证安全:

  在修改默认用户之前,必须保证用户在系统中存在:

[root@Nginx conf]# useradd nginx -s /sbin/nologin -M          # 创建用户
[root@Nginx conf]# id nginx                          # 检查用户
uid=1000(nginx) gid=1000(nginx) 组=1000(nginx)

  1、编译的时候指定:(在对源码解压后在编译安装的时候指定用户,牵扯到安装的知识了,具体的安装:点击这里)

[root@Nginx nginx-1.6.3]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx-1.6.3/ --with-http_stub_status_module --with-http_ssl_module

  2、修改配置文件:(修改主配置文件nginx.conf)

[root@Nginx conf]# cat nginx.conf
user nginx nginx;                        # 添加本行
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
server_tokens on;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  3、检查效果:

[root@Nginx conf]# ps -ef | grep nginx | grep -v grep
root 31719 1 0 14:05 ? 00:00:00 nginx: master process sbin/nginx
nginx 31732 31719 0 14:11 ? 00:00:00 nginx: worker process

  

 

Nginx基本的安全优化的更多相关文章

  1. 【夯实Nginx基础】Nginx工作原理和优化、漏洞

    本文地址 原文地址 本文提纲: 1.  Nginx的模块与工作原理    2.  Nginx的进程模型    3 . NginxFastCGI运行原理        3.1 什么是 FastCGI   ...

  2. Nginx+PHP-fpm高负载优化及压力测试方法

    Nginx+PHP-fpm组合,以内存占用小,负载能力强壮的特点,成为小内存VPS建站的首选组合.我们一起来探讨一下nginx+php-fpm高负载的优化方法. 先来看看nginx配置参数的优化.ng ...

  3. Nginx工作原理和优化、漏洞

    1.  Nginx的模块与工作原理 第三方模块:HTTP Upstream Request Hash模块.Notice模块和HTTP Access Key模块. 图1-1展示了Nginx模块常规的HT ...

  4. Nginx 工作原理和优化、漏洞

    1.  Nginx的模块与工作原理 Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(locat ...

  5. Nginx工作原理和优化、漏洞(转)

    查看安装了哪些模块命令: [root@RG-PowerCache-X xcache]# nginx/sbin/nginx -Vnginx version: nginx/1.2.3built by gc ...

  6. Nginx如何进行配置优化?

    在日常工作的时候,搭建配置Nginx的时候,我们都会做相应的优化,那一般需要做的配置优化有哪些呢?可能有些小伙伴一听到要进行优化,内心难免有些慌. 今天咱们聊聊Nginx进行常规配置优化,这里需要注意 ...

  7. nginx指令中的优化(配置文件)

    nginx指令中的优化(配置文件)worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数.worker_cpu_affinity 00000001 0000 ...

  8. Nginx错误日志与优化专题

    一.Nginx配置和内核优化 实现突破十万并发 二.一次Nignx的502页面的错误记录 (1)错误页面显示 错误日志: // :: [error] #: * recv() failed (: Con ...

  9. (转)Nginx配置和内核优化 实现突破十万并发

    nginx指令中的优化(配置文件) worker_processes 8; nginx进程数,建议按照cpu数目来指定,一般为它的倍数. worker_cpu_affinity 00000001 00 ...

  10. nginx压力测试和优化配置

    115 yum -y install gcc automake autoconf libtool make 116 yum install ctags 117 mkdir -m 644 -p /usr ...

随机推荐

  1. Dev Label显示不同颜色字体

    labelControl1.Text = "<size=14>Size = 14<br>" + "<b>Bold</b> ...

  2. ThreadLocal的练习代码

    场景: 有三个小孩儿,买了一个变形金刚玩具(Transformer).... 三个小孩都争着玩这个玩具....没有一个人可以玩... 第一种方式:每个人各玩一会.... 第二种方式:再买两个玩具,一个 ...

  3. Java中的日志管理

    日志是应用程序运行中不可缺少的一部分,JAVA中有很多已经成熟的方案,尽管记录日志是应用开发中并不可少的功能,在 JDK 的最初版本中并不包含日志记录相关的 API 和实现.相关的 API(java. ...

  4. 全网最详细的启动Kafka服务时出现kafka.common.InconsistentBrokerIdException: Configured brokerId 3 doesn't match stored brokerId 1 in meta.properties错误的解决办法(图文详解)

    不多说,直接上干货! 问题详情 执行bin/kafka-server-start.sh config/server.properties 时, [kfk@bigdata-pro03 kafka_2.- ...

  5. 修改Hosts文件提示没有权限怎么办

    解决办法:给host文件赋予权限 1.打开电脑C盘,在目录C:\Windows\System32\drivers\etc 下找到hosts文件 2.右键hosts文件,选择属性 3.点击hosts属性 ...

  6. 理解 Generator 的执行

    Generator & yield 开局官宣:sec-generatoryield,这是对yield的介绍. 同样巴拉巴拉列了9条,将以上链接中的说明简化成3条: 1. 在GeneratorF ...

  7. python 生成唯一识别码

    import uuid identity = str(uuid.uuid4()).encode('ascii')

  8. java-构建jar带哟参数提示的

    使用command的cli包构建带有参数提示的jar包 需要引入command cli的依赖 <commons.version>1.2</commons.version> &l ...

  9. scala-传名函数和传值函数

    Scala的解释器在解析函数参数(function arguments)时有两种方式:先计算参数表达式的值(reduce the arguments),再应用到函数内部:或者是将未计算的参数表达式直接 ...

  10. logback打印mybatis sql日志

    近期在项目中调试sql,发现现有的配置 使用logback 无法打印出sql语句,原配置如下(修改为debug也不好使): <!--jdbc --><logger name=&quo ...