title: Nginx -- if referer, add (x-forwarded-for) ip to blacklist
body: <p>I have written a basic http referer redirector that looks something like this:</p>
  
  <pre><code>if ($http_referer ~* (google|yahoo|bing|duckduckgo)) {  return 301 https://altavista.com; }
  </code></pre>
  
  <p>My goal is something like this</p>
  
  <pre><code>if ($http_referer ~* (google|yahoo|bing|duckduckgo)) {
      add $x-forwarded-for bad_ips.txt; &lt;-- this line is the question (x-forwarded-for because it's behind cloudflare)
      return 301 https://altavista.com; 
  }
  if ($bad_ip) { 
      return 301 https://altavista.com; 
  }
  </code></pre>
  
  <p>Is there any way to do this?  The goal is essentially if you are referred from a website on the blacklist, I would like to add your ip to the redirect list.</p>
  
  <p>Note: This does not need to be done purely in nginx; using other technologies in addition to nginx is fine so long as they are free. </p>
  
  <p>Please note my site is utilizing cloudflare so rather than banning a simple IP it has to be using the x-forwarded-for address or I'm just banning cloudflare.</p>
  
  <p>Thanks</p>