0
votes

I'm creating simple page where will a lot of pictures. All pictures are hosted on remote provider (hosted on object storage and I have only links to all pictures) To speed up www I would like to use varnish to cache this pictures but I have problem:
All pictures are served with https, so I've used haproxy to terminate ssl and next traffic go to varnish, but how to map in varnish website address that should be visible for end user like https://www.website.com/picture.jpg with remote address where is picture hosted(https://www.remotehostedpictures.com/picture.jpg) . So, in final result user must see first link, remote address remotehostedpictures.com/picture.jpg can't be visible.

2
First, please don't use other people's domain names as placeholders. example.com, example.net, and example.org are specifically assigned for this purpose. Second, what is the problem you are having? I ask, because what you are describing as the desired behavior is how HAProxy and Varnish works, anyway. That's the expected behavior. If you have example.com → HAProxy → Varnish → example.net (content source), the links and the address would be example.com. - Michael - sqlbot
I know how to do it with use haproxy (proxy pass) but I don't know how in varnish. Maybe somebody could share some simple config example in varnish? - stefek143

2 Answers

0
votes

In your varnish vcl_recv you should change your request host header, then you must declare remotehostedpictures.com as your backend.

In the end, you should have something like this (code not tested)

sub vcl_recv {
  if (req.url ~ "\.jpg") {
    set req.http.host = "www.remotehostedpictures.com";
    set req.backend_hint = remote_host;
  }
}

backend remote_host {
  .host = "www.remotehostedpictures.com";
  .port = "80";
}

By the way, beware of dns in backend.host. If the dns resolved to multiple IPs varnish will use the first one. The dns resolving is done at vcl compile time so if the dns change you should reload your vcl.

0
votes

I think that storing images in Varnish is not god idea, because than Varnish will fill whole ram quickly (if You have lot of images), than when Varnish is full it purges whole cache, imagine what is happening on the server when whole cache is purged and You have traffic on Your page.

Some time ago I make such cache in Varnish and after few hours live I have to disable caching images because of purging (for me most important was caching page content).

In such situations best solution is CDN. You can use external service such as Cloudflare, or make simple CDN on Nginx (which will only serve static files with expire header).

Hope it helps :)