4

Is it possible to allow only a single IP address to make POST requests and deny everyone else on the same grounds on NGINX?

Seen an answer related to this one but the solution offered there needs the rule to be applied server-wide. I need to do this on a virtual host level, without affecting the other sites hosted on the same server.

Ali
  • 43
  • 1
  • 4

2 Answers2

1

Under your virtual host directory , create an .htaccess file.

You can use rules like this for deny all post requests:

# deny all POST requests
<IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_METHOD} POST
    RewriteRule .* - [F,L]
</IfModule>

Then allow the whitelist:

# whitelist POST requests
<IfModule mod_rewrite.c>
     RewriteCond %{REQUEST_METHOD} POST
     RewriteCond %{REQUEST_URI} !/contact.php [NC]
     RewriteCond %{REMOTE_ADDR} !127.0.0.1 
     RewriteRule .* - [F,L]
 </IfModule>

More information on this site

EDIT : for NGINX you can traslate precedent rule with this online tool, for instance:

if ($request_method ~ "POST"){
    set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
    return 403;
    break;
}


if ($request_method ~ "POST"){
    set $rule_0 1$rule_0;
}
if ($remote_addr !~ "127.0.0.1"){
    set $rule_0 3$rule_0;
}
if ($rule_0 = "321"){
   return 403;
    break;
}
  • Question is specifically asking for Nginx, not Apache. – parkamark Mar 24 '17 at 10:17
  • pardon, i'll edit the post with converted info for ngnix – Daniele Licitra Mar 24 '17 at 11:08
  • 1
    OK, I have modified yours a bit and it seems like working! Thanks a lot @DanieleLicitra :) `if ($request_method ~ "POST"){ set $poster P$poster; } if ($remote_addr !~ "111.222.333.444"){ set $poster B$poster; } if ($poster = "BP"){ return 444; }` – Ali Mar 24 '17 at 12:22
0

The answer you need is similar to this one.

You would additional need a check for post, which would be ($request_method = POST ).

So the block would look something like the following (which I have not been able to test):

location @location {
  if ($request_method = POST) {
     allow IP;
     deny all;
  }
}

Docs for the access module are here.

iwaseatenbyagrue
  • 3,688
  • 15
  • 24