![](/img/trans.png)
[英]Hosting Rails app on Thins, Nginx on a sub uri, behind a reverse proxy
[英]Nginx proxy redirect to another URI
我們的網站是各種圖像存儲庫。 每個圖像都具有外部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 location
結合使用,可以將Nginx代理與遠程文件配合使用。
以下是添加到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;
}
現在,您只需在對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
完整的解決方案在這里找到。 我建議在實施之前閱讀它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.