ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

ruby-on-rails – Nginx代理重定向到另一个URI

2019-06-29 20:57:11  阅读:185  来源: 互联网

标签:nginx ruby-on-rails large-files cdn streaming


我们的网站是各种图像存储库.每个图像都具有外部URL和内部URL的概念.外部URL由客户端看到,并且随着我们尝试SEO而改变.内部URL是永久URL,指向我们的图像托管服务.我们使用Ruby on Rails应用程序提供URL转换.以下是请求的示例:

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

该架构正在运行,但通过RoR流式传输图像效率很低.我想让Nginx做代理.这就是它的用途.建议的架构看起来像这样:

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

我可以向Nginx发送什么样的响应让它代理数据?我不介意将Nginx模块添加到我的基础架构中,当然我可以更改我的nginx.conf.

X-Sendfile是我发现的最接近的东西,但它只允许从本地文件系统流式传输.也许还有一些我不知道的其他模糊的HTTP响应头或状态代码.

解决方法:

将X-Accel-Redirect标头与特殊的Nginx位置结合使用,可以将Nginx代理与远程文件配合使用.

以下是添加到Nginx配置的位置:

# 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;
}

现在,您只需在对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

找到了完整的解决方案here.我建议在实施之前阅读它.

标签:nginx,ruby-on-rails,large-files,cdn,streaming
来源: https://codeday.me/bug/20190629/1329410.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有