nginx页面优化与防盗链

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、nginx页面优化

1.版本号

对版本号进行更改的目的:可以根据nginx的版本号进行破译,因此都会隐藏后端服务应用的真实版本号

1.1 查看版本号

#使用本地服务地址查询
curl -I 192.168.245.110
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sun, 25 Jun 2023 01:46:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 21 Jun 2023 14:23:30 GMT
Connection: keep-alive
ETag: "649307e2-264"
Accept-Ranges: bytes
#使用nginx命令查询
nginx -v
nginx version: nginx/1.12.2

1.2 修改版本号

1.2.1 修改配置文件
#切换到nginx的配置文件目录下
cd /usr/local/nginx/
cd conf/
#复制配置文件做备份
cp nginx.conf nginx.conf.bak.2023.6.25
vim nginx.conf
#关闭版本号
在http中添加server_tokens off;

#重启服务
systemctl restart nginx.service

1.2.2 修改源码文件,重新编译安装
#切换到配置文件的目录
cd /opt/nginx-1.12.2/src/core/
#进入配置文件,将配置文件的真是版本号进行修改
vim nginx.h
#define NGINX_VERSION      "whd"

#重新编译安装
cd /opt/nginx-1.12.2/
#编译安装
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
#修改配置文件
cd /usr/local/nginx/conf/
vim nginx.conf
#在源码包中重新定义了版本号,则打开版本号并无影响
在http中将off需改为on打开server_tokens on;

 systemctl restart nginx.service

2.nginx的日志分割

为什么做日志分割:nginx不带日志分割的工具,因此所有的日志分割都是以shell脚本的格式来实现日志的分割

 vim nginxlog.sh

#!/bin/bash
#获取当前日期 
d=$(date +%Y-%M-%d)
#定义存储目录 
dir="/usr/local/nginx/logs/"
#定义需要分割的日志源文件 
logs_file='/usr/local/nginx/logs/access.log'
logs_error='/usr/local/nginx/logs/error.log'
#定义nginx服务的pid文件 
pid_file='/usr/local/nginx/logs/nginx.pid'
if [ ! -d $dir ]
  then
  mkdir $dir
fi
#移动日志文件access error,重新命名
mv ${logs_file} ${dir}/access${d}.log
mv ${logs_error} ${dir}/error${d}.log
#发送信号,给nginx主程序,让nginx生成新的日志文件 
kill -USR1 $(cat ${pid_file})
#日志文件清理,把30天前的日志,直接清除 
find $dir -mtime +30 -exec rm -rf {} \;

给脚本执行权限、执行

chmod 777 nginxlog.sh 
./nginxlog.sh 
cd /usr/local/nginx/logs/
ls
access2023-14-25.log  access.log           error.log
access_.log           error2023-14-25.log  nginx.pid

在这里插入图片描述
创建定时任务可以每个月固定分割一次

crontab -e -u root
0 0 1 * * /opt/nginxlog.sh

3.nginx的页面压缩

压缩的目的:为了节约宽带,提高访问速度

由ngx_http_gzip_module压缩模块所提供的功能,默认是注释掉的,不压缩,需要人工指定配置

3.1 配置

cd /usr/local/nginx/conf/
vim nginx.conf
#找到gzip  on;取消注释并在下面添加
    gzip  on;
    #最小的压缩文件,小与1K就不再压缩了
    gzip_min_length 1k;
    #压缩的缓冲区,4个64K缓冲区
    gzip_buffers 4 64K;
    #压缩版本,默认1.1
    gzip_http_version 1.1;
    #压缩级别1-9,6不大不小,不快不慢,正好
    gzip_comp_level 6;
    #支持前段缓存服务器的压缩功能打开
    gzip_vary on;
    #支持压缩的类型
   gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xmltrss image/jpgmage/ipeg image/png image/gif application/x-httpd-php application/iavascriot application/json;

#检查配置文件、重启服务
nginx -t
systemctl restart nginx

3.2 验证

在这里插入图片描述

4.图片缓存

作用:避免重复访问,导致访问速度变慢,加快访问的时间,主要针对静态页面,动态不设置缓存时间

4.1 配置

vim nginx.conf
#在location ~ \.php$下面添加一个新的,location ~*支持正则匹配,\.转义符,以.gif|jpg|jepg|bmp|ico开头的都文件可以压缩
location ~* \.(gif|jpg|jepg|bmp|ico)$ {
#指定根目录的html,就是/usr/local/nginx/html,指定默认静态页面
             root html;
#设定图片缓存时间为1天
             expires 1d;
}


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
systemctl restart nginx

4.2 浏览器页面访问验证

清理缓存,查看304就是缓存
在这里插入图片描述
再次清理缓存就是200,没有缓存
在这里插入图片描述
再次刷新又会变成304即是加载的缓存

图片缓存一天:

在这里插入图片描述

5.连接超时

http自带keepalive模式,keepalive为缓存记录时间,(web加大用户访问速度)

web服务器外理完一个请求之后,保持tcp许接,接受到同样的客户端的其他请求时,web服务器就会利用这个未关闭的连接,继续提供相应,不需要再新建连接了keepalive:在一段时间之内保持打开状态,他会占用资源,占用过多资源,影响整体性能。

连接保持,设置取恰当的范围,不可太长(占用资源),不可太短(第二次访问不快

5.1 配置

vim nginx.conf
#keepalive_timeout  65;的意思是指定的tcp连接最多只能保持65秒,在65秒之后,服务器就会关闭连接(nginx默认65秒。一般浏览器都是60秒,可以指定时间,一般60s,如果设置0,nginx就不会在发送包含keepalive的相应头)
#将65修改为60,60s之后关闭连接
keepalive_timeout  60;
#客户端向服务端发送一个完整的请求头的超时时间
client_header_timeout 60;
#客户端向服务端发送一个完整的请求体的超时时间,没有在60s内向web服务器请求发送任何内容,nginx会返回错误代码408——请求超时
client_body_timeout 60;

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
systemctl restart nginx

6.nginx的并发设置

目的:有很多场景会涉及到高并发,高并发场景是需要进行优化的,需要启动更多的nginx进程,以保证快速响应,处理用户请求,避免阻塞,根据核心数计算:有几个cpu(查看cpu数可以使用命令:cat /proc/cpuinfo | grep processor | wc -l)

nginx工作的核心数和服务器内核之间要么一致,要么是核心数的2倍,如有16个内核,最多给8个,超过8个性能不在提升且会降低稳定性,8个是上限

6.1 配置

vim nginx.conf
#指定内核数,本服务器内核是4个,可给4个
worker_processes  4;
#指定工作的内核编号指定第一个和第二个,要么用第一个,要么用第二个(此处cpu是按二进制排列,第一个表示为0001,第二个为0010,第三个为0100,第四个为0000)
worker_cpu_affinity 01 10 100 0;


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
systemctl restart nginx

二、防盗链

目的:防止别人盗用网站的图片,一旦使用会回复正在盗用图片,不可以用

1.配置

vim /usr/local/nginx/conf/nginx.conf
location ~ .*\.(gif|jpg|swf|png)$ {
#允许访问不加http直接地址,不加协议请求访问资源,此处*.whd.com whd.com设置信任的网站可以正常使用图片文件
                   valid_referers none blocked www.baidu.com;
#如不是受信任网站                
                   if ( $invalid_referer ) {
#直接返回403
                  return 403;
                }               
}

nginx -t
systemctl restart nginx

cd /usr/local/nginx/html

使用一张6.jpg图片传到当前目录下用于验证
在这里插入图片描述

2.验证

虽然在html中的文件进行了防盗配置,但是还是访问的时候会出来图片,并不是403报错,这是因为:这种形式的访问是直接请求服务器中默认html中的6.jpg文件,是没有头部的(只有二次以上请求才会有referer头部),并且我们在配置文件中添加了[none]这个参数,它的含义就是当我们在没有头部referer时,依然能访问到文件,所以我们配置的防盗链在这个时候是不起作用的。

将none参数去掉验证效果
在这里插入图片描述

systemctl restart nginx

再次访问就会有返回错误
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值