You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
8.7 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# Nginx 的 web 缓存服务
- Nginx是从0.7.48版开始提供缓存功能。
- Nginx是基于Proxy Store来实现的其原理是把URL及相关组合当做Key,在使用MD5算法对Key进行哈希得到硬盘上对应的哈希目录路径从而将缓存内容保存在该目录中。
- 它可以支持任意URL连接同时也支持404/301/302这样的非200状态码。Nginx即可以支持对指定URL或者状态码设置过期时间也可以使用purge命令来手动清除指定URL的缓存。
## 1. Nginx缓存设置的相关指令
- Nginx的web缓存服务主要是使用`ngx_http_proxy_module`模块相关指令集来完成
### 1.1 proxy_cache_path
- 该指定用于设置缓存文件的存放路径
| 语法 | proxy_cache_path path [levels=number] <br/>keys_zone=zone_name:zone_size [inactive=time]\[max_size=size]; |
|-----|-----------------------------------------------------------------------------------------------------------|
| 默认值 | — |
| 位置 | http |
- path:缓存路径地址,如: /usr/local/proxy_cache
- levels: 指定该缓存空间对应的目录最多可以设置3层每层取值为1|2如 :
```txt
levels=1:2 缓存空间有两层目录第一次是1个字母第二次是2个字母
举例说明:
itheima[key] 通过MD5加密以后的值为 43c8233266edce38c2c9af0694e2107d
levels=1:2 最终的存储路径为/usr/local/proxy_cache/d/07
levels=2:1:2 最终的存储路径为/usr/local/proxy_cache/7d/0/21
levels=2:2:2 最终的存储路径为??/usr/local/proxy_cache/7d/10/e2
```
- keys_zone:用来为这个缓存区设置名称和指定大小,如:
- keys_zone=host:200m 缓存区的名称是host,大小为200M,1M大概能存储8000个keys
- inactive:指定缓存的数据多次时间未被访问就将被删除,如:
- inactive=1d 缓存数据在1天内没有被访问就会删除
- max_size:设置最大缓存空间,如果缓存空间存满,默认会覆盖缓存时间最长的资源,如:
- max_size=20g
- 配置实例:
```editorconfig
http{
proxy_cache_path /usr/local/proxy_cache keys_zone=itcast:200m levels=1:2:1 inactive=1d max_size=20g;
}
```
### 1.2 proxy_cache
- 该指令用来开启或关闭代理缓存,如果是开启则自定使用哪个缓存区来进行缓存。
| 语法 | proxy_cache zone_name\off; |
|-----|----------------------------|
| 默认值 | proxy_cache off; |
| 位置 | http、server、location |
- zone_name指定使用缓存区的名称
### 1.3 proxy_cache_key
- 该指令用来设置web缓存的key值Nginx会根据key值MD5哈希存缓存。
| 语法 | proxy_cache_key key; |
|-----|---------------------------------------------------|
| 默认值 | proxy_cache_key \$scheme\$proxy_host$request_uri; |
| 位置 | http、server、location |
### 1.4 proxy_cache_valid
- 该指令用来对不同返回状态码的URL设置不同的缓存时间
| 语法 | proxy_cache_valid [code ...] time; |
|-----|------------------------------------|
| 默认值 | — |
| 位置 | http、server、location |
- 如:
```txt
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
为200和302的响应URL设置10分钟缓存为404的响应URL设置1分钟缓存
proxy_cache_valid any 1m;
对所有响应状态码的URL都设置1分钟缓存
```
### 1.5 proxy_cache_min_uses
- 该指令用来设置资源被访问多少次后被缓存
| 语法 | proxy_cache_min_uses number; |
|-----|------------------------------|
| 默认值 | proxy_cache_min_uses 1; |
| 位置 | http、server、location |
### 1.6 proxy_cache_methods
- 该指令用户设置缓存哪些HTTP方法
| 语法 | proxy_cache_methods GET\HEAD\POST; |
|-----|------------------------------------|
| 默认值 | proxy_cache_methods GET HEAD; |
| 位置 | http、server、location |
- 默认缓存HTTP的GET和HEAD方法不缓存POST方法。
# 2. Nginx缓存设置案例
- 需求分析
-![缓存实战](pic/缓存实战.png)
- 步骤实现
- 1.环境准备
- 1在192.168.200.146服务器上的tomcat的webapps下面添加一个js目录并在js目录中添加一个jquery.js文件
- 2启动tomcat
- 3访问测试
```txt
http://192.168.200.146:8080/js/jquery.js
```
- Nginx的环境准备
- 1完成Nginx反向代理配置
```editorconfig
http{
upstream backend{
server 192.168.200.146:8080;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://backend/js/;
}
}
}
```
- 2完成Nginx缓存配置
```editorconfig
http{
proxy_cache_path /usr/local/proxy_cache levels=2:1 keys_zone=itcast:200m inactive=1d max_size=20g;
upstream backend{
server 192.168.200.146:8080;
}
server {
listen 8080;
server_name localhost;
location / {
proxy_cache xxxx1;
proxy_cache_key xxx2;
proxy_cache_min_uses 5;
proxy_cache_valid 200 5d;
proxy_cache_valid 404 30s;
proxy_cache_valid any 1m;
add_header nginx-cache "$upstream_cache_status";
proxy_pass http://backend/js/;
}
}
}
```
# Nginx缓存的清除
## 方式一:删除对应的缓存目录
```shell
rm -rf /usr/local/proxy_cache/......
```
## 方式二:使用第三方扩展模块 ngx_cache_purge
1下载ngx_cache_purge模块对应的资源包并上传到服务器上并解压缩。
```shell
tar -zxf ngx_cache_purge-2.3.tar.gz
```
2修改文件夹名称方便后期配置并查询Nginx的配置参数
```shell
mv ngx_cache_purge-2.3 purge & nginx -V
```
3进入Nginx的安装目录使用./configure进行参数配置
```shell
./configure --add-module=/root/nginx/module/purge
```
4使用make进行编译
```shell
make
```
5将nginx安装目录的nginx二级制可执行文件备份
```shell
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginxold
```
6将编译后的objs中的nginx拷贝到nginx的sbin目录下
```shell
cp objs/nginx /usr/local/nginx/sbin
```
7使用 make 进行升级
```shell
make upgrade
```
8在nginx配置文件中进行如下配置
```shell
server{
location ~/purge(/.*) {
proxy_cache_purge xxxx1 xxx2;
}
}
```
# Nginx设置资源不缓存
- 对于一些经常发生变化的数据。如果进行缓存的话,就很容易出现用户访问到的数据不是服务器真实的数据。所以对于这些资源我们在缓存的过程中就需要进行过滤,不进行缓存。
- Nginx也提供了这块的功能设置需要使用到如下两个指令
- proxy_no_cache
- 该指令是用来定义不将数据进行缓存的条件。
| 语法 | proxy_no_cache string ...; |
|-----|----------------------------|
| 默认值 | — |
| 位置 | http、server、location |
- 配置实例
```editorconfig
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
```
- proxy_cache_bypass
- 该指令是用来设置不从缓存中获取数据的条件。
| 语法 | proxy_cache_bypass string ...; |
|-----|--------------------------------|
| 默认值 | — |
| 位置 | http、server、location |
- 配置实例
```editorconfig
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
```
- 上述两个指令都有一个指定的条件,这个条件可以是多个,并且多个条件中至少有一个不为空且不等于"0",则条件满足成立。
- 里面使用到了三个变量,分别是\$cookie_nocache、\$arg_nocache、\$arg_comment
- $cookie_nocache、\$arg_nocache、\$arg_comment
- 这三个参数分别代表的含义是:
```editorconfig
$cookie_nocache 指的是当前请求的cookie中键的名称为nocache对应的值
$arg_nocache和$arg_comment 指的是当前请求的参数中属性名为nocache和comment对应的属性值
```
- 案例演示下:
```editorconfig
log_format params $cookie_nocache | $arg_nocache | $arg_comment
server{
listen 8081;
server_name localhost;
location /{
access_log logs/access_params.log params;
add_header Set-Cookie 'nocache=999';
root html;
index index.html;
}
}
```
- 案例实现 - 设置不缓存资源的配置方案
```editorconfig
server{
listen 8080;
server_name localhost;
location / {
if ($request_uri ~ /.*\.js$){
set $nocache 1;
}
proxy_no_cache $nocache $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass $nocache $cookie_nocache $arg_nocache $arg_comment;
}
}
```