为了防止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. POJ 2643

    #include<iostream> #include<stdio.h> #include<string> #include<algorithm> #d ...

  2. Bash控制结构

    1. if-else语句 #!/bin/bash #if ... fi 语句: if [ $a != $b ] then echo "a != b" fi #if ... else ...

  3. SQL Server性能优化(7)理解数据库文件组织

    一.基本单位"页"     SQL Server是用8KB的页来存储数据.物理I/O操作也是在页级执行.页的种类有很多,具体参考(MSDN).我们关注更多的是数据页的结构,包括三部 ...

  4. windows下通过VNC图形化访问Ubuntu桌面环境

    要在windows下图形化访问Ubuntu或其它Linux系统桌面环境有很多方法,我比较喜欢的是使用VNC服务,需要在Ubuntu下安装vncserver和在windows下安装客户端访问工具. 1. ...

  5. spring boot实现ssm(1)功能

    前面完成了ssm的整合, 整个过程可以说很繁杂, 各种配置, 很容易让人晕掉. 这里使用spring boot 的方式来实现ssm(1)中的功能. 一. 建项目 1. 使用 idea 来创建 spri ...

  6. KVM部署及硬件参数更改

    一.kvm虚拟化环境搭建准备 1.硬件环境 kvm只能部署在物理机上面. 2.BIOS开启VT Virtualization Tech [Enabled] 3.查看cpu 是否支持kvm 全虚拟化 # ...

  7. 一次Linux服务器被入侵和删除木马程序的经历

    转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://wzlinux.blog.51cto.com/8021085/1740113 一.背景 晚上看到有台服 ...

  8. Spring Actuator源码分析(转)

    转自:http://blog.csdn.net/wsscy2004/article/details/50166333 Actuator Endpoint Actuator模块通过Endpoint暴露一 ...

  9. 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)

    目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 github. 描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回 ...

  10. 网络之XML解析-GData

    GDataXML是一套Google使用Objective-C开发的DOM方式XML解析类库,支持读取和修改XML文档,支持XPath方式查询, GDataXML 是 iOS 下的一款轻量级 XML 解 ...