# Trying to bypass cached pages with nginx

**URL:** <https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215>\
**Category:** Deployment\
**Created:** [August 3, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215 "2007-08-03T21:27:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@seth](https://www.ruby-forum.com/u/seth)\
**Post date:** [August 3, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215/1 "2007-08-03T21:27:56Z")

</div>

I am using page caching on a rails site and I want a logged-in admin  
to be served the non-cached site. This needs to be done in our nginx  
config but I haven’t been able to get anything to work.

My first (and most logical) attempt was to set a cookie for admins and  
have a

conditional statement in the nginx config that checks for  
that cookie. If it finds it, it should go directly to the proxy\_pass.  
Here is what that looked like:

```
if ($http_cookie ~* "nocached=true") {
  proxy_pass http://mongrel;
  break;
}
```

This did not work. If I put a rewrite line in that conditional  
statement, it works. That means it is recognizing the cookie  
correctly and that there is a problem with my approach. I think I  
might need to use different syntax to use only the mongrels and not  
load the cached pages but I don’t know what that would look like. If  
you have tried something like this, I’d love to hear what you found.  
Thanks!

---

<div class="post-metadata">

**Author:** ![seth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@seth](https://www.ruby-forum.com/u/seth)\
**Post date:** [August 3, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215/2 "2007-08-03T22:20:00Z")

</div>

> if ($http\_cookie ~\* “nocached=true”) {  
> you have tried something like this, I’d love to hear what you found.  
> Thanks!

It might be working… mongrel will serve up files cached into public as  
well. So the above might very well be handing it to mongrel, mongrel  
sees  
the cached file and serves it up on it’s own.

-philip

---

<div class="post-metadata">

**Author:** ![seth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@seth](https://www.ruby-forum.com/u/seth)\
**Post date:** [August 3, 2007, 10:34pm UTC](https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215/3 "2007-08-03T22:34:04Z")

</div>

On Aug 3, 1:34 pm, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > if ($http\_cookie ~\* “nocached=true”) {  
> > you have tried something like this, I’d love to hear what you found.  
> > Thanks!
> 
> It might be working… mongrel will serve up files cached into public as  
> well. So the above might very well be handing it to mongrel, mongrel sees  
> the cached file and serves it up on it’s own.
> 
> -philip

Wow, I had not considered that. I will change the location of the  
cached pages to public/cache and have nginx serve cached pages from  
there. That way, Mongrel won’t recognize that there are static files  
for the requests. I will let you know if that works. Thanks philip!

---

<div class="post-metadata">

**Author:** ![seth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f14d63/32.png) [@seth](https://www.ruby-forum.com/u/seth)\
**Post date:** [August 4, 2007, 1:05am UTC](https://www.ruby-forum.com/t/trying-to-bypass-cached-pages-with-nginx/111215/4 "2007-08-04T01:05:24Z")

</div>

On Aug 3, 1:33 pm, Seth [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > might need to use different syntax to use only the mongrels and not  
> > > Wow, I had not considered that. I will change the location of the  
> > > cached pages to public/cache and have nginx serve cached pages from  
> > > there. That way, Mongrel won’t recognize that there are static files  
> > > for the requests. I will let you know if that works. Thanks philip!

Alright, that did the trick. The only other problem I ran into is  
that the $request\_filename variable is an absolute path. I had been  
trying to use that variable in a path like $document\_root/cache/  
$request\_filename.html - this didn’t work, I had to use the $uri  
variable instead: $document\_root/cache/$uri.html

Thanks again!
