# proxy\_cache\_path

<mark style="color:blue;">**proxy\_cache\_path**</mark>指令用于<mark style="color:blue;">**设置Nginx服务器存储缓存数据的路径以及和缓存索引相关的内容**</mark>，其语法结构为：

{% code overflow="wrap" %}

```nginx
proxy_cache_path path [levels=levels] keys_zone=name:size1 [inactive=time1] [max_size=size2] [loader_files=number] [loader_sleep=time2] [loader_threshold=time3];
```

{% endcode %}

* <mark style="color:blue;">**path**</mark>，设置**缓存数据存放的根路径**，该路径应该是**预先存在于磁盘上**的。
* <mark style="color:blue;">**levels**</mark>，设置在相对于path指定目录的第几级hash目录中缓存数据。
  * levels=1，表示一级hash目录；
  * levels=1:2，表示两级，依次类推。
  * **目录的名称是基于请求URL通过哈希算法获取到的**。
* <mark style="color:blue;">**name:size1**</mark>，Nginx服务器的**缓存索引重建进程**在内存中为缓存数据建立索引，这一对变量用来**设置存放缓存索引的内存区域的名称和大小**。
* <mark style="color:blue;">**time1**</mark>，**设置强制更新缓存数据的时间**，当硬盘上的缓存数据在设定的时间内**没有被访问**时， Nginx服务器就强制从硬盘上将其**删除**，下次客户端访问该数据时**重新缓存**。**该指令默认设置为10 s**。
* <mark style="color:blue;">**size2**</mark>，设置**硬盘中缓存数据的大小限制**。硬盘中的缓存源数据由Nginx服务器的**缓存管理进程**进行管理，**当缓存的大小超过该变量的设置时，缓存管理进程将根据**<mark style="color:blue;">**最近最少被访问**</mark>**的策略删除缓存**。
* <mark style="color:blue;">**number**</mark>，设置**缓存索引重建进程每次加载的数据元素的数量上限**。在重建缓存索引的过程中，进程通过一系列的递归遍历读取硬盘上的缓存数据目录及缓存数据文件，对每个数据文件中的缓存数据在内存中建立对应的索引，我们称每建立一个索引为加载一个数据元素。进程在每次遍历过程中可以同时加载多个数据元素，该值限制了每次遍历中同时加载的数据元素的数量。**默认设置为100。**
* <mark style="color:blue;">**time2**</mark>，设置**缓存索引重建进程在一次遍历结束、下次遍历开始之间的暂停时长**。**默认设置为50 ms。**
* <mark style="color:blue;">**time3**</mark>，设置**遍历一次磁盘缓存源数据的时间上限**。**默认设置为200ms。**

{% hint style="success" %}
该指令设置比较复杂，一般需要设置前面三个变量的情形比较多，**后面的几个变量与Nginx服务器缓存索引重建进程及管理进程的性能相关，一般情况下保持默认设置就可以了**。

```nginx
proxy_cache_path /nginx/cache/a levels=1 keys_zone=a:10m;
proxy_cache_path /nginx/cache/b levels=2:2 keys_zone=b:100m;
proxy_cache_path /nginx/cache/c levels=1:1:2 keys_zone=c:1000m;
```

{% endhint %}

{% hint style="info" %} <mark style="color:blue;">**该指令只能放在http块中**</mark>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bohans.gitbook.io/devops/ngnix/ji-chu-zhi-shi/4.-nginx-fu-wu-qi-de-dai-li-fu-wu/4.3-nginx-fu-wu-qi-de-fan-xiang-dai-li-fu-wu/proxy-cache-de-pei-zhi-de-12-ge-zhi-ling/proxy_cache_path.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
