gpt4 book ai didi

vue.js - nginx - 将 facebook/bots 代理到不同的服务器而不更改规范 URL

转载 作者:行者123 更新时间:2023-12-04 16:32:27 26 4
gpt4 key购买 nike

TLDR;
我怎样才能让所有爬虫/机器人请求到达我的前端 https://frontend.example.test/any/path/here来自 https://backend.example.test/prerender/any/path/here 的数据不更改规范 URL?


我有一个复杂的情况,我有一个 Vue 应用程序,它从 php API 中提取数据来呈现数据。这些都是在中国托管的,所以像 netlify prerender 和 prerender.io 这样的细节不是一个选择。

最初我尝试过:

if ($http_user_agent ~* "googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp") {
rewrite ^/(.*)$ https://backend.example.test/prerender/$1 redirect;
}

哪个有效,但 Facebook 使用了 backend.example.text规范 URL frontend.example.test .

设置 og:url由于重定向循环,到前端应用程序导致问题。然后我尝试设置 og:url使用跳过 nginx 向前的查询参数到前端,但由于某种原因,这在实时服务器上无法正常工作,我想 facebook 无论如何最终仍会从最终 url 中提取数据。

因此我想唯一的解决方案是使用 proxy_pass但不允许在 if 中使用 URI声明(我已经阅读了 if is evil article )。

我觉得我所需要的只是以下功能的功能版本:
location / {
if ($http_user_agent ~* "googlebot|bingbot|yandex|baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest\/0\.|pinterestbot|slackbot|vkShare|W3C_Validator|whatsapp") {
proxy_pass https://backend.example.test/prerender;
}
...
}

(I am of course aware of the contradiction of having to have Facebook sharing work in China, but the client is requesting this for their international users as well).

最佳答案

这是您的问题的解决方案:
https://www.claudiokuenzler.com/blog/934/nginx-proper-way-different-upstream-user-agent-based-reverse-proxying-rule-without-if
我在此处复制主要部分,以防链接断开:
使用 map 指令在上游创建一个动态目标:

map "$http_user_agent" $targetupstream {
default http://127.0.0.1:8080;
"~^mybot" http://127.0.0.1:8090;
}
这里的“~^mybot”是一个正则表达式,如果用户代理匹配该表达式,它将使用该上游服务器。
如果用户代理不匹配任何条目,Nginx 将使用“默认”条目(将 http://127.0.0.1:8080 保存为 $targetupstream 变量)。
然后你只需要在代理传递设置中使用上游:
  location / {
include /etc/nginx/proxy.conf;
proxy_set_header X-Forwarded-Proto https;
proxy_pass $targetupstream;
}
现在,您可以使用一个上游指向 locahost 的端口,该端口正被 nginx 用于提供静态文件(仅适用于客户端)和另一个端口用于服务器渲染器。

关于vue.js - nginx - 将 facebook/bots 代理到不同的服务器而不更改规范 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62471702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com