# Cookie and source IP logic in server block

**URL:** <https://www.ruby-forum.com/t/cookie-and-source-ip-logic-in-server-block/236961>\
**Category:** NGINX\
**Created:** [October 15, 2013, 12:17am UTC](https://www.ruby-forum.com/t/cookie-and-source-ip-logic-in-server-block/236961 "2013-10-15T00:17:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [October 15, 2013, 12:17am UTC](https://www.ruby-forum.com/t/cookie-and-source-ip-logic-in-server-block/236961/1 "2013-10-15T00:17:04Z")

</div>

Hello,

I’m trying to block certain IP ranges at my nginx server, but would like  
to  
offer the ability to bypass the block by completing a back-end CAPTCHA,  
which would set a cookie.

Currently I set the block like so:

geo $remote\_addr $blocked {  
default 0;  
include /etc/nginx/conf/nginx-blocked-ips.conf;  
}

…

recursive\_error\_pages on;  
error\_page 429 = @banned;  
if ($blocked = 1) {  
return 429;  
}

location @banned {  
set $args “”;  
rewrite ^ /banned/ ;  
}

Since I can’t nest “if” statements and I can’t make a compound check  
using  
“&&” or “||” or something similar, how can I check both the blocked  
variable  
and look to see if a cookie is set?

Posted at Nginx Forum:

> **[cookie and source IP logic in server block](https://forum.nginx.org/read.php?2,243687,243687#msg-243687)**
>
> Hello, I'm trying to block certain IP ranges at my nginx server, but would like to offer the ability to bypass the block by completing a back-end CAPTCHA, which would set a cookie. Currently I set the block like so: geo $remote\_addr $blocked { defa

---

<div class="post-metadata">

**Author:** ![sfrazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sfrazer](https://www.ruby-forum.com/u/sfrazer)\
**Post date:** [October 15, 2013, 12:35am UTC](https://www.ruby-forum.com/t/cookie-and-source-ip-logic-in-server-block/236961/2 "2013-10-15T00:35:45Z")

</div>

On Mon, Oct 14, 2013 at 06:16:14PM -0400, sfrazer wrote:

Hi there,

untested, but…

> geo $remote\_addr $blocked {  
> default 0;  
> include /etc/nginx/conf/nginx-blocked-ips.conf;  
> }

map $blocked$cookie\_whatever $reallyblocked {  
default 0;  
1 1;  
}

If it is blocked by geo, and has no cookie\_whatever, then $reallyblocked  
is 1. If it has any value for cookie\_whatever, or $blocked is not 1,  
then $reallyblocked is 0.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![sfrazer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/bbce88/32.png) [@sfrazer](https://www.ruby-forum.com/u/sfrazer)\
**Post date:** [October 15, 2013, 10:40pm UTC](https://www.ruby-forum.com/t/cookie-and-source-ip-logic-in-server-block/236961/3 "2013-10-15T22:40:12Z")

</div>

Thanks! I wasn’t aware you could combine variables like that in a map  
statement. handy.

Posted at Nginx Forum:

> **[Re: cookie and source IP logic in server block](https://forum.nginx.org/read.php?2,243687,243736#msg-243736)**
>
> Hello, I'm trying to block certain IP ranges at my nginx server, but would like to offer the ability to bypass the block by completing a back-end CAPTCHA, which would set a cookie. Currently I set the block like so: geo $remote\_addr $blocked { defa
