nginx - 返回后继续解析

时间:2013-03-05 10:01:15

标签: php nginx

我试图拒绝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处理程序?

1 个答案:

答案 0 :(得分: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以阻止。