http - 如何在 nginx 中添加规范 header 和 301 重定向

标签 http nginx header canonical-link

我正在尝试在 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/

相关文章:

java - 从网页响应读取字节时出现问题 (amf)

jquery - 当 json 字符串中有 & 登录时,http post 请求中的数据中断

nginx - 如何在 nginx 代理后面使用 Unix 域套接字托管 ASP.NET Core 2.0 (Kestrel)?

http - 404 header - HTTP 1.0 还是 1.1?

c++ - 如何发布带有模板实现的头文件?

ajax - 如何从缓存中清除 HTTP 304 响应?

html - C-HTTP网络服务器: how to cache

php - 使用 nginx 和 php7 无法加载任何 PHP 文件或在日志中看到任何错误

nginx - 指定--restart开关后,为什么我的Nginx docker 容器无法启动

html - 如何使带有 Logo 的导航栏居中?