Force File Downloads in NGINX with Simple Header Configurations

Learn how to configure NGINX to force browsers to download files instead of displaying them by adding appropriate Content‑Disposition headers, handling various file types, using regex locations, and reloading the server for the changes to take effect.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Force File Downloads in NGINX with Simple Header Configurations

Sometimes you need to force users to download files from your website rather than view them in the browser, especially when many downloadable resources are offered. This guide explains how to enforce file downloads using NGINX.

Situation 1: Basic Header

Adding the Content‑Disposition: attachment; header makes the browser download the file.

# Inline display (browser shows the file)
Content‑Disposition: inline; filename=foobar.pdf

# Force download (e.g., Firefox)
Content‑Disposition: attachment; filename=foobar.pdf

NGINX configuration example (place inside the appropriate server block):

location /download {
    add_header Content‑Disposition "attachment;";
}

Situation 2: Force Download for Specific Paths or Types

For image, PDF, or other files accessed via a certain path, you can force download and preserve the original filename using a regular‑expression location.

location ~ ^/somepath/(.*)$ {
    add_header Content‑Disposition "attachment; filename=$1";
    alias "E:/apache-tomcat-7.0.32/webapps/upload/$1";
}

Note that NGINX location priority is =, then ^~, and finally ~.

General rule to force download for a URL:

add_header Content‑Disposition "attachment; filename=$1";
default_type application/octet-stream;

Example for all URLs under /downloads:

location /downloads {
    ...
    add_header Content‑Disposition "attachment; filename=$1";
    default_type application/octet-stream;
    ...
}

Example for specific file extensions (e.g., .jpg, .png, .mp3):

location ~* ^/.+\.(?:gif|jpe?g|png|mp4|mp3)$ {
    ...
    add_header Content‑Disposition "attachment; filename=$1";
    default_type application/octet-stream;
    ...
}

After making changes, reload NGINX to apply them:

nginx -s reload
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendConfigurationFile Downloadcontent-disposition
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.