Nginx > Varnish > Gunicorn Error Too many Redirections

Posted by kollo on Server Fault See other posts from Server Fault or by kollo
Published on 2012-11-29T10:23:42Z Indexed on 2012/11/29 11:08 UTC
Read the original article Hit count: 312

Filed under:
|
|
|

I have the following config:

Nginx > Varnish > Gunicorn > Django

I want to cache 2 versions of same site (mobile & web) with Varnish.

Gunicorn :

WEB: gunicorn_django --bind 127.0.0.1:8181

MOBILE: gunicorn_django --bind 127.0.0.1:8182

Nginx:

WEB:

server {
        listen 80;
        server_name www.mysite.com;

        location / { 
                proxy_pass http://127.0.0.1:8282;   # pass to Varnish
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

MOBILE:

server {
        listen 80;
        server_name m.mysite.com;

        location / { 
                proxy_pass http://127.0.0.1:8282;   # pass to Varnish
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

Varnish: default.vcl

backend mobile_mysite {
    .host = "127.0.0.1";
    .port = "8182";
}

backend mysite {
    .host = "127.0.0.1";
    .port = "8181";
} 

sub vcl_recv {
    if (req.http.host  ~ "(?i)^(m.)?mysite.com$") {
        set req.http.host = "m.mysite.com";
        set req.backend = mobile_mysite;
    }elsif (req.http.host  ~ "(?i)^(www.)?mysite.com$") {
        set req.http.host = "mysite.com";
        set req.backend = mysite;
    }
    if (req.url ~ ".*/static") {
        /* do not cache static content */
        return (pass);
    }
}

The problem:

On Nginx if I setup Mobile version with Varnish (port 8282) and let WEB version with Gunicorn( port 8181), MOBILE is cached by varnish, both WEB & MOBILE works but WEB is not cached. If I set the proxy_pass of WEB version to Varnish (port 8282) and restart Nginx I got an error when accessing web version (www.mysite.com) "Too many redirections" .

I Think my problem come from the Varnish config file, as the site works well if I setup Nginx proxy_pass to Gunicorn ports (MOBILE & WEB).

© Server Fault or respective owner

Related posts about nginx

Related posts about django