How to set limit_req on a certain folders?

Orlo asked:

How can I set limit_req on all folders except on a “uploads and public” for example.

What I’ve tried:

    location / {
        root /var/www/html/public;
                            if ( $uri !~ ^/(uploads|public) ) {
                                limit_req zone=one burst=5 nodelay;
                            }
...

error:

nginx: [emerg] "limit_req" directive is not allowed here in /etc/nginx/sites-enabled/my.conf:20

My answer:


limit_req is not allowed inside if as you have it here. It’s only allowed in http, server or location.

So you will need to have location blocks for each of these.

location /public/ {
    limit_req zone=one burst=5 nodelay;
}
location /uploads/ {
    limit_req zone=one burst=5 nodelay;
}

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.