Redirection error Nginx + Varnish Solution - Solved

I recently enabled HTTPS on this site and wanted to use a 301 redirect in order to correctly re-route guests from HTTP to HTTPS (HTTP to SSL/TLS). I originally performed all of my rewrites in Apache which acts as my backend. While Apache handled the typical non-www to www redirects with ease, it created a redirect loop when attempting to redirect users from HTTP to HTTPS. I decided to let Varnish Cache 4 rather than the Apache backend handle the redirect.
HTTP to HTTPS redirect
The documentation on the on the Varnish site is for Varnish 3 which is not compatible for Varnish 4 as of this writing:
sub vcl_recv {
    if ( ( ~ "^(?i)" || ~ "^(?i)")
         && req.http.X-Forwarded-Proto !~ "(?i)https") {
        set req.http.x-Redir-Url = "" + req.url;
        error 750 req.http.x-Redir-Url;

sub vcl_error {
    if (obj.status == 750) {
        set obj.http.Location = obj.response;
        set obj.status = 302;
        return (deliver);
After some research, I found a redirect example that was similar to what I was trying to achieve in Varnish 4:
sub vcl_recv {
        if ( ( ~ "^(?i)" || ~ "^(?i)") && req.http.X-Forwarded-Proto !~ "(?i)https") {
                return (synth(750, ""));

sub vcl_synth {
    if (resp.status == 750) {
        set resp.status = 301;
        set resp.http.Location = "" + req.url;
Now non-HTTPS requests to domains listed in the vcl_recv should redirect to the respective HTTPSversion of your site.

Popular posts from this blog

How to Delete WHM Mail Queue through SSH sent from a particular sender

how to change witribe wlan password? how to secure your witribe router

SOLVED: WHM/CPANEL Your server does not support the connection encryption type you have specified. Try changing the encryption method