我正在尝试在 nginx 中的一个位置上添加一个规范 header ,该位置已经返回 301。我正在向目标 URL 添加一个规范 header ,因为规范链接对用户来说比 Google 中显示的实际 URL 更重要.我在检查开发工具时看不到规范的响应 header ?
当前nginx路由设置为
location ~* '^/folder/another-folder/important-document.pdf' {
return 301 $scheme://$host/docs/destination-url;
}
我已经尝试添加规范 URL,如下所示:
location ~* '^/folder/another-folder/important-document.pdf' {
return 301 $scheme://$host/docs/destination-url;
add_header Link "<$scheme://$http_host/folder/another-folder/url>; rel=\"canonical\"";
}
第二个示例已经投入生产大约 2 周,我仍然看到 .PDF 显示在 Google 有机搜索结果中,而开发工具中没有响应 header 。
最佳答案
与 URI 匹配的第一个正则表达式位置将处理该请求,因此如果您在该位置上方有一个不明确的位置 block ,它将阻止您的规则被采纳。参见 this document了解详情。
要么将 location
block 移动到 server
block 中更高的位置,以便首先遇到它。
如果您尝试匹配单个 URI,请使用完全匹配位置,无论其在 server
block 中的位置如何,它始终具有最高优先级。例如:
location = /folder/another-folder/important-document.pdf { ... }
关于http - 如何在 nginx 中添加规范 header 和 301 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55896846/