# Location header, memcached, nginx, django and redirected paths

**URL:** <https://www.ruby-forum.com/t/location-header-memcached-nginx-django-and-redirected-paths/193627>\
**Category:** NGINX\
**Created:** [August 20, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/location-header-memcached-nginx-django-and-redirected-paths/193627 "2010-08-20T20:05:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_Sabchuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Michel\_Sabchuk](https://www.ruby-forum.com/u/Michel_Sabchuk)\
**Post date:** [August 20, 2010, 8:05pm UTC](https://www.ruby-forum.com/t/location-header-memcached-nginx-django-and-redirected-paths/193627/1 "2010-08-20T20:05:52Z")

</div>

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 127.0.0.1:8000 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;
        break;
    }

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

    # 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;  
memcached\_pass 127.0.0.1:11211;

```
    # 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

---

<div class="post-metadata">

**Author:** ![Michel\_Sabchuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3d9bf3/32.png) [@Michel\_Sabchuk](https://www.ruby-forum.com/u/Michel_Sabchuk)\
**Post date:** [August 24, 2010, 3:59pm UTC](https://www.ruby-forum.com/t/location-header-memcached-nginx-django-and-redirected-paths/193627/2 "2010-08-24T15:59:26Z")

</div>

Hi people,

> I using nginx, django and memcached for a site.  
> 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 found what I missed: the error\_page instruction don’t get the  
response code of the fallback unless I use a equal sign.

Where I wrote:

“error\_page 501 502 404 405 @fallback;”

I must write:

“error\_page 501 502 404 405 = @fallback;”

Thanks!

–  
Michel Thadeu Sabchuk  
Curitiba - Brasil
