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?