Question

Having nginx serving static files with url like http://foobar.tld/<random_dir>/<file_md5sum_as_filename> and I would like if I specify at the end of url ?f="filename.filetype" - nginx parse that query string at the end (if is specified) and prepares new content dispose matching specified arguments and without any dynamic backend that's like impossible.

or instance http://foobar.tld/<random_dir>/<file_md5sum_as_filename>?f="foobar.pdf"

Can something like this be done with nginx/lua module? Does any one have any useful example or has done anything similar?

Was it helpful?

Solution 2

This is indeed possible with nginx-lua; in particular, the header_filter_by_lua directive.

Something like the following ought to do the trick:

location / {
  header_filter_by_lua '
    local args = ngx.req.get_uri_args()
    if not args.f then return end

    ngx.header["Content-Disposition"] = "attachment; filename=" .. args.f
  ';
}

OTHER TIPS

Pure Nginx configuration

location / {
  if ($arg_f) {
    add_header Content-Disposition "attachment; filename=$arg_f";
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top