Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Variables if语句中的Nginx$server\u名称_Variables_If Statement_Nginx_Server Name - Fatal编程技术网

Variables if语句中的Nginx$server\u名称

Variables if语句中的Nginx$server\u名称,variables,if-statement,nginx,server-name,Variables,If Statement,Nginx,Server Name,这似乎不起作用: server_name blabla.bla; location ~* (wp-comments-posts|wp-login)\.php$ { if ($http_referer !~ ^(http://$servername) ) { return 405; } } 当 很好用。这是预期的吗?如果是,为什么?还是我在这里做错了什么?正则表达式是在读取配置时编译的,因此它们不能

这似乎不起作用:

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


很好用。这是预期的吗?如果是,为什么?还是我在这里做错了什么?

正则表达式是在读取配置时编译的,因此它们不能包含变量

另请注意:


如果您有您可能喜欢的referer模块,这将只允许当前服务器名称为有效的referer。所有其他将作为405错误返回

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

            ### Do your stuff here
              
        }

谢谢我知道“如果”是邪恶的,但让php执行所有暴力破解密码的尝试更邪恶(性能方面)。我的评论更多的是关于你使用
$http\u referer
而不是特殊的
$invalid\u referer
。掌心感谢!又来了@greg如果防止暴力攻击是您的目标,我建议您使用将其移动到TCP层。
        location ~* (wp-comments-post)\.php$ {
            valid_referers server_names;
            
            if ( $invalid_referer ) {
                    return 405;
            }

            ### Do your stuff here
              
        }