1

I've tried to install Varnish Cache with Magento 2. It seems to work OK (although I still have a no-cache to find and to delete apparently ...) but my .htaccess redirection for my "coming soon" page doesn't work anymore.

I have standard .htaccess code to redirect other users (except me) to my coming soon page:

 # MAINTENANCE-PAGE REDIRECT
<IfModule mod_rewrite.c>
 RewriteEngine on
 RewriteCond %{REMOTE_ADDR} !^xx\.xxx\.xxx\.xxx
 RewriteCond %{REMOTE_ADDR} !^xx\.xxx\.xxx\.xxx
 RewriteCond %{REMOTE_ADDR} !^xx\.xxx\.xxx\.xx
 RewriteCond %{REMOTE_ADDR} !^xxx\.xxx\.xxx\.xxx
# RewriteCond %{REQUEST_URI} !coming/soon.html$ [NC]
 RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js|svg|eot|woff|woff2|php) [NC]
 RewriteCond %{REQUEST_URI} !^/coming/soon\.html$
RewriteRule ^(.*)$ https://example.com/coming/soon.html [R=302,L]
</IfModule>

This works perfectly without the Varnish Cache activated. With the Varnish Cache, all the people (also me) are redirected to my coming soon page.

I tried to set my varnish cache .vcl file to do the same thing, but I have an error (with my IP and without), with a blank varnish cache page and an error 302 code with my URL https://www.example.com/coming/soon.html :

acl nosip {
    "xx.xxx.xxx.xxx";
    "xx.xxx.xx.xxx";
    "xx.xxx.xxx.xx";
    "xxx.xxx.xxx.xxx";
}


sub vcl_recv {

    # Methode ip exclusion 
    if (client.ip !~ nosip) {
    return (synth(302, "https://www.example.com/coming/soon.html"));
    }
}

Is this the right way to do it ?

0

Varnish is implemented as a reverse proxy in front of your application server, so the REMOTE_ADDR server variable (when it hits your application server) is likely going to be the IP address of this proxy server, not the originating IP of the client.

Assuming Varnish is setting the appropriate X-Forwarded-.... request headers then you will probably need to check the X-Forwarded-For HTTP request header instead. For example:

RewriteCond %{HTTP:X-Forwarded-For} !^xx\.xxx\.xxx\.xxx

Note that the X-Forwarded-For header is potentially a comma separated string of IP addresses. The IP of each proxy server that is passed through is appended to the end.

| improve this answer | |
  • 1
    Thanks @MrWhite ! It was the good way to do it, I tried to find too difficult :) – deude Aug 13 '19 at 12:36

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.