Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active January 10, 2022 05:29
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save nepsilon/f46ff51a8c6d8356e35897758d230449 to your computer and use it in GitHub Desktop.
Save nepsilon/f46ff51a8c6d8356e35897758d230449 to your computer and use it in GitHub Desktop.
How to force a file download with Nginx? — First published in fullweb.io issue #73

How to force a file download with Nginx?

In short:

add_header Content-Disposition 'attachment; filename="foo.txt"';

We’re just adding a Content-Disposition header in the response. You can specify the file name, here we’re using foo.txt.

From there you can config the file name to be dynamic, using Nginx’s variables. For instance using $arg_filename:

add_header Content-Disposition 'attachment; filename="$arg_filename"';

You can now send a GET request with ?filename=hey.txt, and the filename will be set to hey.txt as the download starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment