Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save genffy/4944f96c08fab9290da7180d13c2fe33 to your computer and use it in GitHub Desktop.
Save genffy/4944f96c08fab9290da7180d13c2fe33 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