# Question on Rewrite / If statements

**URL:** <https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648>\
**Category:** NGINX\
**Created:** [September 17, 2009, 3:15am UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648 "2009-09-17T03:15:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Resicow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Resicow](https://www.ruby-forum.com/u/Resicow)\
**Post date:** [September 17, 2009, 3:15am UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648/1 "2009-09-17T03:15:36Z")

</div>

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](http://mydomain.com);  
if ($http\_referer ~\* $myvar) {  
do something  
}

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

if ($http\_referer ~\* [mydomain.com](http://mydomain.com)) {  
do something  
}

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

Thanks,

John

---

<div class="post-metadata">

**Author:** ![Resicow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Resicow](https://www.ruby-forum.com/u/Resicow)\
**Post date:** [September 18, 2009, 1:40pm UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648/2 "2009-09-18T13:40:53Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Resicow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Resicow](https://www.ruby-forum.com/u/Resicow)\
**Post date:** [September 18, 2009, 5:42pm UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648/3 "2009-09-18T17:42:39Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Resicow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Resicow](https://www.ruby-forum.com/u/Resicow)\
**Post date:** [September 18, 2009, 7:33pm UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648/4 "2009-09-18T19:33:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Resicow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/c57346/32.png) [@Resicow](https://www.ruby-forum.com/u/Resicow)\
**Post date:** [September 18, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/question-on-rewrite-if-statements/175648/5 "2009-09-18T17:53:46Z")

</div>

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.
