Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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  ~ "^m.mysite.com$") {
        set req.http.host = "m.mysite.com";
        set req.backend = mobile_mysite;
    }elsif (req.http.host  ~ "^(www.)?mysite.com$") {
        set req.http.host = "mysite.com";
        set req.backend = mysite;
    }
    if (req.url ~ ".*/static") {
        /* do not cache static content */
        return (pass);
    }
}

VARNISH command:

/usr/local/sbin/varnishd -P /var/run/varnish.pid -a 127.0.0.1:8282 -f /etc/varnish/default.vcl -T 127.0.0.1:6082 -t 120 -w 50,1000,120 -u varnish -g varnish

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).

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.