Question on Rewrite / If statements

Hello,

I have learned that the http_referer module does not support
variables.

I am trying to work around this with if / rewrite statements, but may
have run into the same issue.

How can I make this work?

set $myvar mydomain.com;
if ($http_referer ~* $myvar) {
do something
}

When I run that, it always returns false. However this works:

if ($http_referer ~* mydomain.com) {
do something
}

It appears that the if statement doesn’t evaluate the variable. Is there
anyway around this?

Thanks,

John

On Wed, Sep 16, 2009 at 08:03:26PM -0500, Resicow wrote:

if ($http_referer ~* $myvar) {
anyway around this?
Yes, “if” is ugly hack and it does not evaluate a right part of
expression.

What task do you want to resolve using $http_referer ?

Hi Igor,

Thanks for your response.

We have lots of virtual hosting domains setup, using $host to point to
the correct root directory. This way we can add / subtract domains
without touching the nginx config.

Similar to the “Wildcard Subdomains in a Parent Folder” at
http://wiki.nginx.org/NginxVirtualHostExample

This has been working perfectly for us, however we want to stop leeching
by checking the HTTP Referer.

Since the configuration is “virtual”, only the $host variable actually
“knows” the name of the vhost.

So we want to test to ensure that either $http_referer contains $host
(using an if statement), or using the HTTP Referer module with something
like this:

valid_referers none blocked $host;

Can you think of anyway to have nginx test two variables in this
fashion? Because of our setup, it is not possible to manually enter
these values in the config file, or manually setup a vhost for each
domain.

Thanks,

John

Hi Maxim,

Thanks! Never even thought of doing that with just a reg exp that way -
I was able to adapt this and make it work!

It’s amazing how powerful nginx really is. We have fairly low traffic,
and some big boxes, so I’m not too concerned with the added performance
penalty / overhead of the if / rewrite module.

I know it is ugly, but the rewrite module does allow us to do some great
things at the server level, that we used to have to do in php.

Thanks again, and I’m going to brush up on my reg exp skills a little
tonight.

John

Hello!

On Fri, Sep 18, 2009 at 10:28:50AM -0500, Resicow wrote:

valid_referers none blocked $host;

Can you think of anyway to have nginx test two variables in this
fashion? Because of our setup, it is not possible to manually enter
these values in the config file, or manually setup a vhost for each
domain.

As a workaround you may use something like:

set $blah "$host:$http_host";

if ($blah ~ "^([^:]+):\1$") {
    return 403;
}

Maxim D.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs