Skip to content

Instantly share code, notes, and snippets.

@adamdilek
Forked from Coopeh/nginx-multiple-if.conf
Created October 3, 2016 21:23
Show Gist options
  • Save adamdilek/5f06e6c6bd07e5776b4ca6a30b9265bc to your computer and use it in GitHub Desktop.
Save adamdilek/5f06e6c6bd07e5776b4ca6a30b9265bc to your computer and use it in GitHub Desktop.
How to use multiple if statements in Nginx
set $posting 0; # Make sure to declare it first to stop any warnings
if ($request_method = POST) { # Check if request method is POST
set $posting N; # Initially set the $posting variable as N
}
if ($geoip_country_code ~ (BR|CN|KR|RU|UA) ) { # Here we're using the Nginx GeoIP module to block some spammy countries
set $posting "${posting}O"; # Set the $posting variable to itself plus the letter O
}
if ($posting = NO) { # We're looking if both of the above rules are set to spell NO
return 403; # If it is then let's block them!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment