# Help about limit\_req

**URL:** <https://www.ruby-forum.com/t/help-about-limit-req/221522>\
**Category:** NGINX\
**Created:** [July 18, 2012, 8:20am UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522 "2012-07-18T08:20:39Z")\
**Posts on this page:** 7\
**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:** [July 18, 2012, 8:20am UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/1 "2012-07-18T08:20:39Z")

</div>

Hi All,  
In the pass several days my server was under attack.  
Someone are using WebBench to test my website, and it takes up all my  
server CPU resources.  
So I’m wondering to use limit\_req to limit the request frequency.

I have 3 important php files – portal.php forum.php home.php, they are  
in the root directory of my website, the URL like below:  
[XXX Sex - Free Porn Videos at XXX.com](http://www.xxx.com/forum.php)  
[XXX Sex - Free Porn Videos at XXX.com](http://www.xxx.com/forum.php?)\*\*\*\*\*\*

And I also created below rules for URL rewrite in NGINX:  
rewrite ^([^.]_)/topic-(.+).html$  
$1/portal.php?mod=topic&topic=$2 last;  
rewrite ^([^.]_)/forum-(\w+)-([0-9]+).html$  
$1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;  
rewrite ^([^.]_)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$  
$1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;  
rewrite ^([^.]_)/group-([0-9]+)-([0-9]+).html$  
$1/forum.php?mod=group&fid=$2&page=$3 last;  
rewrite ^([^.]\*)/space-(username|uid)-(.+).html$  
$1/home.php?mod=space&$2=$3 last;

For now, the problem for me is, if I put below codes in NGINX, then my  
php file will stop execute.

```
location ~*^/(home|forum|portal).php$ {
          limit_conn addr 3;
            limit_req zone=refresh burst=3 nodelay;
                }

```

And if I put the limit ( limit\_req zone=refresh burst=3 nodelay;)  
into below sections. It works, but user reports that sometimes they  
can’t view the images.

location ~ .php$ {  
fastcgi\_pass unix:/tmp/nginx.socket;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME  
/scripts$fastcgi\_script\_name;  
include fastcgi\_params;  
}

```
Anyone can help me about this issue? I just want to set a limit for 

```

some specific php files?

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 18, 2012, 2:22pm UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/2 "2012-07-18T14:22:54Z")

</div>

> For now, the problem for me is, if I put below codes in NGINX, then my php file  
> will stop execute.

A quick fix would be just nest the locations or duplicate the fastcgi  
part. For example:

location ~\*^/(home|forum|portal).php$ {  
limit\_conn addr 3;  
limit\_req zone=refresh burst=3 nodelay;

```
      fastcgi_pass unix:/tmp/nginx.socket;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
      include fastcgi_params;

```

}

rr

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 18, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/3 "2012-07-18T18:33:00Z")

</div>

Thanks Reinis.

I tried to use the code you provided, I’ll get an error message ‘file  
not found’ (not 404)

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 19, 2012, 12:15am UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/4 "2012-07-19T00:15:58Z")

</div>

> I tried to use the code you provided, I’ll get an error message ‘file not  
> found’ (not 404)

Well I just copied over your existing config, to me this doesnt look  
right:

fastcgi\_param SCRIPT\_FILENAME /scripts$fastcgi\_script\_name;

I would suggest to replace it with:

fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;

rr

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 19, 2012, 5:27pm UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/5 "2012-07-19T17:27:53Z")

</div>

> But it seems that this rule is not working, could you please help?

Regular expression locations are matched in the order they are defined  
in the nginx config file, so you need to put that location  
above .

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

rr

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 19, 2012, 5:30pm UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/6 "2012-07-19T17:30:14Z")

</div>

Hi Reinis,

Thanks for your quickly response. But it still doesn’t work.  
Below is my configurations, could you please help to check?

```
location ~*^/forum.php?mod=image$ {
    root /web/www;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
location ~*^/(home|forum|portal).php$ {
    root /web/www;
    limit_conn addr 5;
    limit_req zone=refresh burst=5 nodelay;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
location ~ \.php$ {
    root /web/www;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
```

---

<div class="post-metadata">

**Author:** ![fhal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/ccd318/32.png) [@fhal](https://www.ruby-forum.com/u/fhal)\
**Post date:** [July 19, 2012, 5:05pm UTC](https://www.ruby-forum.com/t/help-about-limit-req/221522/7 "2012-07-19T17:05:41Z")

</div>

Hi Reinis,

One more question, for now, I’m using below code to limit the access of  
some URLs, such as  
/forum.php?mod=forumdisplay?\*\*\*\*\*  
/forum.php?mod=viewthread&\*\*\*\*

```
location ~*^/(home|forum|portal).php$ {
    root /web/www;
    limit_conn addr 5;
    limit_req zone=refresh burst=5 nodelay;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }
location ~ \.php$ {
    root /web/www;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }

```

But I don’t want to limit user access to below URL

/forum.php?mod=image&\*\*\*

So I added below section into Nginx

```
location ~*^/forum.php?mod=image$ {
    root /web/www;
    fastcgi_pass unix:/tmp/nginx.socket;
    fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    include fastcgi_params;
    }

```

But it seems that this rule is not working, could you please help?
