Differentiate nginx behaviour depending on URL

2019-07-16 08:07发布

问题:

I have a Django application and I use nginx to serve static content. Unfortunately, all registered MIME types get displayed in client browser, while I would like to give an ability to download the same content, along with usual behaviour. Say, I have JPEG file under /media/images/image01.jpg and I want that nginx serves this file in usual way, with standard image/jpeg header, but additionally I want the same image to be served by nginx with content-disposition: attachment (effectively forcing content download) when accessed as, say, /downloads/images/image01.jpg. Anybody can suggest a solution?

回答1:

Make sure you have the http_headers_module compiled in. (should be by default, if it isn't in the core)

Use "add_header content-disposition attachment;"

I recommend using a url like "/download?file=/downloads/images/image01.jpg" combined with a rewrite rule to avoid some annoying bug later.

Http Headers Module Documention