简体   繁体   English

Nginx代理重定向到另一个URI

[英]Nginx proxy redirect to another URI

Our site is an image repository of sorts. 我们的网站是各种图像存储库。 Each image has the notion of an external URL and an internal URL. 每个图像都具有外部URL和内部URL的概念。 External URL's are seen by clients and they change as we experiment with SEO. 外部URL由客户端看到,并且随着我们尝试SEO而改变。 The internal URL's are permanent URL's that point to our image hosting service. 内部URL是永久URL,指向我们的图像托管服务。 We use our Ruby on Rails app to provide the URL translation. 我们使用Ruby on Rails应用程序提供URL转换。 Here's an example of a request: 以下是请求的示例:

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

The architecture is working, but streaming the image through RoR is inefficient. 该架构正在运行,但通过RoR流式传输图像效率很低。 I want to have Nginx do the proxying. 我想让Nginx做代理。 That's what it's for. 这就是它的用途。 The proposed architecture would look something like this: 建议的架构看起来像这样:

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

What response can I send to Nginx to have it proxy the data? 我可以向Nginx发送什么样的响应让它代理数据? I don't mind adding Nginx modules to my infrastructure and of course I'm open to changing my nginx.conf. 我不介意将Nginx模块添加到我的基础架构中,当然我可以更改我的nginx.conf。

X-Sendfile is the closest thing I've found, but that only allows streaming from the local filesystem. X-Sendfile是我发现的最接近的东西,但它只允许从本地文件系统流式传输。 Maybe there is some other obscure HTTP response header or status code I'm unaware of. 也许还有一些我不知道的其他模糊的HTTP响应头或状态代码。

Use the X-Accel-Redirect header in combination with a special Nginx location to have Nginx proxy the remote file. X-Accel-Redirect标头与特殊的Nginx location结合使用,可以将Nginx代理与远程文件配合使用。

Here is the location to add to your Nginx configuration: 以下是添加到Nginx配置的location

# 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 have to set the X-Accel-Redirect header in your responses to Nginx: 现在,您只需在对Nginx的响应中设置X-Accel-Redirect标头:

# 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

The full solution was found here . 完整的解决方案在这里找到。 I suggest reading it before implementing. 我建议在实施之前阅读它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM