Location header, memcached, nginx, django and redirected paths

Michel Sabchuk michelts at gmail.com
Fri Aug 20 22:04:52 MSD 2010

Hi guys, how are you?

I using nginx, django and memcached for a site. If some path isn't on
memcached, nginx send it to apache/django, the cache are generated
and, until it expires, nginx takes care of the requests by sending it
to memcached directly.

It works very well except when django tries to redirect a user using a
301 or 302 redirect (for login protected pages in example).

I reading the debug but, as far as I could undestand, the problem is
related with the "memcached_pass" instruction. I removed the
memcached_pass instruction sending all the traffic to the fallback and
the redirects worked again.

Is there any way to test the request and check if it is a redirect
and, in this case, just do a proxy_pass? I see that all redirects have
a "Location: something" header. I could not test it, does anyone know
how to?

Here is my configuration file:

upstream backend {
    server weight=1;

server {
    listen 80 default;
    server_name localhost;

    location / {

        # do not cache post requests
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        if ($request_method = POST) {
            proxy_pass http://backend;

        # how to check if this is a redirection?
        if ($http_location ~* .+) {
            proxy_pass http://backend;

        # each page can have a pv cookie that sets the page version of this
        # page. It can be used for login pages
        if ($http_cookie ~* "pv=([^;]+)(?:;|$)") {
            set $page_version $1;
        set $memcached_key PR:$request_uri&pv=$page_version;
        default_type text/html;

        # if the page was not found in memcached, go to the backend
        error_page 501 502 404 405 @fallback;

    location @fallback {
        proxy_pass http://backend;
        proxy_set_header   Host             $host;
        proxy_set_header   X-Real-IP        $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;


Can I paste some nginx error log snippet (debugged)?

Thanks for any help.

Michel Thadeu Sabchuk
Curitiba - Brasil

More information about the nginx mailing list