# Conditional rule

**URL:** <https://www.ruby-forum.com/t/conditional-rule/188486>\
**Category:** NGINX\
**Created:** [May 12, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486 "2010-05-12T19:09:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 7:09pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/1 "2010-05-12T19:09:48Z")

</div>

Hey,

I need to add conditional rule with more than one test.  
I need something like :  
if ($remote\_addr = “aa.bb.cc.dd” && $request\_uri ~ ^/XXXXX$)  
{

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 7:13pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/2 "2010-05-12T19:13:54Z")

</div>

Sorry for the previous partial email.

Hey,

I need to add conditional rule with more than one test.  
I need something like :  
if ($remote\_addr != “aa.bb.cc.dd” && $request\_uri !~ ^/XXXXX$)  
{  
rewrite …  
}  
else{  
proxy\_pass …  
}

I don’t find the good nginx syntax to obtain a such behaviour.  
I try :  
if ( ) {  
if () {  
}  
}  
without success

What is the proper way to do such condition ?

Vincent

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 8:15pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/3 "2010-05-12T20:15:58Z")

</div>

On Wed, May 12, 2010 at 07:13:00PM +0200, Vincent MAUGE wrote:

> else{
> 
> What is the proper way to do such condition ?

nginx does not support such syntax.  
However, the proper way is to not use "if"s at all.  
nginx should be configured using locations but not if/rewrites.

Instead of backward logic:

if ($request\_uri !~ ^/XXXXX$) {  
part A  
}

you should use clear logic:

```
location / {
    part A
}

location /XXXXXX {
    part B
}

```

What do you want to configure ?

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 10:21pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/4 "2010-05-12T22:21:14Z")

</div>

On Wed, May 12, 2010 at 10:01:12PM +0200, Vincent MAUGE wrote:

> > > {  
> > > š }
> > 
> > š šlocation /XXXXXX {  
> > exemple directory /documentation.
> 
> Whar is the best way ? to configure 2 location ?

http {  
geo $https\_only {  
default 1;  
10.10.10.10 0;  
10.10.10.20 0;  
…  
}

```
 server {
      listen 80;

      root ..

      location / {
          if ($https_only) {
              rewrite ^ https://site.com$request_uri? permanent;
          }
          proxy_pass ...
      }

      location /XXXX {
          proxy_pass ...
      }
 }

 server {
      listen 443;

      ssl stuff

      root ..

      location / {
          proxy_pass ...
      }
 }

```

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 10:01pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/5 "2010-05-12T22:01:54Z")

</div>

2010/5/12 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > rewrite …  
> > }  
> > if ($request\_uri !~ ^/XXXXX$) {  
> > part B  
> > }
> 
> What do you want to configure ?

My nginx is configure as a reverse proxy (http and https).  
I want to allow some ip to access both for example 10.10.10.10.  
Others are allow to access only https and get a permanent redirect on  
http to https except for specific url where I allow http/https for  
exemple directory /documentation.

Whar is the best way ? to configure 2 location ?

Thanks for your answer.

Vincent

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 10:41pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/6 "2010-05-12T22:41:44Z")

</div>

On Wed, May 12, 2010 at 10:29:52PM +0200, Vincent MAUGE wrote:

> > > > > I need to add conditional rule with more than one test.  
> > > > > I try :  
> > > > > nginx should be configured using locations but not if/rewrites.  
> > > > > š š š špart A  
> > > > > I want to allow some ip to access both for example 10.10.10.10.  
> > > > > š š š š 10.10.10.20 š0;  
> > > > > š š š š š š š š šrewrite ^ https://site.com$request\_uri? špermanent;  
> > > > > š š š š šlisten 443;
> > 
> > –  
> > Igor S.  
> > [Igor Sysoev](http://sysoev.ru/en/)
> 
> Thanks Igor. This match exactly my need. I didn’t notice geo Module until now.

“root ..” is surplus here, since you use only proxying.

The other way to say

š š š š šlocation / {  
š š š š š š šif ($https\_only) {  
š š š š š š š š šrewrite ^ https://site.com$request\_uri? špermanent;  
š š š š š š š}  
š š š š š š šproxy\_pass …  
š š š š š}

is

š š š š šlocation / {  
š š š š allow 10.10.10.10;  
š š š š allow 10.10.10.20;  
deny all;

š š š š š š šerror\_page 403š https://site.com$request\_uri;

š š š š š š šproxy\_pass …  
š š š š š}

Although in this case redirect will be with 302 status code.

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**

---

<div class="post-metadata">

**Author:** ![Vincent\_MAUGE](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/e68b1a/32.png) [@Vincent\_MAUGE](https://www.ruby-forum.com/u/Vincent_MAUGE)\
**Post date:** [May 12, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/conditional-rule/188486/7 "2010-05-12T22:30:36Z")

</div>

2010/5/12 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > I need something like :  
> > > > if ( ) {
> > > 
> > > }  
> > > Others are allow to access only https and get a permanent redirect on  
> > > …  
> > > }
> 
> –  
> Igor S.  
> [Igor Sysoev](http://sysoev.ru/en/)

Thanks Igor. This match exactly my need. I didn’t notice geo Module  
until now.

Vincent
