Linux系统架构-----Nginx的服务优化

目录

 

一.隐藏版本号

二.修改用户和组

三.配置网页缓存时间

四.日志分割

五.设置连接超时


一.隐藏版本号

  • 在生产环境中,需要隐藏Nginx的版本号,以免泄露Nginx的版本,使得攻击者不能针对特定版本进行攻击

查看Nginx的版本有两种方法

  • 使用fiddler工具抓取数据包,查看Nginx版本
  • 在Centos7上使用使用命令  curl -I http://192.168.43.211/ 查看

隐藏Nginx版本号也有两种方法

  • 修改Nginx的源码文件,指定不显示版本号
  • 修改Nginx的主配置文件

隐藏版本号操作

  • 修改源码文件
#解压到/opt目录下
 tar xzvf nginx-1.12.2.tar.gz -C /opt
#进入Nginx的源码文件/opt/nginx-1.12.2/src/core/nginx.h

vim /opt/nginx-1.12.2/src/core/nginx.h


...
#define nginx_version      1012002
#define NGINX_VERSION      "1.1.1"                         //修改版本号
#define NGINX_VER          "nginx/" NGINX_VERSION
...
  • 配置,编译且安装
#安装环境包
yum install gcc gcc-c++ pcre* zlib-devel make -y
#创建用户,不建立宿主文件,且不能再shell上登录
useradd -M -s /sbin/nologin nginx
#配置,安装且编译
cd /opt/nginx-1.12.2/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module 
#nigix运行的用户和组都为nginx
#启用http_stub_status_module模块以支持状态统计,便于查看服务器的连接信息

make && make install

#为主程序nginx创建软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin


 #优化服务控制,service工具
vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: -99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"                  //nginx主程序
PIDF="/usr/local/nginx/logs/nginx.pid"              //nginx的PID号
case "$1" in
        start)
                $PROG
                ;;
        stop)
                kill -s QUIT $(cat $PIDF)
                ;;
        restart)
                $0 stop
                $0 start
                ;;
        reload)
                kill -s HUP $(cat $PIDF)
                ;;
        *)
                echo "Usage: $0 {start|stop|restart|reload}"
        exit 1
esac
exit 0
#添加执行权限
chmod +x /etc/init.d/nginx
#添加为系统服务
chkconfig --add nginx 
#开启服务
service nginx start
#关闭防火墙
systemctl stop firewalld
setenforce 0
  • 使用命令  curl -I http://192.168.43.211/ 查看版本号

[root@localhost nginx-1.12.2]#  curl -I http://192.168.43.211/
HTTP/1.1 200 OK
Server: nginx/1.1.1
Date: Mon, 23 Dec 2019 11:20:21 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 23 Dec 2019 11:11:14 GMT
Connection: keep-alive
ETag: "5e00a0d2-264"
Accept-Ranges: bytes

[root@localhost nginx-1.12.2]# 
  • 使用fiddler工具抓取数据包,查看Nginx版本

  • 修改配置文件

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
...
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
...
[root@localhost nginx]# service nginx stop
[root@localhost nginx]# service nginx start 
[root@localhost nginx]# 

 

二.修改用户和组

  • Nginx在运行时进程需要有用户和组的支持,用于实现对网站读取时的进行访问控制
  • 主进程由root创建,子进程有指定的用户与组创建
  • Nginx默认使用nobody用户账户和组账号

修改Nginx用户和组有两种方法

  • 在编译安装时指定的用户与组
  • 修改配置文件

修改用户和组操作

  • 编译时指定用户和组
#创建用户,不建立宿主文件,且不能再shell上登录
useradd -M -s /sbin/nologin nginx
#配置,安装且编译
cd /opt/nginx-1.12.2/

./configure \
--prefix=/usr/local/nginx \
--user=nginx \                       //指定用户名为nginx
--group=nginx \                      //指定组名为nginx
--with-http_stub_status_module

#需要以前创建用户
  • 修改Nginx的配置文件nginx.conf指定用户和组
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
user  nginx nginx;           //修改用户名为nginx,修改组名为nginx

...
  • 查看Nginx进程运行情况
[root@localhost ~]# ps aux | grep nginx
root      39065  0.0  0.0  20544   612 ?        Ss   19:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     39066  0.0  0.1  23072  1396 ?        S    19:54   0:00 nginx: worker process
root      39358  0.0  0.0 112728   972 pts/0    R+   20:18   0:00 grep --color=auto nginx
[root@localhost ~]# 

 

三.配置网页缓存时间

  • 当Nginx将网页数据返回给给客户端之后,可以设置缓存的时间,方便下次再浏览相同的内容时直接返回,避免重复请求,加快访问速度,一般只针对静态资源设置,对于动态网页不用设置缓存时间
  • 在Nginx服务中,expires参数指定缓存时间
  • 当没有设置expires参数时,使用Fiddler进行抓包

  • 设置expires参数,指定缓存时间
[root@192 ~]# vim /usr/local/nginx/conf/nginx.conf

...
   location ~ \.(gif|jpg|jepg|png)$ {
          root html;
          expires 1d;

        }
...

[root@192 ~]# service nginx stop
[root@192 ~]# service nginx start
[root@192 ~]# 

四.日志分割

  • Nginx没有类似于Apache的cronlog日志分割处理功能,但是可以通过Nginx的信号控制功能脚本来实现日志的自动分割,并且将脚本加入到Linux的计划任务中去,让脚本在每天固定的时间执行,便可以实现切割功能
  • 编写脚本进行日志切割的思路
  • 设置时间变量
  • 设置保存日志路径
  • 将目前的日志文件进行重命名
  • 删除时间过长的日志文件
  • 设置cron任务,定期执行脚本自动进行日志分割
vim /opt/fenge.sh

#!/bin/bahs
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d")
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
#创建日志文件目录
[ -d $logs_path ] || mkdir -p $logs_path
#移动且重命名日志文件
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
#重建新的日志文件
kill -USR1 $(cat $pid_path)
#删除30天之前的日志文件
find $logs_path -mtime +30 |xargs rm -rf 

[root@192 opt]# bash fenge.sh  //执行分割脚本
[root@192 logs]# ls /var/log/nginx  //查看日志文件
test.com-access.log-20191222 
[root@192 logs]# date
2019年 12月 23日 星期一 21:37:53 CST
[root@192 logs]# date -s 2019-12-24  //修改日期
2019年 12月 24日 星期二 00:00:00 CST
[root@192 logs]# bash /opt/fenge.sh 
[root@192 logs]# ls /var/log/nginx    //查看日志文件
test.com-access.log-20191222  test.com-access.log-20191223


#设置crontab任务,每天零点执行脚本
[root@192 logs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@192 logs]# crontab -l
0 0 * * * /opt/fenge.sh
[root@192 logs]# 




 

五.设置连接超时

  • 在企业网站中为了避免同一个客户长时间占用连接,造成资源浪费,可以设置相应的连接超时参数,实现对连接访问时间的控制,可以修改配置文件nginx.conf,设置keepalive_timeout超时时间
  • 具体操作如下
keepalive_timeout 65 180;
//第一个参数指定了与客户端的keep-alive连接超时时间,服务器将会在这个时间后关闭连接
//第二个参数指定了在响应头Keep-Alive_timeout中的time值,这个头能够让一些浏览器主动关闭连接,这样服务器就不必关闭连接。如果没有这个参数,Nginx将不会发送Keep_Alive响应头
client_header_timeout 80;
//指定等待客户端发送请求头的超时时间
client_body_timeout 80;
//指定请求体读的超时时间
  • 设置连接超时的具体操作
vim /usr/local/nginx/conf/nginx.conf
...
    #keepalive_timeout  0;
    keepalive_timeout  65 180;
...

[root@192 conf]# service nginx stop
[root@192 conf]# service nginx start

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值