wordpress - Apache 2.4 要求 ip 不工作

标签 wordpress apache .htaccess

试图从旧的允许、拒绝、命令语法转到新的语法以保护 WordPress 管理部分,但我无法让它识别我的 IP。

这是我的 .htaccess 文件包含在 /wp-admin 文件夹中的内容。

ErrorDocument 401 default
ErrorDocument 403 default

# Disallow access for everyone except these IPs
<RequireAny>
    Require ip 50.153.218.4
</RequireAny>

# Allow plugin access to admin-ajax.php around password protection
<Files admin-ajax.php>
    <RequireAll>
        Require all granted
    </RequireAll>
</Files>

这就是我在 .htaccess 中的 WordPress mod rewrite info 根目录下的内容。

# Protect WordPress
ErrorDocument 401 default
ErrorDocument 403 default

<Files wp-login.php>
    <RequireAny>
        Require ip 50.153.218.4
    </RequireAny>
</Files>

但我只是不断收到 403 Forbidden 错误。当我在 IP 下添加 Require All Granted 时,它工作正常,但它向每个用户开放。似乎 apache 只是没有正确读取我的 ip?有人知道我做错了什么吗?

最佳答案

你的语法对我来说看起来非常好。

我认为 apache 可能无法正确读取用户 IP 的唯一原因是您使用了代理或负载平衡器。如果是这种情况,您将使用 X-Forwarded-For 而不是 ip。在 PHP 中,您可以通过比较 $_SERVER['REMOTE_ADDR']$_SERVER['HTTP_X_FORWARDED_FOR'] 来确认您是否在代理后面。

如果这不是问题,那么您可能会更幸运地在 ServerFault 找到答案.

不过,我可以为您提供一些解决方法。最简单的解决方案可能是使用几个 WordPress security plugins 之一允许您通过 IP 地址限制对后端的访问。

或者,在您的主题或插件中,您可以实现同样的身份验证逻辑:

add_action('init', function() {
    $allowed_ips = array('50.153.218.4');
    if(is_admin() || $GLOBALS['pagenow'] == 'wp-login.php') {
        if( !DOING_AJAX && !in_array($_SERVER['REMOTE_ADDR'], $allowed_ips) ) {
            wp_die('', 'Forbidden' array(
                'response' => 403
            ));
        }
    }
});

更新:从评论看来,涉及代理。这应该工作:

ErrorDocument 401 default
ErrorDocument 403 default

SetEnvIF X-Forwarded-For "50.153.218.4" AllowIP

# Disallow access for everyone except these IPs
<RequireAny>
    Require env AllowIP
</RequireAny>

# Allow plugin access to admin-ajax.php around password protection
<Files admin-ajax.php>
    <RequireAll>
        Require all granted
    </RequireAll>
</Files>

# Protect WordPress
ErrorDocument 401 default
ErrorDocument 403 default

SetEnvIF X-Forwarded-For "50.153.218.4" AllowIP

<Files wp-login.php>
    <RequireAny>
         Require env AllowIP
    </RequireAny>
</Files>

您还应该能够使用“允许,拒绝”语法使用类似的方法。

关于wordpress - Apache 2.4 要求 ip 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29356187/

相关文章:

mysql - WordPress JOIN wp_post 和 wp_postmeta

php - 如何真正快速地使用 PHP 在服务器上复制 2000 张图像?

apache - .htaccess 域从子文件夹打开然后 rewriterule

.htaccess - 在 RewriteRule 中匹配 HTTP_HOST 而不是使用 RewriteCond

php - 如何使用购物车总数来更改 WooCommerce 中的购物车商品价格

mysql - 在使用 MySQL 计算的同一查询中按距离过滤?

php - CentOS 7 - 更新默认存储库中未找到的软件包

perl - 在没有 CGI 的情况下运行像 php 这样的 perl 脚本

apache - htaccess帮助,需要强制www,https并删除index.php

javascript - Ajax 调用 Php 脚本以在本地主机上工作但不能从托管服务器上获取 Base64 字符串中的图像