配置Nginx網頁緩存時間
當Nginx將網頁數據返回給客戶端後,可設置緩存的時間,以方便在日後進行相同內容的請求時直接返回,避免重複請求,加快了訪問速度。一般針對靜態網頁設置,對動態網頁不設置緩存時間。可在Windows客戶端中使用fiddler查看網頁緩存時間。
設置方法
可修改配置文件,在http段、或者server段、 或者location段加入對特定內容的過期參數
1.將圖片複製到站點目錄
[root@localhost nginx-1.12.2]# cd /mnt/tools/
[root@localhost tools]# ls
awstats-7.6.tar.gz fiddler.exe jdk-8u191-windows-x64.zip LNMP zhuang.mp4
cronolog-1.6.2-14.el7.x86_64.rpm forbid.png john-1.8.0.tar.gz picture.jpg
extundelete-0.2.4.tar.bz2 intellijideahahau2018.rar LAMP-C7 yuan.mp4
[root@localhost tools]# cp picture.jpg /usr/local/nginx/html/ //複製圖片
[root@localhost tools]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html picture.jpg
[root@localhost html]#
2.在首頁文件添加引用圖片
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
<img src="picture.jpg"/> //添加
[root@localhost html]# systemctl stop firewalld.service //關閉防火牆
[root@localhost html]# setenforce 0 //關閉增強性安全功能
[root@localhost html]#
3.用win10虛擬機訪問nginx服務,並抓包(未開啓緩存)
4.在nginx配置文件中添加緩存模塊
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
location ~ \.(gif|jepg|jpg|ico|bmp|png)$ { //添加緩存
root html; //站點
expires 1d; //緩存一天
}
[root@localhost html]# service nginx restart //重啓服務
[root@localhost html]#