Skip to content

Instantly share code, notes, and snippets.

Created October 19, 2011 01:30
  • Star 12 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
nginx remote file proxying
location ~* ^/remote-files/(http[s]*://)(.*?)/(.*) {
# Do not allow people to mess with this location directly
# Only internal redirects are allowed
# nginx has to be able to resolve the remote URLs
# Location-specific logging
#access_log /usr/local/etc/nginx/logs/internal_redirect.access.log main;
error_log /usr/local/etc/nginx/logs/internal_redirect.error.log warn;
# Extract download url from the request
set $download_uri $3;
set $download_host $2;
set $download_protocol $1;
# Compose download url
set $download_url $download_protocol$download_host/$download_uri;
# The next two lines could be used if your storage
# backend does not support Content-Disposition
# headers used to specify file name browsers use
# when save content to the disk
proxy_hide_header Content-Disposition;
add_header Content-Disposition 'attachment; filename="$args"';
# Do not touch local disks when proxying
# content to clients
proxy_max_temp_file_size 0;
# Download the file and send it to client
proxy_pass $download_url;
Copy link

Copy link

hedii commented Dec 1, 2015

Copy link

There's no reason to parse out and reconstruct the url unless there's a desire to set the Host header as demonstrated in other example. Simplifying the regex results in a more concise definition:

location ~ ^/remote-files/(.+) {
   resolver; # nginx has to be able to resolve the external url
   proxy_pass $1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment