1

I have lots directories which have the same image "_a.png",like these:

/data/images/{a,b,c,d}

Because this, the varnish cache multiple copies. Now I want to rewrite the urls to one,like this:

if (req.url ~ "/_pty.png$") {
set req.url="/images/a/_pty.png";
}

so I reload varnish but it not work in right! eg. I get the url, http://xx.com/images/c/_pty.png ,with varnishlog I find it's no right,the code above no effect! how to set?

Sola.Shawn
  • 15
  • 4

1 Answers1

0

You need to rewrite the URL like this:

if (req.url ~ "/_pty.png$") {
  set req.url = regsub(req.url, "^/images/(.+)/_pty.png$", "/images/a/_pty.png");
}

This should work. Fixed as requested in the comment.

Napster_X
  • 3,373
  • 18
  • 20
  • hi,maybe i don't Speak clearly,I mean the urls like this: **bold** `http://xx.com/images/c/_pty.png or http://xx.com/images/b/_pty.png when i get them ,the url will rewrite to http://xx.com/images/a/_pty.png ` by the way way "set req.url=" don't effect! any differents? – Sola.Shawn Dec 28 '12 at 09:56
  • ok. Fixed my answer. This should work. – Napster_X Dec 28 '12 at 10:01
  • Thanks . But I had done it `if (req.request == "GET" && req.url ~ "\.(jpg|png|gif|swf|jpeg|ico)$") { unset req.http.cookie; #清除url中jpg等文件cookie if (req.url ~ "/_empty.png$") { #set req.url="/images/mlogo/sze/_empty.png"; set req.url=regsub(req.url,"^/images/mlogo/(.+)/_empty.png$","/images/mlogo/sze/_empty.png"); } }` and it's also wrong – Sola.Shawn Dec 28 '12 at 10:34
  • Are you sure that your URL is `http://ip_address/images/mlogo/sze/_empty.png` ? – Napster_X Dec 28 '12 at 11:17
  • Or there may be few other directories in between this ? – Napster_X Dec 28 '12 at 11:18
  • yes,I am sure the URL is `http://ip_address/images/mlogo/sze/_empty.png ` ,and there is no other directories in between them ,it like this varnish(192.168.1.10:80)-->nginx(192.168.1.11:80) – Sola.Shawn Dec 29 '12 at 00:31
  • **`12 VCL_call c recv lookup 12 VCL_call c hash 12 Hash c /images/mlogo/dw/_empty.png 12 Hash c images.xx.com 12 VCL_return c hash 12 VCL_call c miss fetch 12 Backend c 13 www www 12 TTL c 695673534 RFC 259200 -1 -1 1356743465 0 1356743464 1357002664 259200 12 VCL_call c fetch 12 TTL c 695673534 VCL 86400 -1 -1 1356743465 -0 12 VCL_return c deliver 12 ObjProtocol c HTTP/1.1`** this is the log – Sola.Shawn Dec 29 '12 at 01:12
  • Oh ... you are also changing the ip_address ... I thought just the URL ... is it true ? – Napster_X Dec 29 '12 at 04:33
  • yes,the ip_address i had change when i test! it like this images.xx.com – Sola.Shawn Dec 29 '12 at 04:40
  • So, there are two things, one is URL rewrite, which we are doing above and other is host rewrite. Can you please be clear what all you are looking for and please explain completely. Most of the things you are adding later. Please update the question with complete details and let me know once it's done. – Napster_X Dec 29 '12 at 04:42
  • Hi, GeekRide,sorry for replies late.I got it,the reason is because I put the code : `if (req.url ~ "\.(jpe?g|png|gif|cio)$") { unset req.http.cookie; return (lookup);}` before the code ,`if (req.url ~ "/_pty.png$") { set req.url = regsub(req.url, "^/images/(.+)/_pty.png$", "/images/a/_pty.png"); }` ,so I delete it,and it work fine now! thanks. By the way ,can i use code like this ? `if (req.url ~ "/_pty.png$") { set req.url="/images/a/_pty.png"; }` to redirect the URL?.-:) – Sola.Shawn Dec 30 '12 at 02:42
  • I am not sure Sola, as I haven't tried this, but I don't see a reason for this not to work. You can give it a try, and hopefully this should work. – Napster_X Dec 30 '12 at 17:19