Questions about solutions when using proxy_cache to redirect to cached pages for non logged in users.

Zev Blut zblut at
Tue Feb 16 11:25:43 MSK 2010


I am trying out the proxy_cache commands in nginx.  The recent mails
on the mailing list have been quite helpful for figuring out what to
do.  I am able to use nginx to cache pages for non logged in users and
have logged in users go directly to the backend.

I have a few questions about if I am calling proxy_pass correctly, and
if I can use proxy_pass_header to only let a certain Set-Cookie header

* in location / I rewrite to either the logged in or not logged in states.
But, I have to explicitly regex catch the real uri and add the args to 
the proxy pass.

would lose the ?keyword=nginx if I just used
proxy_pass http://app/$1

Is my solution for calling "proxy_pass http://app/$1?$args;" correct?

* For not logged in users I would only like to let the cookie 
through and block any other cookies.  Is that possible in nginx?

* On a side note, the site has the same uri for multiple languages.
So when a user changes the site's language I am able to have nginx
properly cache based on the language.  Except the user's web browser
will keep a cached copy of the first language, which can create a
confusing experience.  So far my solution is to striping the
Cache-Control from the response header.  This way nginx keeps a copy of
the page in its' cache, but the web browser will not.  I am wondering
if there is a better solution.


The following is a roughly summarized view of my configuration.

----- nginx.conf --------
upstream app {

location / {
   set $iflang "ja";
   set $backend "not_logged_in_user";
   if ($loggeduser) { set $backend "logged_in_user"; }
   rewrite ^(.*)$ /$backend$1;

location ~ ^/not_logged_in_user/(.*)$ {
   proxy_pass http://app/$1?$args;
   proxy_cache one;
   proxy_cache_key   "$scheme$host$iflang$request_uri$args";
   proxy_cache_valid  200 15m;
   proxy_cache_valid  301 1h;
   proxy_cache_use_stale updating error;

   # Ideally would only like to pass through language_code cookie
   proxy_pass_header Set-Cookie;

   # Stop the browser from caching, try to just keep the caching on nginx.
   proxy_hide_header Cache-Control;

location ~ ^/logged_in_user/(.*)$ {
   proxy_pass http://app/$1?$args;
----- nginx.conf --------

More information about the nginx mailing list