A
A
Andrey2019-01-07 09:33:53
Nginx
Andrey, 2019-01-07 09:33:53

How to compose a rule for NGIX for a page with a parameter?

There is a following task - there is a site on Wordpress with the included cache. Someone is constantly hammering at him on the heaviest page with a request like /slow-page?workaround=time(). Since there is no page in the cache, a new one is generated, which heavily loads the server. I added this rule to

location / {
    if ( $args ~ "workaround=" ) { return 404; }
}

But this rule is not quite universal, since a person can change the parameter and I will have to change it. I want to do otherwise - give a 404 error if there is a request with any request in the query_string, except for a search request like /?s=*, since my search is implemented through Elastic by MySQL. That is, in fact, I need to understand that a person has entered the main page (to avoid constructions like /page/?s=*) and he has the s= parameter in his query_string.
I don't quite understand how to implement this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-01-07
@dodo512

if ($request_uri ~ "^/(?!\?s=).*\?") {
    return 404;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question