# proxy\_cache\_valid

<mark style="color:blue;">**proxy\_cache\_valid**</mark>指令<mark style="color:blue;">**可以针对不同的HTTP响应状态设置不同的缓存时间**</mark>，其语法结构为：

```nginx
proxy_cache_valid [ code ...] time;
```

* <mark style="color:blue;">**code**</mark>，设置**HTTP响应的状态码**。
  * 该指令可选，如果**不设置**，Nginx服务器只为HTTP状态代码为**200**、**301**和**302**的响应数据做缓存。
  * 可以使用“**any**”表示缓存所有该指令中未设置的其他响应数据。
* <mark style="color:blue;">**time**</mark>，设置缓存时间。

{% hint style="success" %}

```nginx
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
```

该例子中，对返回状态为**200**和**302**的响应数据缓存**10分钟**，对返回状态为**301**的响应数据缓存**1小时**，对返回状态为**非200、302和301**的响应数据缓存**1分钟**。
{% 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_valid.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.
