Stop users accessing website via IP address

Puneet puneet.arora at insticator.com
Sat Jul 27 23:15:31 CEST 2013


Hi Stephen,

 

I think I did not stated the problem correctly.

 

Let me explain the issue:

Say, my website www.mywebsite.com <http://www.mywebsite.com>  is hosted on
IP : 167.123.98.20

Now the Users could type in the IP address i.e. 167.123.98.20 and access my
website as the server is listening at port 80.

 

Now, if any user does that, I want to redirect the Users to
www.mywebsite.com <http://www.mywebsite.com> .

I don't want any User transaction to be done using 167.123.98.20.

 

Therefore I had the following code in place.

 

//If the User access my website using the IP address

vcl_recv() {
     if(req.url ~ "167.123.98.20") {
     error 750 "Moved Permanently";
  } } 

 

Redirect the user to www.mywebsite.com.

sub vcl_error {
    if (obj.status == 750) {
    set req.http.X-REDIRURL = regsub(req.url,"https?://[^/$]+", "
<http://www.linkedin.com/redirect?url=http%3A%2F%2Fmywebsite%2Ecom&urlhash=5
qRF&_t=tracking_anet> http://mywebsite.com");
    set obj.http.Location = req.http.X-REDIRURL;
    set obj.status = 301;
    unset req.http.X-REDIRURL;
    return(deliver);
}

 

I hope I clarified the issue.

 

Thanks

Puneet

 

From: Stephen Wood [mailto:smwood4 at gmail.com] 
Sent: Saturday, July 27, 2013 5:05 PM
To: Puneet
Cc: varnish-misc at varnish-cache.org
Subject: Re: Stop users accessing website via IP address

 

Hello, Puneet. You'll need to do the blocking on the sub vcl_recv and not
sub vcl_error.

 

Here's an easy VCL config example:

 

acl forbidden {
  "xxx.xxx.xxx.xxx";
  "xxx.xxx.xxx.xxx";
}

sub vcl_recv {
  # Prevent access from these hosts
  if (client.ip ~ forbidden) {
    error 403 "Forbidden";
  }
}


Let me know if you have any other questions.

 

On Sat, Jul 27, 2013 at 1:48 PM, Puneet <puneet.arora at insticator.com
<mailto:puneet.arora at insticator.com> > wrote:

Hi all,


I want to stop the users accessing my website via IP address.
I am using varnish as cache.
I have the following code in place but it is not working.

In vcl_recv() {
     if(req.url ~ "XX.XX.XXX.XXX") {
     error 750 "Moved Permanently";
  } } 

And in vcl_error()
sub vcl_error {
    if (obj.status == 750) {
    set req.http.X-REDIRURL = regsub(req.url,"https?://[^/$]+", "
<http://www.linkedin.com/redirect?url=http%3A%2F%2Fmywebsite%2Ecom&urlhash=5
qRF&_t=tracking_anet> http://mywebsite.com");
    set obj.http.Location = req.http.X-REDIRURL;
    set obj.status = 301;
    unset req.http.X-REDIRURL;
    return(deliver);
}

But this does not redirect the user to the website, instead it delivers the
page.
Can anyone tell what I am missing?

 

Thanks

Puneet


_______________________________________________
varnish-misc mailing list
varnish-misc at varnish-cache.org <mailto:varnish-misc at varnish-cache.org> 
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-misc





 

-- 

Stephen Wood

Dev/Ops Engineer

Moz, Inc.

Website: www.heystephenwood.com <http://www.heystephenwood.com> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.varnish-cache.org/lists/pipermail/varnish-misc/attachments/20130727/b8c5def5/attachment-0001.html>


More information about the varnish-misc mailing list