nginx优化与防盗链

文章目录

  • nginx优化与防盗链
    • 一、隐藏nginx版本号
      • 1.1修改源码法
        • 1.1.1安装nginx环境依赖包
        • 1.1.2 修改版本信息
        • 1.1.3编译安装
        • 1.1.4优化路径
        • 1.1.5检查配置文件格式 /usr/loc al/nginx/conf/nginx.conf
        • 1.1.6 编译安装启动脚本
        • 1.1.7 验证编译脚本格式
        • 1.1.8添加启动脚本
        • 1.1.9查看版本号
      • 1.2配置nginx主配置文件修改
    • 二、修改用户的属主属组
      • 2.1修改主配置文件
      • 2.2在编译前配置
    • 三、优化nginx缓存时间
      • 3.1 修改nginx主配置文件/usr/local/nginx/conf/nginx.conf
      • 3.2 测试
        • 3.2.1在nginx站点目录上传一张timg.jpg图片
        • 3.2.2在nginx站点目录指定图片路径/usr/local/nginx/html
        • 3.2.3在win10测试机利用fiddler抓包工具测试缓存功能
    • 四、nginx日志分割
      • 4.1日志分割思路
      • 4.2编写脚本进行日志分割
    • 五、配置nginx连接超时
    • 六、nginx深入优化
      • 6.1更改nginx运行进程数
      • 6.2网页压缩
    • 七、nginx防盗链
    • 七、nginx防盗链

nginx优化与防盗链

一、隐藏nginx版本号

1.1修改源码法

1.1.1安装nginx环境依赖包

1 )安装环境包
yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel

上传nginx压缩包到/opt目录下
2 ) 解压nginx压缩包
tar -zxvf nginx-1.12.2.tar.gz
3 )创建运行用户
useradd -M -s /sbin/nologin nginx

1.1.2 修改版本信息

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

#define nginx_version      1012002
#define NGINX_VERSION      "1.12.2"	##1.12.2是版本号,直接修改为1.1.1
#define NGINX_VER          "nginx/" NGINX_VERSION

1.1.3编译安装

cd /opt/nginx-1.12.2
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
  make && make install

1.1.4优化路径

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin

1.1.5检查配置文件格式 /usr/loc al/nginx/conf/nginx.conf

[root@localhost sbin]# 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@localhost sbin]# nginx
[root@localhost sbin]# netstat -napt |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      70403/nginx: master 

1.1.6 编译安装启动脚本

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
      $o start
        ;;
    reload)
      kill -s HUP $(cat $PIDF)
        ;;
    *)
            echo "Usage: $0 {start|stop|restart|lsreload}"
            exit 1
esac
exit 0

1.1.7 验证编译脚本格式

nginx -t

1.1.8添加启动脚本

chkconfig --add nginx
chmod +x nginx 增加执行权限

1.1.9查看版本号

[root@localhost html]# curl -I http://14.0.0.8
HTTP/1.1 200 OK
Server: Nginx/1.1.1
Date: Mon, 10 Aug 2020 10:15:48 GMT
Content-Type: text/html
Content-Length: 634
Last-Modified: Mon, 10 Aug 2020 07:50:36 GMT
Connection: keep-alive
ETag: "5f30fc4c-27a"
Accept-Ranges: bytes

1.2配置nginx主配置文件修改

vim /usr/local/nginx/conf/nginx.conf 

http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;	##添加
}

二、修改用户的属主属组

2.1修改主配置文件

vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;	'//添加'

service nginx restart
ps aux |grep nginx 
root	130034 0.0 0.0 20220 620 ? Ss 19:41 0:00  nginx:master process
/usr/local/sbin/nginx 
nginx	130035 0.0 0.0 20664 1512? S 19:41 0:00  nginx:worker process

2.2在编译前配置

[root@localhost nginx-1.12.2]#./configure /
--prxfix=/usr/local/nginx \
--user=nginx \
--group=nginx \

三、优化nginx缓存时间

3.1 修改nginx主配置文件/usr/local/nginx/conf/nginx.conf

vim /usr/local/nginx/conf/nginx.conf

location ~\.(gif|jpg|jepg|png|bmp|ico)$ {
    root html;
    expires 1d;
} 	
'//1d,一天'

nginx优化与防盗链_第1张图片

3.2 测试

3.2.1在nginx站点目录上传一张timg.jpg图片

3.2.2在nginx站点目录指定图片路径/usr/local/nginx/html

vim /usr/local/nginx/html/index.html

nginx优化与防盗链_第2张图片

3.2.3在win10测试机利用fiddler抓包工具测试缓存功能

访问14.0.0.8

nginx优化与防盗链_第3张图片

nginx优化与防盗链_第4张图片

四、nginx日志分割

4.1日志分割思路

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

4.2编写脚本进行日志分割

[root @www logs]# vim /opt/fenge.sh

#!/bin/bash
#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)
find $logs_path -mtime +30 | xargs rm -rf

chmod +x /opt/fenge.sh 增加执行权限

执行

[root@localhost opt]# ./fenge.sh
[root@localhost opt]# netstat -napt|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30192/nginx: master 
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200809

crontab -e ‘//设置周期性任务’

nginx优化与防盗链_第5张图片

0 1 * * * /opt/fenge.sh

通过更改时间验证

[root@localhost opt]# date -s 08/12/20
2020年 08月 12日 星期三 00:00:00 CST
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200811
[root@localhost opt]# date -s 08/15/20
2020年 08月 15日 星期六 00:00:00 CST
[root@localhost opt]# ./fenge.sh
[root@localhost opt]# ls /var/log/nginx/
test.com-access.log-20200811  test.com-access.log-20200814
[root@localhost opt]# 

----date -d +1(second minute hour day month year)–
--------kill -QUIT 5410 结束进程 -HUP 平滑重启 类似 reload -USRl 日志分隔 -USR2平滑升级-----

date -d “-1 day” “+%Y%m%d” ‘//##时间向前推进一天’

date -s 2019-12-25 ‘//##时间向后推移一天’

五、配置nginx连接超时

Keepalive_timeout设置

vim /usr/local/nginx/conf/nginx.conf

nginx优化与防盗链_第6张图片

六、nginx深入优化

6.1更改nginx运行进程数

[root@localhost opt]# cat /proc/cpuinfo|grep -c “physical”
8

[root@localhost opt]# ^C
[root@localhost opt]# ps aux |grep nginx
root      31441  0.0  0.0  20540   804 ?        Ss   8月14   0:00 nginx: m
nginx     31442  0.0  0.0  23068  1640 ?        S    8月14   0:00 nginx: w
root

有1个进程

nginx优化与防盗链_第7张图片

[root@localhost opt]# ps aux |grep nginx
root      32106  0.0  0.0  20540   612 ?        Ss   00:35   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     32107  0.0  0.0  23068  1388 ?        S    00:35   0:00 nginx: worker process
nginx     32108  0.0  0.0  23068  1388 ?        S    00:35   0:00 nginx: worker process
root      32112  0.0  0.0 112724   984 pts/0    S+   00:36   0:00 grep --color=auto nginx

有两个进程了

6.2网页压缩

  • gzip on:开启gzip压缩输出g
  • zip_min_length 1k:用于设置允许压缩的页面最小字节数
  • gzip_buffers 4 16k:表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果(buffers:缓存区)
  • zip_http_version1.0:用于设置识别htt协议版本,默认是1.1,目前大部分浏览器已经支持gzip解压,但处理最慢,也比较消耗服务器CPU资源
  • gzip_comp_level2:用来指定gzp缩比,1压缩比最小,处理速度最快;9压缩比最大,传输速度快,但处理速度最慢,使用默认即可
  • gzip_types text/plain:压缩类型,是就对哪些网页文档启用压缩功能
  • gzip_vary on:选项可以让前端的缓存服务器缓存经过gzi压缩的页面
  • 将以上的压缩功能参数加入到主配置文件httpd配置中
  • 重启服务,并用 Fiddler工具查看开启结果

优化

网页压缩配置

vim /usr/local/nginx/conf/nginx.conf

gzip  on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
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;

nginx优化与防盗链_第8张图片

重启服务

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

win10客户机访问14.0.0.8

nginx优化与防盗链_第9张图片

用于压缩功能了

七、nginx防盗链

编辑配置文件(主机1)

vim /usr/local/nginx/conf/nginx.conf

location ~*.(jpg|gif|swf)$ {
 valid_referers none blocked *.kgc.com kgc.com;
 if ( $invalid_referer ) {
 rewrite ^/ http://www.kgc.com/error.png;
 }
 }

nginx优化与防盗链_第10张图片

重启服务

systemctl restart nginx

dl2oFtFn-1597499441057)]

用于压缩功能了

七、nginx防盗链

编辑配置文件(主机1)

vim /usr/local/nginx/conf/nginx.conf

location ~*.(jpg|gif|swf)$ {
 valid_referers none blocked *.kgc.com kgc.com;
 if ( $invalid_referer ) {
 rewrite ^/ http://www.kgc.com/error.png;
 }
 }

[外链图片转存中…(img-GqvGYfr9-1597499441058)]

重启服务

systemctl restart nginx

nginx优化与防盗链_第11张图片

你可能感兴趣的:(nginx优化与防盗链)