记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

重定向varnish前端用户的URL请求


Rewriting and redirecting URLs in Varnish

Rewriting an URL

You can easily rewrite the URL using the regsub() function in VCL. This takes place in vcl_recv.

if (req.http.host ~ "^(www\.)?example\.com" && req.url~ "^/images/") {
  set req.http.host = "images.example.com";
  set req.url = regsub(req.url, "^/images/", "/");
} 

This examples rewrites access to http://www.example.com/images/foo.jpg to http://images.example.com/foo.jpg. This is not visible to the user, but affects how the request to the backend(s) look. This does not affect which backend is used, so images.example.com is not actually resolved.

This is useful to avoid double caching of items available from different URIs. For example, to cache www.example.com and example.com identically, one could use:

set req.http.host = regsub(req.http.host, "^www\.example\.com___FCKpd___1quot;,"example.com");

Requests to www.example.com and example.com will all go to the backend as "example.com" and end up cached by that string.

By the way, regsub() can rewrite any header, really.

Redirection

Varnish does not have any built in support for HTTP redirections so redirection in Varnish can only be accomplished using something of a dirty hack. You trigger an specified error and then pick this error code up in the vcl_error subroutine. See this example for details.

转载请标明出处【重定向varnish前端用户的URL请求】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论