简体   繁体   English

if 语句中的 Nginx $server_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?或者我在这里做错了什么?

Regular expressions are compiled while reading configuration, thus they cannot contain variables.正则表达式是在读取配置时编译的,因此它们不能包含变量。

Also please note:另请注意:

If you have the referer module you might like this one, this will ONLY allow the current server names to be valid referrers.如果你有引用模块,你可能会喜欢这个模块,这将只允许当前的服务器名称是有效的引用。 All others will return as 405 error.所有其他人将返回 405 错误。

        location ~* (wp-comments-post)\.php$ {
            valid_referers server_names;
            
            if ( $invalid_referer ) {
                    return 405;
            }

            ### Do your stuff here
              
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM