# 2 variables inside if()?

**URL:** <https://www.ruby-forum.com/t/2-variables-inside-if/197926>\
**Category:** NGINX\
**Created:** [October 22, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926 "2010-10-22T18:43:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [October 22, 2010, 6:43pm UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926/1 "2010-10-22T18:43:02Z")

</div>

Hello,

I’m trying to compare 2 variables, like so (I’ve made sure the variables  
match):

`if ($arg_hash !~ $sha1) {
...
}`

However in error.log it says:

`*1 "$sha1" does not match
"a78c3ecd2e349d1f1050fa555842a60b585489d8"`

I’ve tried swapping the variables over and the same error occurs, so it  
looks like the variable after the comparison character(s) is considered  
a string by nginx.

Does anyone know of a way to do this? Or of a third party module which  
does this?

Thanks very much.

Posted at Nginx Forum:

> **[2 variables inside if() ?](https://forum.nginx.org/read.php?2,143569,143569#msg-143569)**
>
> Hello, I'm trying to compare 2 variables, like so (I've made sure the variables match): \[code\]if ($arg\_hash !~ $sha1) { ... }\[/code\] However in error.log it says: \[code\]\*1 "$sha1" does not match "a78c3ecd2e349d1f1050fa5

---

<div class="post-metadata">

**Author:** ![untamed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a4c791/32.png) [@untamed](https://www.ruby-forum.com/u/untamed)\
**Post date:** [October 22, 2010, 7:47pm UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926/2 "2010-10-22T19:47:51Z")

</div>

On 22 Out 2010 17h42 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> `*1 "$sha1" does not match
> "a78c3ecd2e349d1f1050fa555842a60b585489d8"`
> 
> I’ve tried swapping the variables over and the same error occurs, so  
> it looks like the variable after the comparison character(s) is  
> considered a string by nginx.

$sha1 is not set by Nginx during runtime. You’ve to set it in your  
config:

Ex: set $sha1 a78c3ecd2e349d1f1050fa555842a60b585489d8

Then compare them in your conditional.

> Does anyone know of a way to do this? Or of a third party module  
> which does this?

HTH,  
— appa

---

<div class="post-metadata">

**Author:** ![untamed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a4c791/32.png) [@untamed](https://www.ruby-forum.com/u/untamed)\
**Post date:** [October 22, 2010, 10:06pm UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926/3 "2010-10-22T22:06:30Z")

</div>

Thank you very much for your reply,

I know for definite both variables are set and are the same, eg:

`set $var1 'yes';
set $var2 'yes';
if ($var1 ~ $var2) {
     ...
}`

Returns:

```auto
*1 "$var2" does not match "yes"
```

So seems like the second variable is considered a string and not a  
variable.

Posted at Nginx Forum:

> **[Re: 2 variables inside if() ?](https://forum.nginx.org/read.php?2,143569,143627#msg-143627)**
>
> Hello, I'm trying to compare 2 variables, like so (I've made sure the variables match): \[code\]if ($arg\_hash !~ $sha1) { ... }\[/code\] However in error.log it says: \[code\]\*1 "$sha1" does not match "a78c3ecd2e349d1f1050fa5

---

<div class="post-metadata">

**Author:** ![untamed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a4c791/32.png) [@untamed](https://www.ruby-forum.com/u/untamed)\
**Post date:** [October 22, 2010, 10:12pm UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926/4 "2010-10-22T22:12:50Z")

</div>

Hello!

On Fri, Oct 22, 2010 at 12:42:08PM -0400, untamed wrote:

> `*1 "$sha1" does not match
> "a78c3ecd2e349d1f1050fa555842a60b585489d8"`
> 
> I’ve tried swapping the variables over and the same error occurs, so it  
> looks like the variable after the comparison character(s) is considered  
> a string by nginx.

Right hand side is considered to be regular expression in “~”  
check, and compiled on configuration reading. No variables are  
expected.

> Does anyone know of a way to do this? Or of a third party module which  
> does this?

Any checks may be done within embedded perl.

In rewrite module you may do this by using combined variable and  
regexp with backreferences (see man pcresyntax for details):

```
set $tt "$arg_hash:$sha1";

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

```

Note well: before using “if” please make sure you’ve read and  
understand [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/IfIsEvil) page.

Maxim D.

---

<div class="post-metadata">

**Author:** ![untamed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/a4c791/32.png) [@untamed](https://www.ruby-forum.com/u/untamed)\
**Post date:** [October 23, 2010, 6:56am UTC](https://www.ruby-forum.com/t/2-variables-inside-if/197926/5 "2010-10-23T06:56:54Z")

</div>

Great, thank you very much Maxim, it works perfectly.

Posted at Nginx Forum:

> **[Re: 2 variables inside if() ?](https://forum.nginx.org/read.php?2,143569,143743#msg-143743)**
>
> Hello, I'm trying to compare 2 variables, like so (I've made sure the variables match): \[code\]if ($arg\_hash !~ $sha1) { ... }\[/code\] However in error.log it says: \[code\]\*1 "$sha1" does not match "a78c3ecd2e349d1f1050fa5
