Force File Download in NGINX & Apache

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!

19 Comments

  1. Duhvillage said:

    Hey Mike thanks for this it really helped

    February 2, 2015
    Reply
    • Mike Price said:

      Thanks! no problem, glad it was helpful 🙂

      February 2, 2015
      Reply
  2. Nick said:

    Thanks! this was a life saver, I recently migrated from Apache to NGINX and had no idea how to handle this.

    June 10, 2015
    Reply
    • Mike Price said:

      Hey Nick!

      No problem at all, I am very glad that I was able to help you with this 🙂 enjoy NGINX!

      June 10, 2015
      Reply
      • Abdou Rasiz said:

        Hi , Mike , I just want to know if your solution is applicable for a rails application using nginx as a server
        Thanks

        September 29, 2015
        Reply
        • Mike Price said:

          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!

          September 29, 2015
          Reply
          • Abdou Rasiz said:

            Mike thank you , I have nothing to lose by trying it.

            September 30, 2015
  3. Jess said:

    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

    November 25, 2015
    Reply
  4. Amie Colbert said:

    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.

    August 14, 2016
    Reply
  5. Nauris said:

    Finally found! Thanks a lot!

    October 24, 2016
    Reply
    • Mike Price said:

      Awesome Nauris. I am so happy to know this was helpful to you. Hope you have a great week

      October 24, 2016
      Reply
  6. Faith said:

    Thank it what on my site users can now download mp3 files

    August 27, 2017
    Reply
    • Mike Price said:

      Hey Faith

      So glad to hear this was helpful to you. Take care!

      August 28, 2017
      Reply
  7. raven lescano said:

    can I change it with .rar?

    October 10, 2017
    Reply
    • Mike Price said:

      Hi Raven,

      Sure, I think it should work if you replace .pdf with .rar in the code.

      October 11, 2017
      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *