0

I am trying to implement varnish on my ssl enalbled website. But i am getting loop redirect. This is how i am going

varnish is listening on port 80;

DAEMON_OPTS="-a :80 \
         -T localhost:6082 \
         -f /etc/varnish/default.vcl \
         -S /etc/varnish/secret \
         -s malloc,256m"

and backend

backend default {
.host = "127.0.0.1";
.port = "8080";
.connect_timeout = 16s;
.first_byte_timeout = 96s;
.between_bytes_timeout = 8s;

}

nginx is running on 8080 . nginx configuration

upstream backend {   
  ip_hash;
  server 127.0.0.1:80; # IP goes here.
}

server {   
    listen        443 ssl;
    server_name   example.com www.example.com;
    root       /var/www/test/public;
    index           index.php;


    ssl_certificate      /var/www/ascacacaa1341.crt;
    ssl_certificate_key  /var/www/www.example.com.key;

    ssl_session_cache    shared:SSL:10m;
    ssl_session_timeout  10m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
        proxy_pass http://backend;
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;          
        proxy_set_header X-Forwarded-Port 443;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Host $host;
        proxy_set_header X-Secure on;
 }
 location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/test/public$fastcgi_script_name;
        }
     location /var/www/test/public {
         autoindex on;
         autoindex_exact_size off;
        }  
    # We don't need .ht files with nginx.
        location ~ /\.htaccess {
            deny all;
        }

        # Set header expirations on per-project basis
         location ~* \.(?:ico|css|js|jpe?g|JPG|png|svg|woff)$ {
            expires 365d;
        }
}

server {

    listen 8080;       
    server_name  example.com www.example.com;
    return https://$server_name$request_uri;


}





tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      30700/nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30479/varnishd
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      30700/nginx
tcp        0      0 127.0.0.1:6082          0.0.0.0:*               LISTEN      30475/varnishd
tcp6       0      0 :::80                   :::*                    LISTEN      30479/varnishd

curl response curl -I -k https://www.domain.com

HTTP/1.1 302 Moved Temporarily
Server: nginx/1.6.2
Date: Mon, 03 Nov 2014 10:38:31 GMT
Content-Type: text/html
Content-Length: 160
Connection: keep-alive
Location: https://www.domain.com/
X-Varnish: 623340 361289
Age: 280
Via: 1.1 varnish-v4

It does work well for ssl . proxy redirect is not working . I am redirecting to port 80 where varnish is listening. But still i am getting loop redirect. from inspect i find net::ERR_TOO_MANY_REDIRECTS. error. Any one who can help here. I have more domain and sub domains in this same server. But i won't to implement varnish on ssl domain only .

  • Why aren't you providing everything in https ? – Xavier Lucas Oct 31 '14 at 21:38
  • @XavierLucas But i am still getting that redirect loop on providing everything on https . – Anil Sharma Nov 1 '14 at 9:44
  • Can you post updated configuration so ? Did you check redirects aren't issued by proxied servers ? – Xavier Lucas Nov 1 '14 at 13:43
  • @XavierLucas Check updated configuration. – Anil Sharma Nov 1 '14 at 15:24
1

The issue is there :

server_name  www.example.com example.com

...    

location / {
   rewrite ^ https://$server_name$request_uri permanent;
}

You are responding to domain www.example.com and example.com requests on port 8080 and redirecting to www.example.com because server_name will always match the first server_name value while you are willing to respond https requests for example.com.

So change that server block to :

server {
    listen 8080;
    server_name example.com www.example.com;
    return https://$server_name$request_uri;
}

Btw stop using rewrite / regex when you don't need it.

| improve this answer | |
  • Thanks for help But still i am getting same infinite redirect. Take a look at my config and ports listening to nginx and varnish. – Anil Sharma Nov 3 '14 at 10:12
  • @Creator Are you testing with a browser or with curl ? – Xavier Lucas Nov 3 '14 at 10:21
  • I am testing in browser – Anil Sharma Nov 3 '14 at 10:23
  • 1
    @Creator Then test with curl because browsers are caching response headers. – Xavier Lucas Nov 3 '14 at 10:25
  • HTTP/1.1 302 Moved Temporarily . This is what i am getting.Some Config error . – Anil Sharma Nov 3 '14 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.