Nginx $http_cookie if directive pattern matching
<p>So on the Nginx documentation it is a bit unclear but it appears we can use Regex to match anything within the if statement</p>

<p><a href="http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if" rel="nofollow">http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#if</a></p>

<p>So i wanted to tweak this to make it so Nginx checks the value of any cookie.</p>

<p>But my following string what should be from my understanding * matching any cookie name and = matching any cookie contents.</p>

<pre><code>if ($http_cookie ~* "*=*") {
return 444;
}
</code></pre>

<p>But i get the error "pcre_compile() failed: nothing to repeat in "<em>" at "</em>" in"</p>

<p>What i am trying to achieve is to Have nginx check the cookie like a WAF (web application firewall) to make sure it only contains A-Z uppercase a-z lowercase 0-9 + - _ . my PHP app is Joomla what does these checks too but be useful if i could perform these checks with Nginx too since it could deny the request faster.</p>

<p>EDIT to Show half solved issue / dilemma</p>

<pre><code>set $block_cookie_exploits 0;
#If cookie name or contents does not contain the following
if ($http_cookie !~ "[a-zA-Z0-9\+\-\_]=[a-zA-Z0-9\+\-\_]") {
set $block_cookie_exploits 1;
}
#Block the client request
if ($block_cookie_exploits = 1) {
return 403;
} 
</code></pre>

<p>New problem with above configuration is it will return 403 while no cookie is present. And if you put characters in the name or contents of the cookie like {} it does not return 403</p>