为了防止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. linux 下NFS远程目录挂载

    NFS 是Network File System的缩写,中文意思是网络文件系统.它的主要功能是通过网络(一般是局域网)让不同的主机系统之间可以共享文件或目录.NFS客户端(一般为应用服务器,例如web ...

  2. mysql添加类似oracle的伪列及查看表信息

    sql格式: AS rownum, table_name.* ) r, table_name; AS rownum, table_name.字段1, table_name.字段2, table_nam ...

  3. 让机器说话(文字转美女语音,擅长中英文哦),大小600K(免费下载)!

    机器人之路的第二小步:说话(文字转语音美女哦),大小600K(免费下载)! 机器人之路的第二小步:说话(文字转语音美女哦),准确率特别高,普通话标准,中英文都可以说,大家可以体验一下,请下载到电脑上在 ...

  4. 课程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)——2.Programming Assignments: Building your Deep Neural Network: Step by Step

    Building your Deep Neural Network: Step by Step Welcome to your third programming exercise of the de ...

  5. dubbo-常用配置

    一.启动时检查 官网说明: Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true&q ...

  6. 编程珠玑第一章的算法,Java实现,通俗易懂

    该算法也就是所谓的位图算法,用一个int表示32位,也就是实际值为1~32的数. 按照书里说的, 该算法只适合内存有限,而磁盘和时间不限,且数字在1~MAX之间不重复的排序. package demo ...

  7. 前端通信:ajax设计方案(八)--- 设计请求池,复用请求,让前端通信快、更快、再快一点

    直接进入主题,本篇文章有点长,包括从设计阶段,到摸索阶段,再到实现阶段,最后全面覆盖测试阶段(包括数据搜集清洗),还有与主流前端通信框架进行对比PK阶段. 首先介绍一下一些概念: 1. 浏览器的并发能 ...

  8. 网络安全事件频发,全站HTTPS势在必行

    报告显示,2015年互联网应急中心发现网络安全事件超过12万起,同比增长125.9%.消息一出震惊国人. 网络安全事件频发,全站HTTPS势在必行 正如习主席所讲:“互联网给人们的生产生活带来巨大变化 ...

  9. double转换为int以及浮点型相加损失精度问题

    最近在做支付相关模块的业务,数据库字段却使用的是double类型,其实也行,只要计算不在sql语句中进行,也是没有问题的. 预先的类属性设置的是Double类型,自己算的时候发现小数相加会出现损失精度 ...

  10. dom操作------创建节点/插入节点

    <section> <div id="box" style="position: relative;"> <p id=" ...