简体   繁体   中英

Nginx: How to divert Googlebot traffic to Varnish

In Nginx, how can I proxy Googlebot traffic to a Varnish backend, while keeping all other traffic directed at my PHP backend?

I'd like something like this:

try_files $uri $uri/ /index.php;
if ($http_user_agent ~* Googlebot) {
    proxy_pass http://varnish;
}
location ~* \.php$ {
    fastcgi_pass 127.0.0.1:9000;
}

However, the above doesn't work in server context because proxy_pass is not allowed in a server context.

If I wrap the above in a location / {} block, then I have an if inside a location block which makes the if evil . (I tried it and it worked sometimes, but it was intermittent and unpredictable.)

Any way to make this work?

You could use error pages to internally route traffic to a different location in the server block, and then use that location block to proxy to varnish.

example:

server {
    error_page 519 = @google;
    if ($http_user_agent ~* Googlebot) {
        return 519;
}
    location @google {
         proxy_pass http://varnish;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM