# Possible to limit\_req based on requests coming from a Class C (/24 subnet) instead of per IP (/32)?

**URL:** <https://www.ruby-forum.com/t/possible-to-limit-req-based-on-requests-coming-from-a-class-c-24-subnet-instead-of-per-ip-32/220371>\
**Category:** NGINX\
**Created:** [June 7, 2012, 8:16am UTC](https://www.ruby-forum.com/t/possible-to-limit-req-based-on-requests-coming-from-a-class-c-24-subnet-instead-of-per-ip-32/220371 "2012-06-07T08:16:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [June 7, 2012, 8:16am UTC](https://www.ruby-forum.com/t/possible-to-limit-req-based-on-requests-coming-from-a-class-c-24-subnet-instead-of-per-ip-32/220371/1 "2012-06-07T08:16:30Z")

</div>

Hello,

Is it Possible to limit\_req based on requests coming from a Class C (/24  
subnet) instead of per IP (/32) ? If so can anybody please provide an  
example.

Regards,  
Joseph

---

<div class="post-metadata">

**Author:** ![jchengj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/d78d45/32.png) [@jchengj](https://www.ruby-forum.com/u/jchengj)\
**Post date:** [June 7, 2012, 10:34am UTC](https://www.ruby-forum.com/t/possible-to-limit-req-based-on-requests-coming-from-a-class-c-24-subnet-instead-of-per-ip-32/220371/2 "2012-06-07T10:34:26Z")

</div>

On Thu, Jun 07, 2012 at 06:15:46AM +0000, Joseph C. wrote:

Hi there,

> Is it Possible to limit\_req based on requests coming from a Class C (/24 subnet)  
> instead of per IP (/32) ? If so can anybody please provide an example.

Totally untested, but:

Use exactly the same method as in the responses to your other limit\_req  
questions?

limit\_req\_zone ([Module ngx\_http\_limit\_req\_module](http://nginx.org/r/limit_req_zone)) using a new variable  
“$the\_class\_c”.

limit\_req ([Module ngx\_http\_limit\_req\_module](http://nginx.org/r/limit_req)) to do the limiting.

map ([Module ngx\_http\_map\_module](http://nginx.org/r/map)) to set the variable “$the\_class\_c” to  
empty, or to some identifier for the class C that should be limited.

Note that those docs for “map” don’t currently mention the “~ means  
regex  
match” or the “you can refer back to matched parts from the pattern,  
in the value”, which are shown on [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpMapModule),  
and which will likely be useful here.

In your map, you could test $remote\_addr for “everything up to the  
final .digits”; or possibly you could try taking “three bytes of  
$binary\_remote\_addr”.

Test it and see. Usually the debug log will include useful information  
about what nginx thinks is going on, in case it is unclear.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
