{"id":2485,"date":"2013-01-25T22:05:45","date_gmt":"2013-01-25T22:05:45","guid":{"rendered":"http:\/\/blog.ed.gs\/?p=2485"},"modified":"2013-01-25T22:05:45","modified_gmt":"2013-01-25T22:05:45","slug":"nginx-multiple-if-statements","status":"publish","type":"post","link":"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/","title":{"rendered":"Nginx Multiple If Statements"},"content":{"rendered":"

Nginx doesn’t allow multiple if statements, so if you need to match multiple rules and you’re not checking if files exist then this is how you need to proceed.<\/p>\n

set $posting 0; # Make sure to declare it first to stop any warnings\n \nif ($request_method = POST) { # Check if request method is POST\n  set $posting N; # Initially set the $posting variable as N\n}\n \nif ($geoip_country_code ~ (BR|CN|KR|RU|UA) ) { # Here we're using the Nginx GeoIP module to block some spammy countries\n  set $posting "${posting}O"; # Set the $posting variable to itself plus the letter O\n}\n \nif ($posting = NO) { # We're looking if both of the above rules are set to spell NO\n  return 403; # If it is then let's block them!\n}<\/pre>\n

As you can see we have to use a variable, adding another character at each step and matching the final value with what we want to do.<\/p>\n","protected":false},"excerpt":{"rendered":"

Nginx doesn’t allow multiple if statements, so if you need to match multiple rules and you’re not checking if files exist then this is how you need to proceed. set $posting 0; # Make sure to declare it first to stop any warnings if ($request_method = POST) { # Check if request method is POST […]<\/p>\n","protected":false},"author":2,"featured_media":3497,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ep_exclude_from_search":false},"categories":[32,46],"tags":[],"yoast_head":"\nNginx Multiple If Statements - E<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nginx Multiple If Statements - E\" \/>\n<meta property=\"og:description\" content=\"Nginx doesn’t allow multiple if statements, so if you need to match multiple rules and you’re not checking if files exist then this is how you need to proceed. set $posting 0; # Make sure to declare it first to stop any warnings if ($request_method = POST) { # Check if request method is POST […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/\" \/>\n<meta property=\"og:site_name\" content=\"E\" \/>\n<meta property=\"article:published_time\" content=\"2013-01-25T22:05:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/static.ed.gs\/wp-content\/uploads\/2013\/01\/168H.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2048\" \/>\n\t<meta property=\"og:image:height\" content=\"1365\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ed\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ed\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/\",\"url\":\"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/\",\"name\":\"Nginx Multiple If Statements - E\",\"isPartOf\":{\"@id\":\"https:\/\/ed.gs\/#website\"},\"datePublished\":\"2013-01-25T22:05:45+00:00\",\"dateModified\":\"2013-01-25T22:05:45+00:00\",\"author\":{\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ed.gs\/#website\",\"url\":\"https:\/\/ed.gs\/\",\"name\":\"E\",\"description\":\"automation, consultancy, project management, web\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ed.gs\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628\",\"name\":\"Ed\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/ed.gs\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g\",\"caption\":\"Ed\"},\"url\":\"https:\/\/ed.gs\/author\/ed\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nginx Multiple If Statements - E","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/","og_locale":"en_GB","og_type":"article","og_title":"Nginx Multiple If Statements - E","og_description":"Nginx doesn’t allow multiple if statements, so if you need to match multiple rules and you’re not checking if files exist then this is how you need to proceed. set $posting 0; # Make sure to declare it first to stop any warnings if ($request_method = POST) { # Check if request method is POST […]","og_url":"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/","og_site_name":"E","article_published_time":"2013-01-25T22:05:45+00:00","og_image":[{"width":2048,"height":1365,"url":"https:\/\/static.ed.gs\/wp-content\/uploads\/2013\/01\/168H.jpg","type":"image\/jpeg"}],"author":"Ed","twitter_misc":{"Written by":"Ed","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/","url":"https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/","name":"Nginx Multiple If Statements - E","isPartOf":{"@id":"https:\/\/ed.gs\/#website"},"datePublished":"2013-01-25T22:05:45+00:00","dateModified":"2013-01-25T22:05:45+00:00","author":{"@id":"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ed.gs\/2013\/01\/25\/nginx-multiple-if-statements\/"]}]},{"@type":"WebSite","@id":"https:\/\/ed.gs\/#website","url":"https:\/\/ed.gs\/","name":"E","description":"automation, consultancy, project management, web","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ed.gs\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/ed.gs\/#\/schema\/person\/d775615f2296ad0129fa3ea66346c628","name":"Ed","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/ed.gs\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fe1dfaed09e6bdceb557d3008f5cc47?s=96&d=mm&r=g","caption":"Ed"},"url":"https:\/\/ed.gs\/author\/ed\/"}]}},"_links":{"self":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts\/2485"}],"collection":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/comments?post=2485"}],"version-history":[{"count":0,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/posts\/2485\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/media\/3497"}],"wp:attachment":[{"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/media?parent=2485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/categories?post=2485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ed.gs\/wp-json\/wp\/v2\/tags?post=2485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}