Question

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?

Était-ce utile?

La solution

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;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top