缓存是指在指定服务器不更新文简单情况下,将某些文件缓存到客户端本地,并且配置一定的期限。用户在第二次访问这个文件的时候,请求的不再是服务器,而是直接调用客户端本地缓存内容
安装好nginx后进入主配置文件
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak //备份配置文件
vim nginx.conf
server {
listen 80;
server_name 192.168.159.140;
location / {
root html;
index index.html index.htm;
location ~* \.(jpg|png|jpeg|bmp|gif|swf|ico|txt|css|js)$ {
root /usr/local/nginx/html;
expires 30d; //缓存期限
}
}
}
然后在 /usr/local/nginx/html 中放入图片测试
在浏览器中测试:http://192.168.159.140/微信图片_20220314100103.jpg
用curl命令测试显示结果
[root@localhost html]# curl -I http://192.168.159.140/微信图片_20220314100103.jpg
HTTP/1.1 200 OK
Server: nginx/1.20.2
Date: Mon, 30 May 2022 01:59:46 GMT
Content-Type: image/jpeg
Content-Length: 171299
Last-Modified: Mon, 30 May 2022 01:39:30 GMT
Connection: keep-alive
ETag: "62942052-29d23"
Expires: Wed, 29 Jun 2022 01:59:46 GMT
Cache-Control: max-age=2592000
Accept-Ranges: bytes