# Reverse Proxy Cache Setup

**URL:** <https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944>\
**Category:** NGINX\
**Created:** [March 29, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944 "2010-03-29T15:42:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 29, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/1 "2010-03-29T15:42:22Z")

</div>

I’m trying to use nginx as a reverse cahce for my website which may  
require people to be logged in. I’m trying to figure out how to  
configure it so that users that don’t have a certain cookie set receive  
cached pages while others will receive non-cached responses, assuming  
the cookie is “logmein”.

Posted at Nginx Forum:

> **[Reverse Proxy Cache Setup](https://forum.nginx.org/read.php?2,69049,69049#msg-69049)**
>
> I'm trying to use nginx as a reverse cahce for my website which may require people to be logged in. I'm trying to figure out how to configure it so that users that don't have a certain cookie set receive cached pages while others will receive...

---

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 29, 2010, 6:45pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/2 "2010-03-29T18:45:36Z")

</div>

## Ryan M. Wrote:

> you’re using for  
> all.
> 
> –  
> RPM
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Thanks Ryan. I don’t have much control over the application itself, so I  
guess I’d need to enforce the caching somehow for users that are logged  
in and disable it for those that aren’t from nginx.

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,69049,69127#msg-69127](http://forum.nginx.org/read.php?2,69049,69127#msg-69127)

---

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 29, 2010, 5:47pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/3 "2010-03-29T17:47:39Z")

</div>

On Mon, Mar 29, 2010 at 8:41 AM, royo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m trying to use nginx as a reverse cahce for my website which may require people to be logged in. I’m trying to figure out how to configure it so that users that don’t have a certain cookie set receive cached pages while others will receive non-cached responses, assuming the cookie is “logmein”.

Use $http\_cookie\_$logmein appended to whatever you’re using for  
proxy\_cache\_key, so that you get a unique cache key for each user.  
This assumes that $logmein has a different value for every logged-in  
user (i.e. it is a session ID).

You can also have the back-end explicitly set “Cache-Control:  
no-cache” for logged in pages, so they don’t get stored by nginx at  
all.

–  
RPM

---

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 29, 2010, 7:07pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/4 "2010-03-29T19:07:51Z")

</div>

On Mon, Mar 29, 2010 at 11:44 AM, royo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Ryan. I don’t have much control over the application itself, so I guess I’d need to enforce the caching somehow for users that are logged in and disable it for those that aren’t from nginx.

If you include a unique string in your nginx proxy\_cache\_key, then  
logged-in user requests should never be in cache (again, assuming the  
back-end sets cache-control headers).

So, assuming you have “proxy\_cache\_key  
$http\_host$request\_uri$http\_cookie\_logmein” in your nginx config:

A request for [http://mine.example.com/foo](http://mine.example.com/foo) from an anonymous user would  
get nginx cache key:  
[http://mine.example.com/foo](http://mine.example.com/foo) ← logmein is empty  
This result could be served from cache for future anonymous requests

A request for [http://mine.example.com/foo](http://mine.example.com/foo) from a a logged in user user  
would get nginx cache key:  
[http://mine.example.com/fooAB324494354CD43254DC](http://mine.example.com/fooAB324494354CD43254DC) ← logmein  
contains user’s session ID  
In this case, there would not be a cache hit on the nginx side, and a  
logged in user’s request is passed to the back-end and they get a  
customized version of the page. No anonymous users would ever see that  
because they don’t have that session ID. Presumably, the back-end must  
set “Cache-Control: private” or “Cache-Control: no-cache” for  
logged-in users. If it doesn’t, you’re really going to have problems.

I do not think that nginx proxy directives are allowed inside an “if”  
block. You might be able to use “if $http\_cookie\_logmein” to redirect  
internally to a named nginx location that does not have any caching.

Can you post your nginx configuration?

–  
RPM

---

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 29, 2010, 8:30pm UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/5 "2010-03-29T20:30:23Z")

</div>

## Ryan M. Wrote:

> logged-in user requests should never be in cache  
> [http://mine.example.com/foo](http://mine.example.com/foo) ← logmein is  
> In this case, there would not be a cache hit on  
> going to have problems.
> 
> –  
> RPM
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

The app sets “Cache-Control: private” right now for everyone, and with  
the following setup even users that are not logged in receive a fresh  
copy of the page every time. The said cookie is set when logging in to  
the domain [xyz.com](http://xyz.com)(not actual domain) as well as other cookies but when  
using the nginx proxy only the other cookies are set and the logmein  
cookie isn’t visible.

http {  
include mime.types;  
default\_type application/octet-stream;

```
#log_format main '$remote_addr - $remote_user [$time_local] 

```

“$request” ’  
# '$status $body\_bytes\_sent “$http\_referer” ’  
# ‘“$http\_user\_agent” “$http\_x\_forwarded\_for”’;

```
#access_log logs/access.log main;

```

client\_body\_buffer\_size 128K;  
client\_header\_buffer\_size 128K;  
client\_max\_body\_size 1M;  
large\_client\_header\_buffers 1 1k;

client\_body\_timeout 60;  
client\_header\_timeout 60;  
keepalive\_timeout 60 60;  
send\_timeout 60;

ignore\_invalid\_headers on;  
keepalive\_requests 100;  
limit\_zone gulag $binary\_remote\_addr 5m;  
recursive\_error\_pages on;

```
sendfile on;

```

server\_name\_in\_redirect off;  
server\_tokens off;  
tcp\_nodelay on;  
tcp\_nopush on;

gzip on;  
gzip\_buffers 16 8k;  
gzip\_comp\_level 6;  
gzip\_http\_version 1.0;  
gzip\_min\_length 0;  
gzip\_types text/plain text/css image/x-icon application/x-perl  
application/x-httpd-cgi;  
gzip\_vary on;

proxy\_buffering on;  
proxy\_cache\_path /optimum/nginx/cache levels=1:2  
keys\_zone=my-cache:8m max\_size=1000m inactive=600m;  
proxy\_temp\_path /optimum/nginx/cache/tmp;  
proxy\_set\_header X-Forwarded-For $remote\_addr;  
proxy\_redirect off;  
proxy\_connect\_timeout 30;  
proxy\_send\_timeout 30;  
proxy\_read\_timeout 30;  
proxy\_cache\_valid any 10m;

```
server {
    listen 80;
    server_name localhost;

    location / {

  proxy_pass http://xyz.com;
  proxy_cache my-cache;

```

proxy\_pass\_header Set-Cookie;

proxy\_cache\_key $host$request\_uri$http\_cookie\_logmein;

```
  proxy_cache_valid 404 1m;

        index index.php index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }

```

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,69049,69144#msg-69144](http://forum.nginx.org/read.php?2,69049,69144#msg-69144)

---

<div class="post-metadata">

**Author:** ![royo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@royo](https://www.ruby-forum.com/u/royo)\
**Post date:** [March 31, 2010, 5:05am UTC](https://www.ruby-forum.com/t/reverse-proxy-cache-setup/185944/6 "2010-03-31T05:05:30Z")

</div>

On Mon, Mar 29, 2010 at 1:29 PM, royo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The app sets “Cache-Control: private” right now for everyone,  
> and with the following setup even users that are not logged in  
> receive a fresh copy of the page every time. The said cookie  
> is set when logging in to the domain [xyz.com](http://xyz.com)(not actual  
> domain) as well as other cookies but when using the nginx  
> proxy only the other cookies are set and the logmein cookie  
> isn’t visible.

If nginx can see one cookie for /, it should see them all unless it is  
scoped improperly. Have you used fiddler or Firebug to be sure that  
the cookie isn’t ebing set and passed?

Secondly, if the back-end is setting Cache-Control: private, then  
nginx will obey and not cache anything. Changing the back-end to set  
“Cache-Control: public,max-age=3600” or whatever is the easiest route.

If you can’t do that, you can do something like this with “if” and  
rewrite, to a location that does not use the cache at all for logged  
in user requests. $http\_cookie\_logmein should be accessible for such  
requests, you can echo it back for debuggin using:

add\_header “X-Logmein-Received” $http\_cookie\_logmein

Once again, debugging with Fiddler or Firebug will let you see these  
headers and work out the kinks.
