Nginx proxy redirection to another URI

Our site is a repository of images. Each image has the concept of an external URL and an internal URL. Customers see the external URL and they change when we experiment with SEO. An internal URL is a permanent URL that points to our image hosting service. We use our Ruby on Rails application to provide URL translation. Here is an example request:

-------- ----- ------- ------- ------------ | | --eURL--> | | --> | | --> | | -iURL--> | | |client| |CDN| |Nginx| | RoR | |Image Host| | | <-------- | | <-- | | <-- | | <-IMG--- | | -------- ----- ------- ------- ------------ 

The architecture works, but image streaming through RoR is inefficient. I want Nginx to proxy. What is this for. The proposed architecture will look something like this:

 -------- ----- ------- ------- | | --eURL--> | | --> | | ------> | RoR | |client| |CDN| |Nginx| <-????- | | | | <-------- | | <-- | | ------- -------- ----- | | ------------ | | -iURL-> |Image Host| | | <-IMG-- | | ------- ------------ 

What answer can I send to Nginx to get proxy data? I am not opposed to adding Nginx modules to my infrastructure, and, of course, I am open to modifying my nginx.conf.

X-Sendfile is the closest thing I found, but it only allows streaming from the local file system. Maybe there is another obscure HTTP response header or status code that I don't know about.

+4
source share
1 answer

Use the X-Accel-Redirect header in conjunction with the special Nginx location so that the Nginx proxy deletes the file.

The following is the location to add Nginx to your configuration:

 # Proxy download location ~* ^/internal_redirect/(.*?)/(.*) { # Do not allow people to mess with this location directly # Only internal redirects are allowed internal; # Location-specific logging access_log logs/internal_redirect.access.log main; error_log logs/internal_redirect.error.log warn; # Extract download url from the request set $download_uri $2; set $download_host $1; # Compose download url set $download_url http://$download_host/$download_uri; # Set download request headers proxy_set_header Host $download_host; proxy_set_header Authorization ''; # 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; } 

Now you just need to set the X-Accel-Redirect header in your answers to Nginx:

 # This header will ask nginx to download a file # from http://some.site.com/secret/url.ext and send it to user X-Accel-Redirect: /internal_redirect/some.site.com/secret/url.ext # This header will ask nginx to download a file # from http://blah.com/secret/url and send it to user as cool.pdf X-Accel-Redirect: /internal_redirect/blah.com/secret/url?cool.pdf 

A complete solution is found here . I suggest reading it before implementation.

+3
source

All Articles