为了防止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. opencv2函数学习之blur,GaussianBlur,medianBlur和bilateralFilter:实现图像平滑处理

    在opencv2中,可能使用blur对图像进行平滑处理,这种方法就是最简单的求平均数. 平滑 也称 模糊, 是一项简单且使用频率很高的图像处理方法. 平滑处理的用途有很多, 但是在很多地方我们仅仅关注 ...

  2. CentOS6.7-64bit编译hadoop2.6.4

    1.下载maven(apache-maven-3.3.3-bin.tar.gz) http://archive.apache.org/dist/maven/maven-3/3.3.3/binaries ...

  3. React VR 技术开发群 579149907

    React VR 技术开发群 579149907,欢迎加入讨论!分享经验!

  4. SpringMVC+FreeMarker+Mybatis 整合

    这个项目自己有时写写测试代码什么的, 代码比较简单,代码已放在 github : https://github.com/zhouyongtao/homeinns-web 目录结构: 贴一下Maven的 ...

  5. Android _关于fragment切换重新加载的解决分享给大家

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment但是,这样会有一个问题 ,应该很多朋友都遇到过:每次切换的时候,Fragment都会重新实例化,也就是运行 ...

  6. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(三):搭建开发环境

    生成项目模板 登录Spring Initializr生成Spring Boot项目模板,保存到本地. 地址:https://start.spring.io/ 导入Maven项目 使用IDE导入生成的M ...

  7. linux内核学习之全局描述符表(GDT)(二)

    来源:https://www.cnblogs.com/longintchar/p/5224406.html 在进入保护模式之前,我们先要学习一些基础知识.今天我们看一下全局描述符表(Global De ...

  8. solr(二) : 整合ik-analyzer

    一. 问题: 在使用solr时, 分词器解析中文的时候, 是一个一个字解析的. 这并不是我们想要的结果. 而在lucene中, 使用的中文分词器是 IKAnalyzer. 那么在solr里面, 是不是 ...

  9. PTA (Advanced Level) 1006 Sign In and Sign Out

    Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...

  10. MySQL中的三中循环 while 、 loop 、repeat 求 1~n 的和

    -- MySQL中的三中循环 while . loop .repeat 求 1-n 的和 -- 第一种 while 循环 -- 求 1-n 的和/* while循环语法:while 条件 DO 循环体 ...