Search code examples
variablesif-statementnginxserver-name

Nginx $server_name in if statement


It seems like this is not working:

server_name blabla.bla;    
    location ~* (wp-comments-posts|wp-login)\.php$ {
            if ($http_referer !~ ^(http://$servername) ) {
            return 405;
            }
     }

While

server_name blabla.bla;    
    location ~* (wp-comments-posts|wp-login)\.php$ {
            if ($http_referer !~ ^(http://blabla.bla) ) {
            return 405;
            }
     }

works just fine. Is this expected and if so why? Or am I doing something wrong here?


Solution

  • Regular expressions are compiled while reading configuration, thus they cannot contain variables.

    Also please note: