# A 503 page gets written to my proxy cache, overwriting the 200

**URL:** <https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552>\
**Category:** NGINX\
**Created:** [January 16, 2014, 3:03pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552 "2014-01-16T15:03:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 16, 2014, 3:03pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552/1 "2014-01-16T15:03:22Z")

</div>

Hi,

I’m trying to use the proxy cache to store regular pages (200) from my  
web  
server so that when the web server goes into maintenance mode and starts  
returning 503 nginx can still serve the good page out of cache. It  
works  
great for a few minutes but then at some point (5 to 10 minutes in)  
nginx  
will overwrite the good 200 page in the cache with the bad 503 page and  
then  
start handing out the 503. Looking at my config I don’t understand how  
a  
503 could ever get written to cache but it is. And the 200 page was  
brand  
new (written 10 minutes before) so it shouldn’t be the “inactive” time  
on  
the proxy\_cache\_path setting causing nginx to delete the good file. Can  
anyone tell me what I’m missing? Here are the relevant pieces of my  
config:

```
    proxy_cache_path /var/www/cache levels=1:2 

```

keys\_zone=my-cache:500m  
max\_size=3000m inactive=120h;  
proxy\_temp\_path /var/www/cache/tmp;  
proxy\_cache\_key “$scheme$host$request\_uri”;

```
    map $http_cookie $GotSessionCookie {
            default "";
            "~(?P<sessionid>\bSESS[^;=]+=[^;=]+)" $sessionid;
    }

    server {
            listen 80;
            server_name _;

            proxy_cache my-cache;

            location / {
                    proxy_pass http://production;
                    proxy_cache_valid 200 301 302 30m;
                    proxy_cache_valid 404 1m;
            }

            # don't cache pages with php's session cookie
            proxy_no_cache $cookie_$GotSessionCookie;

            # bypass the cache if we get a X-NoCache header
            proxy_cache_bypass $http_nocache 

```

$cookie\_$GotSessionCookie;

```
            proxy_cache_use_stale http_500 http_503 error timeout

```

invalid\_header updating;  
}

I can’t imagine how a 503 would ever get cached given those  
proxy\_cache\_valid lines but maybe I don’t understand something. Thanks  
for  
any ideas!

-Rick

Posted at Nginx Forum:

> **[A 503 page gets written to my proxy cache, overwriting the 200](https://forum.nginx.org/read.php?2,246507,246507#msg-246507)**
>
> Hi, I'm trying to use the proxy cache to store regular pages (200) from my web server so that when the web server goes into maintenance mode and starts returning 503 nginx can still serve the good page out of cache. It works great for a few minutes...

---

<div class="post-metadata">

**Author:** ![rge3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@rge3](https://www.ruby-forum.com/u/rge3)\
**Post date:** [January 16, 2014, 5:07pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552/2 "2014-01-16T17:07:14Z")

</div>

Hello!

On Thu, Jan 16, 2014 at 09:02:36AM -0500, rge3 wrote:

> the proxy\_cache\_path setting causing nginx to delete the good file. Can  
> anyone tell me what I’m missing? Here are the relevant pieces of my  
> config:

[…]

> I can’t imagine how a 503 would ever get cached given those  
> proxy\_cache\_valid lines but maybe I don’t understand something. Thanks for  
> any ideas!

An exiting cache can be bypassed due to proxy\_cache\_bypass in your  
config, and 503 response can be cached if it contains  
Cache-Control and/or Expires which allow caching.

–  
Maxim D.  
[http://nginx.org/](http://nginx.org/)

---

<div class="post-metadata">

**Author:** ![rge3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@rge3](https://www.ruby-forum.com/u/rge3)\
**Post date:** [January 16, 2014, 8:51pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552/3 "2014-01-16T20:51:15Z")

</div>

## Maxim D. Wrote:

> An exiting cache can be bypassed due to proxy\_cache\_bypass in your  
> config, and 503 response can be cached if it contains  
> Cache-Control and/or Expires which allow caching.

Oh, I hadn’t thought of that part about the bypass. Okay, that makes  
sense.  
But what about the Cache-Control/Expires causing it to cache the 503…  
that can  
still happen even if my “proxy\_cache\_valid” line doesn’t list a 503?

-Rick

Posted at Nginx Forum:

> **[Re: A 503 page gets written to my proxy cache, overwriting the 200](https://forum.nginx.org/read.php?2,246507,246517#msg-246517)**
>
> Hi, I'm trying to use the proxy cache to store regular pages (200) from my web server so that when the web server goes into maintenance mode and starts returning 503 nginx can still serve the good page out of cache. It works great for a few minutes...

---

<div class="post-metadata">

**Author:** ![rge3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@rge3](https://www.ruby-forum.com/u/rge3)\
**Post date:** [January 17, 2014, 1:41pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552/4 "2014-01-17T13:41:41Z")

</div>

Maxim D. Wrote:

> The “proxy\_cache\_valid” directives are used if there are no  
> Cache-Control/Expires to allow caching (or they are ignored with  
> proxy\_ignore\_headers). That is, with “proxy\_cache\_valid” you can  
> cache something which isn’t normally cached, but they don’t  
> prevent caching of other responses.

That was my missing piece. Thanks so much!! Now it makes sense!

-R

Posted at Nginx Forum:

> **[Re: A 503 page gets written to my proxy cache, overwriting the 200](https://forum.nginx.org/read.php?2,246507,246558#msg-246558)**
>
> Hi, I'm trying to use the proxy cache to store regular pages (200) from my web server so that when the web server goes into maintenance mode and starts returning 503 nginx can still serve the good page out of cache. It works great for a few minutes...

---

<div class="post-metadata">

**Author:** ![rge3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/5daacb/32.png) [@rge3](https://www.ruby-forum.com/u/rge3)\
**Post date:** [January 16, 2014, 11:08pm UTC](https://www.ruby-forum.com/t/a-503-page-gets-written-to-my-proxy-cache-overwriting-the-200/238552/5 "2014-01-16T23:08:39Z")

</div>

Hello!

On Thu, Jan 16, 2014 at 02:50:41PM -0500, rge3 wrote:

> that can  
> still happen even if my “proxy\_cache\_valid” line doesn’t list a 503?

The “proxy\_cache\_valid” directives are used if there are no  
Cache-Control/Expires to allow caching (or they are ignored with  
proxy\_ignore\_headers). That is, with “proxy\_cache\_valid” you can  
cache something which isn’t normally cached, but they don’t  
prevent caching of other responses.

–  
Maxim D.  
[http://nginx.org/](http://nginx.org/)
