I have a service being accessed through nginx and I wish to white list post requests only. I have written this in my nginx config file:
location / {
if ( $request_method ~ ^(POST|PUT)$ ) {
allow 127.0.0.1;
}
if ( $request_method !~ ^(GET|POST|PUT|HEAD)$ ) {
return 405;
}
}
This config gives me the following error -
nginx: [emerg] "allow" directive is not allowed here
On the other hand, if I write the allow directive out of the if block like this, it works.
location / {
allow 127.0.0.1;
if ( $request_method !~ ^(GET|POST|PUT|HEAD)$ ) {
return 405;
}
}
I believe this means that I can't use the allow directive in an if block. Am I doing something wrong here? If not, is there a workaround for achieving this?
return_405, it always raisesinvalid variable name "return_405". I've tried to change the variable's name to all alphabets likefooand ones without special characters likereturn405, but none of them seems to work. Can you tell me where the problem lies? – Keshav Agarwal Mar 02 '17 at 05:13mapdirective should have$prefix. I fixed the answer – Selivanov Pavel Mar 02 '17 at 09:35localhost:8000and127.0.0.1:8000using postman. – Keshav Agarwal Mar 02 '17 at 12:42return 405to something else, likereturn 418, and you will see if 405 is returned by nginx or by underlying code. – Selivanov Pavel Mar 02 '17 at 13:24