在 Nginx 中,如何将 Googlebot 流量代理到 Varnish 后端,同时将所有其他流量定向到我的 PHP 后端?
我想要这样的东西:
try_files $uri $uri/ /index.php;
if ($http_user_agent ~* Googlebot) {
proxy_pass http://varnish;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
}
但是,上述内容在
server
上下文中不起作用,因为 proxy_pass
在 server
上下文中是不允许的。如果我将上述内容包装在
location / {}
块中,那么我在 if
块中有一个 location
,它使 if
evil 。 (我试过了,它有时奏效,但它是间歇性的,不可预测的。)有什么办法可以使这项工作?
最佳答案
您可以使用错误页面在内部将流量路由到服务器块中的其他位置,然后使用该位置块代理 Varnish 。
例子:
server {
error_page 519 = @google;
if ($http_user_agent ~* Googlebot) {
return 519;
}
location @google {
proxy_pass http://varnish;
}
关于php - Nginx:如何将Googlebot流量转移到Varnish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19433360/