This week I encountered a customer who was struggling to find the proper code that would force the download of PDF and .txt files on his WordPress site. The reason for the struggle was based in the fact that he was using code that was designed to work with Apache in the .htaccess file.
However, his WordPress site was running on a server that used NGINX before passing requests to Apache. Because of this, the code in the .htaccess was not working to force his files to download. So what is the proper code to use?
I will start off by showing you the code that does work when running just plain Apache. The following code snippet would need to be placed in your .htaccess file. This will force all PDF files to download on your site:
ForceType application/octet-stream Header set Content-Disposition attachment
The mention of ‘pdf’ can be changed to ‘txt’ in order to do the same for txt files as well as any other file type such as mp3. Now, if like my customer, you are running NGINX, you would want to use the following rewrite rule:
location ~* (.*\.pdf) { types { application/octet-stream .pdf; } default_type application/octet-stream; }
Again, replace ‘pdf’ with any other type of file that you would like to force download such as txt, mp3, etc.
Keep in mind that some of you may not have access to input rewrite rules into the NGINX configuration. You may have to contact your host directly in order for them to apply the rule.
Want to Learn More About WordPress? Subscribe Now!
Hey Mike thanks for this it really helped
Thanks! no problem, glad it was helpful 🙂
This is really helpful. Thanks so much
Thanks! this was a life saver, I recently migrated from Apache to NGINX and had no idea how to handle this.
Hey Nick!
No problem at all, I am very glad that I was able to help you with this 🙂 enjoy NGINX!
Hi , Mike , I just want to know if your solution is applicable for a rails application using nginx as a server
Thanks
Hi Abdou
Great question. I am not familiar with Rails since I handle only WP but this particular code snippet is pure nginx so I don’t see why it wouldn’t work with Rails as well. It isn’t WP specific nginx code. Hope it works out for you!
Mike thank you , I have nothing to lose by trying it.
Mike,
Curious if you could help me make the folder location in the NGINX block variable. For example, if I wanted to apply this to all files in all subfolders under the /files/ parent, is that possible?
Thanks!
Jess
Hey Jess
I am not a guru at this (more of my knowledge is in WordPress) so I would probably recommend taking a look at this article that shows a section on using the location block:
https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms
I hope that helps you!
Just awesome topic! Thanks for the info, super helpful. Filling out forms is super easy with PDFfiller. Try it on your own here https://goo.gl/udMb44 a form and you’ll make sure how it’s simple.
Finally found! Thanks a lot!
Awesome Nauris. I am so happy to know this was helpful to you. Hope you have a great week
Hey! This does not work only on Firefox. Tested non Mac. Linux and Windows ;/
Cheers
Ah sorry to hear that. Very odd but appears to be something within Firefox settings that prevents it from working. I found this https://kb.mozillazine.org/File_types_and_download_actions
[…] Force File Download in NGINX & Apache – Mike Price Nov Force File Download in NGINX & Apache. This week I encountered a customer who was struggling to […]
Thank it what on my site users can now download mp3 files
Hey Faith
So glad to hear this was helpful to you. Take care!
can I change it with .rar?
Hi Raven,
Sure, I think it should work if you replace .pdf with .rar in the code.
Thanks a lot for this it really works, i just changed it to my own mp3 file extension and work like magic, gratitude
Awesome, so glad that this was helpful for you and your Mp3 files. Have a great day!
Hi, I am also using nginx. And I tried your solution but it didnot work for me. So how can I go about this?
Hi Martin
Honestly, this should work without issues as it is a general rule for NGINX. So, I really don’t have an alternative answer for you. I am sorry.
Hi, I am also using nginx. And I tried your solution .. Worked Perfectly
Awesome, so glad to hear it helped you!
Hello, thank you so much, Mike, for this solution. I was looking for the same thing for some time and I found it here.
Thanks again.
I was really frustrated this morning when I got this issue, thank you for the solution.
I was really sad when I got this error earlier today. Thanks for this
Wow, I’m sincerely grateful, I finally found what I’ve been searching for, I’m a female upcoming writer and I must confess, this is worth reading.