A
A
Anton Ipatov2018-11-12 16:58:16
Nginx
Anton Ipatov, 2018-11-12 16:58:16

How to use Nginx X-Accel-Redirect to proxy remote files in a Ruby on Rails application?

I want to use a proxy server to be able to collect file stats and not provide direct urls to users.
And in the future, be able to switch to another service without changing the original URL.
Right now I'm using Cloudinary and the file path looks like this:

<%= cl_video_tag("dog") %>
=> https://res.cloudinary.com/demo/video/upload/dog.mp4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
ivankomolin, 2018-11-13
@ivankomolin

Consider an example, without reference to the programming language:
There is a URL https://res.cloudinary.com/demo/video/upload/dog.mp4
There is a file /var/www/files/dog.mp4
There is nginx that processes client requests
There is some backend code
In nginx
1. Create an internal location in nginx, for example:

location /video/ {
  internal;
  root   /var/www/files/;
}

The internal instruction says that this location will only process requests from the backend.
In the code
1. Using the router, you catch a request for the URL https://res.cloudinary.com/demo/video/upload/dog.mp4
2. Process (in your case, save statistics, decide whether to give the content to the user or not)
3. If the file is needed give, add to the title
X-Accel-Redirect https://res.cloudinary.com/video/dog.mp4

After that, nginx gives the client the /var/www/files/dog.mp4 file
. And if we go directly to the url like https://res.cloudinary.com/video/dog.mp4, we get a 403 error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question