更正 nginx 配置以防止索引某些文件夹

Evg*_*niy 5 googlebot nginx

我使用以下Nginx配置来防止在使用x-robots tag

location ~ .*/(?:archive|filter|topic)/.* {
    add_header X-Robots-Tag "noindex, follow";      
}
Run Code Online (Sandbox Code Playgroud)

内容保持索引,但我无法调试Nginx配置。

我的问题:我使用的配置是否正确,我是否应该等到 googlebot 重新抓取内容并对内容取消索引?还是我的配置有问题?

All*_*uce 6

你写的配置是正确的。我会给出一个警告(假设您的配置是其他标准的):

仅当结果码为 200、201、204、206、301、302、303、304 或 307(例如内容匹配磁盘文件、发出重定向等)时,才会输出 X-Robots-Tag。因此,如果您有一个/archive/index.html,点击 tohttp://yoursite.com/archive/将给出标题。如果index.html不存在 (404),您将看不到标签。

always假设处理了位置块,该参数将输出所有响应代码的标头:

location ~ .*/(?:archive|filter|topic)/.* {
    add_header X-Robots-Tag "noindex, follow" always;      
}
Run Code Online (Sandbox Code Playgroud)

另一个选项将保证在 URI 匹配上输出标头。这对于有可能无法处理位置块(由于短路,例如return或 a laston a rewrite 等)时很有用:

http {
    ...
    map $request_uri $robot_header {
        default "";
        ~.*/(?:archive|filter|topic)/.* "noindex, follow";
    }

    server {
        ...
        add_header X-Robots-Tag $robot_header;
        ...
    }
Run Code Online (Sandbox Code Playgroud)