I have a storage where files are stored with filename corresponding to their MD5 hash. The files are stored like this:
...
/srv/storage/25/
/srv/storage/25/25D6D1AD130E4EB7D11F6AB48C09414E
...
/srv/storage/DF/
/srv/storage/DF/DF35407C5E433B5644538B41C957ABCE
...
The extra directories are just to prevent single root directory size blowing up.
These files are of various types; text files, PDF files, XLS files, anything goes. I'm serving these files over web interface that lists the files with real human file names that link to the HTTP file server. To make it easy for users to open these files from browser (Firefox) in appropriate application the web application appends file extension to the MD5 file name in file server URL so that browser can infer appropriate handler based on the filename extension. So for instance file report2017.pdf
in the web interface links to
http://corp.biz/files/DF35407C5E433B5644538B41C957ABCE.pdf
which with the help of rewriting defined below will serve file:
/srv/storage/DF/DF35407C5E433B5644538B41C957ABCE
I use this location in my Nginx site to rewrite the URL to the correct path in the filesystem:
location /files {
root /srv/storage;
rewrite '^/files/([0-9A-F]{2})([0-9A-F]{30}).*$' /$1/$1$2 break;
}
Awesome! It works. Except that now Nginx replies with Content-Type: application/octet-stream
for any URL under /files
. Is there any way to make Nginx resolve MIME type before the rewrite?
The problem with Content-Type: application/octet-stream
is that for PDF files even though the filename in URL ends with .pdf
, Firefox for some reason goes straight to Save File dialog instead of letting users open it in a PDF viewer. This only happens on boxes where PDF file type is set to Preview in Firefox.