0

我试图拒绝 bingbot 访问 /wp-admin 和 /wp-login.php,但我的 nginx 配置有问题。我从以下内容开始:

location ~ /(wp-admin|wp-login\.php) {
    if ($http_user_agent ~* "(alltheweb|baidu|bingbot|googlebot|msnbot|slurp)") {
        return 403;
        break;
    }
}

# Allow any php files to run
location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/lib/php/php.socket;
}

这对于阻止 bingbot 非常有效,但它也不会执行wp-login.php;它把它当作一个静态文件。我想“好吧,你不会陷入更多的位置块”,所以我将 php 代码复制到更高的代码中:

location ~ /(wp-admin|wp-login\.php) {
    if ($http_user_agent ~* "(alltheweb|baidu|bingbot|googlebot|msnbot|slurp)") {
        return 403;
        break;
    }
    location ~ \.php$ {
        try_files $uri =404;
        include /etc/nginx/fastcgi.conf;
        fastcgi_pass unix:/var/lib/php/php.socket;
    }
}

# Allow any php files to run
location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi.conf;
    fastcgi_pass unix:/var/lib/php/php.socket;
}

这使我能够再次登录而不是下载wp-login.php,但现在 bingbot 再次获得 HTTP 200 OK!如何有选择地正确返回 403 或 php 处理程序?

4

1 回答 1

1

你可以试试

if ($http_user_agent ~* "(alltheweb|baidu|bingbot|googlebot|msnbot|slurp)") {
    location ~ /(wp-admin|wp-login\.php) {
        return 403;
    }
}

但是,您可能需要设置如下内容:

location /spider_blocked {
    return 403;
}

if ($http_user_agent ~* "(alltheweb|baidu|bingbot|googlebot|msnbot|slurp)") {
    rewrite ^/(wp-admin|wp-login\.php) /spider_blocked redirect;
}

基本上,您所做的是为 nginx 设置一个特殊的 catch URL,并对特定用户代理访问的特定 url 进行重定向,并将其重定向到特殊的 catch URL 以被阻止。

于 2013-03-05T16:11:05.177 回答