Web服务器网页优化(隐藏版本号,缓存时间,日志切割,网页压缩,防盗链优化)


前言

默认的Nginx安装参数只能提供最基本的服务,还需要调整如网页缓存时间、连接超时、网页压缩等相应参数,才能发挥出服务器的最大作用。

一:Nginx优化与防盗链实验

实验过程

1.1:方法一:配置Nginx隐藏版本号

在生产环境中需要隐藏Nginx的版本号,以避免泄露Nginx的版本,使×××者不能针对特定版本进行×××。查看Nginx的版本在CentOS中使用命令curl -I http://20.0.0.41即可。

  • 直接修改
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
  • 解压缩
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz 
  • 创建管理用户
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
  • 编译及安装
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
#编译
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
  • 路径优化,便于系统管理
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/bin/
  • 使用service控制
[root@localhost nginx-1.12.2]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/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
#增加权限
[root@localhost nginx-1.12.2]# chmod +x /etc/init.d/nginx 
#重启服务
[root@localhost nginx-1.12.2]# service nginx start
#查看端口
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22235/nginx: master 
  • 客户机访问

mark

  • 使用curl -l命令检测
[root@localhost nginx-1.12.2]# curl -I http://20.0.0.42
HTTP/1.1 200 OK
Server: nginx/1.12.2                        ##版本号
Date: Mon, 10 Aug 2020 07:15:28 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:07:32 GMT
Connection: keep-alive
ETag: "5f30f234-264"
Accept-Ranges: bytes
  • 修改配置文件
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@localhost conf]# vim nginx.conf

mark

  • 重启服务
lhost conf]# curl -I http://20.0.0.42
HTTP/1.1 200 OK
Server: nginx                ##已经隐藏
Date: Mon, 10 Aug 2020 07:18:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:07:32 GMT
Connection: keep-alive
ETag: "5f30f234-264"
Accept-Ranges: bytes

1.2:方法二:修改源码

  • Nginx源码文件/usr/src/ nginx-1.12.2/src/ core/nginx. h包含了版本信息,可以随意设置
  • 重新编译安装,隐藏版本信息
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
rh
[root@localhost opt]# rz -E
rz waiting to receive.
[root@localhost opt]# ls
nginx-1.12.2.tar.gz  rh
[root@localhost opt]# yum install gcc gcc-c++ pcre pcre-devel zlib-devel -y
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz 
[root@localhost nginx-1.12.2]# cd src/
[root@localhost src]# ls
core  event  http  mail  misc  os  stream
[root@localhost src]# cd core/
[root@localhost core]# vim nginx.h
[root@localhost core]# vim nginx.h

mark

  • 创建用户
[root@localhost core]# useradd -M -s /sbin/nologin nginx
  • 编译安装
[root@localhost core]# cd ../../
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
#编译
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
  • 创建软链接进行路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/bin/
  • 开启服务
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21887/nginx: master 
[root@localhost nginx-1.12.2]# curl -I http://20.0.0.42
HTTP/1.1 200 OK
Server: nginx/1.2.2                     ##已经隐藏修改成功
Date: Mon, 10 Aug 2020 07:28:53 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:26:43 GMT
Connection: keep-alive
ETag: "5f30f6b3-264"
Accept-Ranges: bytes

二:修改用户和组

  • Nginx运行时进程需要有用户与组的支持,用以实现对网站文件读取时进行访问控制。主进程由root创建,子进程由指定的用户与组创建。Nginx默认使用nobody用户账号与组账号,一般要修改。
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# id nobody
uid=99(nobody) gid=99(nobody)=99(nobody)
[root@localhost conf]# vim nginx.conf

#修改用户为nginx,组为nginx

mark

  • 重启nginx查看进程运行情况,主进程由root账户创建,子进程由nginx创建
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# ps aux | grep nginx
root      21957  0.0  0.0  20544   608 ?        Ss   15:48   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     21958  0.0  0.0  23072  1384 ?        S    15:48   0:00 nginx: worker process
root      21960  0.0  0.0 112724   988 pts/1    S+   15:48   0:00 grep --color=auto nginx

三:配置Nginx网页缓存时间

  • 当Nginx将网页数据返回给客户端后,可设置缓存时,以方便在日后进行相同的内容请求时直接放回,避免重复请求,加快了访问速度

  • 一般针对静态网页设置,对动态网页不设置缓存时间

设置方法

3.1:修改Nginx的配置文件,在location段加入expires参数

[root@localhost conf]# vim nginx.conf

mark

3.2:配置首页信息

  • 以图片作为缓存对象,将图片放在Nginx的网站目录下。
[root@localhost conf]# cd ..
[root@localhost nginx]# cd html/
#移动一张图片进来
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
50x.html                              index.html
df3499798e4f5110479f1b4d5c2f2026.jpg
#编辑首页
[root@localhost html]# vim index.html 

mark

3.3:重启服务

[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start

3.4:访问http://20.0.0.41,在用Fidder抓包工具

fiddler抓包查看,已经缓存成功

mark

四:实现Nginx的日志切割

  • 随着Nginx运行时间增加,日志也会增加。为了方便掌
    握Nginx运行状态,需要时刻关注Nginx日志文件

  • 太大的日志文件对监控是一一个大灾难
    定期进行日志文件的切割

  • Nginx自身不具备日志分割处理的功能,但可以通过

    Nginx信号控制功能的脚本实现日志的自动切割

  • 通过Linux的计划任务周期性地进行日志切割

4.1:日志分隔思路

  • 设置时间变量
  • 设置保存日志路径
  • 将目前的日志文件进行重命名
  • 重建新日志文件
  • 删除时间过长的日志文件
  • 设置cron任务,定期执行脚本自动进行日志分割

date -d:设置时间格式

[root@localhost html]# date
20200810日 星期一 17:33:31 CST
[root@localhost html]# date -d "-1 day" "+%Y%m%d"
20200809
[root@localhost html]# date -d "0 day" "+%Y%m%d"
20200810
[root@localhost html]# date -d "1 day" "+%Y%m%d"
20200811
[root@localhost html]# date -d "1 day"
20200811日 星期二 17:34:38 CST
  • 编写脚进行日志分割的思路
  • 把脚本/opt/shuai.sh,把Nginx的日志文件/usr/localnginx/logs/access.log移动到目录/var/log/nginx下面,以当前时间作为日志文件的名称,然后用kill-USR1创建新的日志文件/usr/local/nginx/logs/access.log,最后删除前30天的日志文件。
[root@localhost html]# cd /opt
[root@localhost opt]# ls
nginx-1.12.2  nginx-1.12.2.tar.gz  rh
[root@localhost opt]# vim shuai.sh
#编写以下内容

#!/bin/bash
#Filename:shuai.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}/tase.cpm-access.log-$d
#生成新日志
kill -USR1 $(cat $pid_path)
#删除30天前的日志
find $logs_path -mtime +30 | xargs rm -rf

#增加权限
[root@localhost opt]# chmod +x shuai.sh 
  • 运行脚本
  • 测试日志文件是否被切割
[root@localhost nginx]# cd /opt/
[root@localhost opt]# ./shuai.sh 
[root@localhost opt]# ls /var/log/nginx/
tase.cpm-access.log-20200809                   #已经生成日志文件了
  • 定时任务,定期执行脚本自动进行日志切割
[root@localhost opt]# crontab -e
#每天一点定时任务
0 1 * * * /opt/shuai.sh
crontab: installing new crontab

五:配置Nginx实现连接超时

  • 超时参数■为避免同-客户端长时间占用连接,造成资源浪费,可
    设置相应的连接超时参数,实现控制连接访问时间
    ■超时参数

    Keepalive_timeout
    设置连接保持超时时间
    Client_header_timeout
    指定等待客户端发送请求头的超时时间
    Client_body_timeout
    设置请求体读超时时间
    

    5.1:修改配置文件的work_processes参数

    [root@localhost opt]# cd /usr/local/nginx/conf/
    [root@localhost conf]# ls
    fastcgi.conf            nginx.conf
    fastcgi.conf.default    nginx.conf.default
    fastcgi_params          scgi_params
    fastcgi_params.default  scgi_params.default
    koi-utf                 uwsgi_params
    koi-win                 uwsgi_params.default
    mime.types              win-utf
    mime.types.default
    [root@localhost conf]# vim nginx.conf
    

    mark

第一个是客户端的超时时间,第二个是服务端的超时时间,参数放在http中以上就是进行超时时间的设置

六:nginx运行进程数

  • 在高并发场景,需要启动更多的 Nginx进程以保证快速响应,以处理用户的请求,避免造成阻塞

  • 可以使用 ps auxi命令查看Ngnx运行进程的个数

  • 更改进程数的配置方法

    修改配置文件,修改进程配置参数

  • 修改配置文件的 worker_ processes参数

    一般设为CPU的个数或者核数

    在高并发情况下可设置为CPU个数或者核数的2倍

  • 运行进程数多一些,响应访问请求时, Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务速度

  • 使用 ps aux查看运行进程数的变化情况

  • 默认情况, Nginx的多个进程可能跑在一个cPU上,可以分配不同的进程给不同的CPU处理,充分利用硬件多核多CPU

  • 在一台4核物理服务器,可进行以下配置,将进程进行分配

    Worker_cpu_affinity 0001 0010 0100 1000 ‘//核心数的序列位置’

    6.1:修改nginx的文件中的worker_process参数,一般设为CPU的个数或核数,在高并发的情况下,可设置为CPU的个数或者核数的2倍,可以先查看CPU的核数以确定参数

#查看cpu核心
[root@localhost conf]# cat /proc/cpuinfo | grep -c "physical"
8

参数设置为4,运行进程数设置多一些,响应客户端要求时,Nginx就不会临时启动新的进程进行提供服务,减少了系统的开销资源,提升了服务的速度。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

mark

  • 修改完成,重启服务,查看运行进程的变化
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
[root@localhost conf]# ps aux | grep nginx       #一个进程包含一个子进程
root      24803  0.0  0.0  20544   612 ?        Ss   18:59   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     24804  0.0  0.0  23072  1388 ?        S    18:59   0:00 nginx: worker process
nginx     24805  0.0  0.0  23072  1388 ?        S    18:59   0:00 nginx: worker process
nginx     24806  0.0  0.0  23072  1388 ?        S    18:59   0:00 nginx: worker process
nginx     24807  0.0  0.0  23072  1388 ?        S    18:59   0:00 nginx: worker process
root      24809  0.0  0.0 112724   988 pts/1    S+   18:59   0:00 grep --color=auto nginx

七:配置Nginx实现网页压缩功能

  • Nginx的ngx_htto_gzip_ module压缩模块提供对文件内容压缩的功能
  • 允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装
  • 可在配置文件中加入相应的压缩功能参数对压缩性能进行优化

优化网页压缩设置

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf
#搜索gzip on 第一行取消注释
    gzip  on;                       #开启gzip压缩输出
    gzip_min_length 1k;             #指定允许压缩的压面最小字节数
    gzip_buffers 4 16k;             #表示申请4个单位为64kb的内存作为压缩结果流缓存
    gzip_http_version 1.1;          #用于设置httpd协议版本,默认是1.1
    gzip_comp_level 6;              #指定fzip压缩比,压缩比越小,处理器越快
    gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;
  • 重启服务
[root@localhost conf]# service nginx stop
[root@localhost conf]# service nginx start
  • 宿主机测试点击刷新

mark

八:防盗链优化

防盗链概述

  • 在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失
  • Nginx防盗链功能也非常强大。默认情况下,只需要进行简单的配置,即可实现防盗链处理

使用三台主机模拟防盗链

IP地址 域名 用途 系统
20.0.0.43 www.abc.com 源主机 CentOS7
20.0.0.42 www.ab.com 盗链主机 CentOS7
20.0.0.41 -------------- 客户机 Windows7

服务端 20.0.0.43

[root@tom03 ~]# iptables -F
[root@tom03 ~]# setenforce 0
[root@tom03 ~]# yum install gcc gcc-c++  pcre pcre-devel zlib-devel -y
#创建用户
[root@tom03 ~]# useradd -M -s /sbin/nologin nginx
[root@tom03 ~]# cd /opt
[root@tom03 opt]# rz -E
rz waiting to receive.
[root@tom03 opt]# ls
awstats-7.6.tar.gz  nginx-1.12.2.tar.gz  rh
[root@tom03 opt]# tar zxvf nginx-1.12.2.tar.gz 
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with_http_stub_status_module
#编译
[root@localhost nginx-1.12.2]# make
[root@localhost nginx-1.12.2]# make install
  • 使用service控制
[root@tom03 nginx-1.12.2]# cd /etc/init.d/
[root@tom03 init.d]# vim nginx
#编写以下内容 
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/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
#增加权限
[root@tom03 init.d]# chmod +x nginx 
[root@tom03 init.d]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      86849/nginx: master 
#路径优化,便于系统管理
[root@tom03 init.d]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
#验证
[root@tom03 init.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#重启服务
[root@tom03 init.d]# service nginx start
#查看端口
[root@tom03 init.d]# netstat -ntap | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      86913/nginx: master 
  • 编辑首页文件
  • 把图片放在源主机abc.com的站点目录下。
[root@tom03 nginx]# cd html/
[root@tom03 html]# ls
50x.html  index.html
#随便找一张图片进来
[root@tom03 html]# rz -E
rz waiting to receive.
[root@tom03 html]# ls
138171822818.jpg  50x.html  index.html
[root@tom03 html]# vim index.html 

mark

  • 安装DNS服务
[root@tom03 html]# yum -y install bind
  • 配置区域文件
[root@tom03 html]# vim /etc/named.conf

mark

  • 配置区域
[root@tom03 html]# vim /etc/named.rfc1912.zones 

mark

  • 配置区域数据
[root@tom03 ~]# cd /var/named/
[root@tom03 named]# ls
abc.com.zone  dynamic   named.empty      named.loopback
data          named.ca  named.localhost  slaves
[root@tom03 named]# cp -p named.localhost abc.com.zone 
[root@tom03 named]# vim abc.com.zone

mark

  • 客户机访问测试
    mark

访问源主机网页正常

mark

九:配置Nginx盗链

  • 客户机IP 20.0.0.42

  • 安装apache

[root@localhost ~]# yum -y install httpd
  • 配置监听端口跟域名
#开启本地监听断开   ipv6端口注释
Listen 20.0.0.42:80
#Listen 80
#搜索Server
ServerName www.ab.com:80
  • 配置站点信息
  • 在盗链主机ab.com的站点目录下编写盗链页面abc.com盗取源主机abc.com的图片.
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
index.html
[root@localhost html]# vim index.html

<h1>this is ab web</h1>
<img src="http://www.abc.com/138171822818.jpg"/>
#域名指向
[root@localhost html]# echo "nameserver 20.0.0.43" > /etc/resolv.conf 
#重启服务
[root@localhost html]# systemctl start httpd

访问盗链网页http://20.0.0.42查看是否盗链成功

mark

十:配置Nginx防盗链步骤

1:Nginx的防盗原理是加入location项,用正则表达式过滤图片类型文件,对于信任的网址可以正常使用,对于不信任的网址则返回相应的错误图片。在源主机abc.com的配置文件加入以下代码:

[root@tom03 ~]# cd /usr/local/nginx/conf/
[root@tom03 conf]# vim nginx.conf
#添加以下内容

mark

  • ~*.(jpg|gif|swf)$: 匹配不区分大小写,以.jpg 或.gif或 .swf结尾的文件。

  • valid_referers:设置信任的网站,可以正常使用图片。

  • none:浏览器中refer为空的情况,就是直接在浏览器访问图片。

  • blocked:浏览器中refer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或 https://开头

  • 后面的网址或域名:refer包含相关字符串的网址。

  • if语句:如果链接的来源域名不在valid_referers所列出的列表中, $invalid_referer 为1,则执行后面的操作,即进行重写或返回403页面。

    2:编辑站点目录

[root@tom03 html]# pwd
/usr/local/nginx/html
[root@tom03 nginx]# cd html/
[root@tom03 html]# ls
138171822818.jpg  50x.html  index.html
#复制一张图片进来
[root@tom03 html]# rz -E
rz waiting to receive.
[root@tom03 html]# ls
138171822818.jpg  1389753641370.jpg  50x.html  index.html

3:配置防盗图片指向

[root@tom03 html]# vim /usr/local/nginx/conf/nginx.conf

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oJct6LMK-1597635531253)(C:\Users\liu\AppData\Roaming\Typora\typora-user-images\image-20200811103138317.png)]4:重启服务

[root@tom03 html]# service nginx stop
[root@tom03 html]# service nginx start

5:宿主机再次访问,访问前把浏览器缓存清空一下,显示的是被重写的图片,说明防盗链成功

mark

需要注意的是如果在配置文件中加入了新的location项去配置网页缓存时间,那么防盗链加入的location项应置于其之前,才能防盗链成功。

猜你喜欢

转载自blog.csdn.net/weixin_47151643/article/details/108052707