1. 程式人生 > 其它 >nginx cache新增快取和刪除快取

nginx cache新增快取和刪除快取

技術標籤:大運維大前端nginxlinux

1、前言

說起快取,大家也許知道CDN。購買CDN服務後,服務提供商會把你的資源都快取到各個加速節點上,當用戶訪問時,會自動獲取距離使用者最近節點的快取資源,直接由最近的節點快速響應,有效解決網站訪問慢,延遲高等問題。

nginx快取和CDN不同,nginx是快取到當前機器上,減少同一資源的重複請求和伺服器併發請求的壓力,讓網站響應訪問更快。

nginx主要採用proxy_cache_path指令,去定義共享的快取

2、配置快取

nginx.conf

proxy_cache_path /data/nginx/mycache levels=1:2 keys_zone=mycache:100m max_size=1024m inactive=7d
# 快取存放的位置是 /data/nginx/mycache,目錄層級為兩層,最多存放 1024M 快取
# keys_zone=mycache:100m 快取key存放的記憶體空間是 mycache ,單個快取最大為100m
# inactive=7d 自動移除7d內沒有人訪問的快取

server配置

server {
  listen 80;
  server_name localhost;

  location / {
    proxy_cache mycache;
    proxy_cache_valid 200 206 304 301 302 7d;
    proxy_cache_key $uri;
    proxy_set_header Host $host:$server_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # 快取狀態標記
    add_header Nginx-Cache "$upstream_cache_status";
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_pass http://192.168.6.19:8090;
  }
}
# $upstream_cache_status包含以下幾種狀態:
#	MISS 未命中,請求被傳送到後端
#	HIT 快取命中
#	EXPIRED 快取已經過期請求被傳送到後端
#	UPDATING 正在更新快取,將使用舊的應答
#	STALE 後端將得到過期的應答

檢視快取檔案和響應頭

[[email protected] /]# ll /data/nginx/mycache/
總用量 0
drwx------ 10 nobody nobody  86 1月  19 14:29 0
drwx------ 10 nobody nobody  86 1月  19 14:29 1
drwx------  8 nobody nobody  66 1月  19 14:29 2
drwx------  6 nobody nobody  46 1月  19 14:29 3
drwx------  9 nobody nobody  76 1月  19 14:29 4
drwx------  4 nobody nobody  26 1月  19 14:27 5
drwx------ 13 nobody nobody 116 1月  19 14:29 6
drwx------  5 nobody nobody  36 1月  19 14:29 7
drwx------  7 nobody nobody  56 1月  19 14:29 8
drwx------ 17 nobody nobody 156 1月  19 14:29 9
drwx------  6 nobody nobody  46 1月  19 14:29 a
drwx------ 11 nobody nobody  96 1月  19 14:29 b
drwx------  8 nobody nobody  66 1月  19 14:29 c
drwx------  7 nobody nobody  56 1月  19 14:29 d
drwx------  9 nobody nobody  76 1月  19 14:29 e
drwx------  6 nobody nobody  46 1月  19 14:29 f
Response Header:
HTTP/1.1 200 OK
Server: nginx/1.11.2
Date: Tue, 19 Jan 2021 06:29:39 GMT
Content-Type: image/jpeg
Content-Length: 344991
Connection: keep-alive
Last-Modified: Tue, 29 Dec 2020 06:45:35 GMT
ETag: "5fead08f-5439f"
# 響應頭立馬標明Hit是走了快取
Nginx-Cache: HIT
Accept-Ranges: bytes

分析下快取的檔案

[[email protected] conf.d]# cd /data/nginx/mycache/
[[email protected] mycache]# ll
總用量 0
drwx------  5 nobody nobody  36 1月  19 17:15 0
drwx------  7 nobody nobody  56 1月  19 17:15 1
drwx------  9 nobody nobody  76 1月  19 17:15 2
drwx------  5 nobody nobody  36 1月  19 17:15 3
drwx------  4 nobody nobody  26 1月  19 17:15 4
drwx------  3 nobody nobody  16 1月  19 17:15 5
drwx------ 10 nobody nobody  86 1月  19 17:15 6
drwx------  4 nobody nobody  26 1月  19 17:15 7
drwx------  8 nobody nobody  66 1月  19 17:15 8
drwx------ 12 nobody nobody 106 1月  19 17:15 9
drwx------  7 nobody nobody  56 1月  19 17:15 a
drwx------  8 nobody nobody  66 1月  19 16:56 b
drwx------  7 nobody nobody  56 1月  19 16:56 c
drwx------  6 nobody nobody  46 1月  19 17:15 d
drwx------  3 nobody nobody  16 1月  19 16:56 e
drwx------  7 nobody nobody  56 1月  19 17:15 f
# 快取檔案的樹形結構
[[email protected] mycache]# tree
.
├── 0
│   ├── 6c
│   │   └── 9ae9c2f571e0686404c33c7daeaa06c0
│   ├── 8a
│   │   └── 89fd0882a629e6219440dc2c2a1728a0
│   └── cb
│       └── 01f416e47e21f704999f91395b203cb0
├── 1
│   ├── 33
│   │   └── b2b723a43ffbcb52895de63af10c3331
│   ├── 48
│   │   └── 4c98030d1e7cde9952f12149800f5481
│   ├── 50
│   │   └── 63423e0931541247e600ce4b19498501
│   ├── 54
│   │   └── 0660d7e3fa893d9cada7645f347a8541
│   └── e4
│       └── fe321d64b34ada0fe8dc0653199d5e41
├── 2
│   ├── 12
│   │   └── 7ed64f947ec3d1372321f03ca626e122
│   ├── 27
│   │   └── 0c3b3a8aae4b25cadef128f0f1066272
│   ├── 5f
│   │   └── 7214874cd1b5f713fc36f58d356b85f2
│   ├── 9b
│   │   └── d4cf56855cb03dcd5c3c72d6cd63a9b2
│   ├── ae
│   │   └── 11cb7df570b1b301421d3bd8371d0ae2
│   ├── b2
│   │   └── cd92f4555f2c1ebd3c91281e58ef1b22
│   └── ce
│       └── a9b9d14cb18c3bdfffb43add07274ce2
├── 3
│   ├── 14
│   │   └── 12defce88137f1e7f8a45039a1475143
│   ├── 5e
│   │   └── a67e128c8c6248d2096679608ded85e3
│   └── 86
│       └── 485bbc7486cf36e34d3b366ff6cdc863
├── 4
│   ├── 9b
│   │   └── 3840ff95cc47e9f1f57335a8cfc549b4
│   └── a4
│       └── 1df942d653c6faf959703a733e764a44
├── 5
│   └── 7e
│       └── a514fa7e1ba5f3ee1d18b3119f8ed7e5
├── 6
│   ├── 01
│   │   └── f82f942af8321678a87dd9d3574b2016
│   ├── 02
│   │   └── 91d7283dc35d682bb48c550f0ba04026
│   ├── 13
│   │   └── eae7ee8ee076f44fa2e89e4be81d6136
│   ├── 91
│   │   └── 05ffa9f02ec224a1903ac67cbe18c916
│   ├── af
│   │   └── 8d36734e0889c6824a734695f1bc0af6
│   ├── d0
│   │   └── 1ed1eaa4c88b4b159f57111bd7ab5d06
│   ├── d8
│   │   └── 2663c0aba362e45d6fe8ff208b4e3d86
│   └── e6
│       └── c411c8b600995d375bd6847b39530e66
├── 7
│   ├── 7d
│   │   └── f1b7e28273b0cfa2e89017d19958f7d7
│   └── cb
│       └── 65746eca271882028ab6c3dd6515ccb7
├── 8
│   ├── 15
│   │   └── 781c040f32284af16eccb94348696158
│   ├── 26
│   │   └── c8a8b5be72449e52d2b9e967e5f77268
│   ├── 95
│   │   └── 49ea98efeb7ff338d0c7106448f12958
│   ├── 96
│   │   └── 413d6f4fbff770df85644c86acf7d968
│   ├── 9f
│   │   └── 7a1bef87f7ed095e5b1475a06dc909f8
│   └── fa
│       └── d53e83d6753a983786ac0d1990852fa8
├── 9
│   ├── 65
│   │   └── de54cb450c5df4076167ee4f72e5d659
│   ├── 7d
│   │   └── 6666cd76f96956469e7be39d750cc7d9
│   ├── 87
│   │   └── 86687aa6db0f9cef67c78ca0c9e05879
│   ├── 9a
│   │   └── b7a6417d24ed983a3cc8f0b99fcc19a9
│   ├── 9d
│   │   └── 35a63c8a85b1279a0f991ce8828fb9d9
│   ├── b9
│   │   └── ee6de8f5f99600772d32573d6ec18b99
│   ├── c2
│   │   └── fb7004eceb8226d8f96fba8db5d8bc29
│   ├── c7
│   │   └── f3c633c9af460f0eec5a15ab9729ec79
│   ├── ea
│   │   └── 767cb65c0b2c73b52728f1b477211ea9
│   └── fe
│       └── 495b4912b20cab0427e403c097e7ffe9
├── a
│   ├── 13
│   │   └── 17e274514ff3617ed7d5d79ab415813a
│   ├── 53
│   │   └── 123894b06cea028a5d9811215f3c053a
│   ├── c3
│   │   └── 93462aab0d354f3cb97ede5ed2d6fc3a
│   ├── e7
│   │   └── c4a16c5a6fd27939cb73d5320f197e7a
│   └── ed
│       └── 4e3fd92bad2ccb81bc2057b98605ceda
├── b
│   ├── 31
│   │   └── 5386768afecd87dda979f6d334ab331b
│   ├── 50
│   │   └── 10231799803326c557073800fe3bf50b
│   ├── 8c
│   │   └── 2b0b48516c4f8c930743f47ac69868cb
│   ├── d0
│   │   └── 3f8e127f3c7dd449187f14e803cc0d0b
│   ├── d3
│   │   └── 9a6d85fac597d4d094020be0887c3d3b
│   └── e2
│       └── c63d6dc1b9635f9b3c3f4b7e99060e2b
├── c
│   ├── 09
│   │   └── 98d57d14791e9ff9cc78e6128cbc109c
│   ├── 1a
│   │   └── 0f9dc5b1ea56e9d53421a2c46ba791ac
│   ├── 33
│   │   └── d41e7f6e551d5a90ecfab6c5c3e9433c
│   ├── 89
│   │   ├── 0023164edcc675a4d3ecedcce4f6f89c
│   │   └── 9762206a91f1d81c6de3999d0d25589c
│   └── ab
│       └── faffa09c8565fdf96281df3af9fadabc
├── d
│   ├── 23
│   │   └── 7dc20f318404a98515268c775820623d
│   ├── 6d
│   │   └── ae568cd89a7087c128b3bae75af056dd
│   ├── 7d
│   │   └── 8b53ac35e1688385a2e1bd062d1e77dd
│   └── 87
│       └── 62ad2439a0f9c910c58f2cbf0c9c987d
├── e
│   └── 8f
│       └── 26a75d07473d2c7deac4dbb6f7d8f8fe
└── f
    ├── 10
    │   └── 6ac830707f4eb196462d1114cfed310f
    ├── 29
    │   └── af169d9bab3ab6fe0e42e7d5a94af29f
    ├── 86
    │   └── 78ca094dc16c5d64c10dfe624169386f
    ├── 91
    │   └── e00ba68ecb1f25afb5c0042b10c6391f
    └── f3
        └── 966a2f57505d2f9a91be6c48abdecf3f

89 directories, 74 files
# 上面的快取檔案ID都是根據快取的cache_key自動生成的,我們不能直接看出來是對應那個檔案的快取
# 通過cat命令檢視快取的檔案詳情
[[email protected] mycache]# cat /data/nginx/mycache/0/8a/89fd0882a629e6219440dc2c2a1728a0
:ؠܮ