« Back to Index

[Fastly to S3 with Query Params]

View original Gist on GitHub

Tags: #fastly #vcl #varnish

Fastly to S3 with Query Params.vcl

sub filter_qs_by_whitelist {
  # Clean up the query string:
  #
  #  - Sort query params by name
  #  - Remove unnecessary query params (i.e. keep only those that match regex pattern)
  #
  # See helper functions documentation:
  # https://docs.fastly.com/guides/vcl/query-string-manipulation-vcl-features
  #
  # tested:
  # https://fiddle.fastlydemo.net/fiddle/d19f1e72
  set req.url = querystring.sort(req.url);
  set req.url = querystring.regfilter_except(req.url, "^(adops_giraffe|bids|country|cs|ct|ids|language|network|or|p|page|page_quantity|page_size|s|sub|u|uo|user_id|wid)$");
}

sub plan_z_recv {
    ...
    
    # S3 doesn't understand Query Params
    # So we need to encode any params into the url itself
    #
    # e.g. /news?id=foo -> /news%3Fid%3Dfoo
    #
    if (req.url.qs != "") {
      call filter_qs_by_whitelist;
      set req.http.X-URL-Original = req.url;
      set req.url = req.url.path + urlencode("?" + req.url.qs);
      set req.http.X-URL-Modified = req.url;
    }
    
    ...
}