Q: How to prevent caching ajax by Varnish? such a problem, I need to receive some useful data via ajax, but varnish caches it all the time. So I need some solution to stop varnish caching ajax.

A: Varnish will only cache content as long as the backend server asks it to. If the backend server doesn't send anything like that, the default is to cache for 120 seconds.
The correct way of doing this is to modify the backend application to send a Cache-Control: max-age=0 response header.
It should also be noted that even caching for a second (max-age=1) will, on sites with a lot of concurrent users, give good results.

A: If you can identify your AJAX requests based on the URL, you can easily pipe all of them to the backend with the following VCL:
sub vcl_recv {
  if (req.url ~ "(ajax\.php|dynamic\.php)") {
    return(pipe);
  }
}

This would pipe all requests where the URL contains either ajax.php or dynamic.php.
