Conditional rule

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$)
{

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

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.
http://sysoev.ru/en/

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.
http://sysoev.ru/en/

2010/5/12 Igor S. [email protected]:

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

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

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.
http://sysoev.ru/en/

2010/5/12 Igor S. [email protected]:

I need something like :
if ( ) {

}
Others are allow to access only https and get a permanent redirect on

}


Igor S.
Igor Sysoev

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

Vincent