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?

有帮助吗?

解决方案 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
  ';
}

其他提示

Pure Nginx configuration

location / {
  if ($arg_f) {
    add_header Content-Disposition "attachment; filename=$arg_f";
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top