我想允许用户从远程存储下载文件,但我想先通过我的Rails应用程序validation请求。 当railsvalidation请求时,我想把远程文件的代理移交给nginx,释放ruby / rails线程。
我有这个名为proxy_download.conf的nginx conf文件:
# 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 combined; 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 conf中,如下所示:
include $ROOT/TO/APP/nginx.conf.d/proxy_download.conf;
该应用程序部署良好,并使用此设置正确运行。
这些是启动下载请求的控制器方法:
def x_accel_url(url, file_name = nil) uri = "/internal_redirect/#{url.gsub('http://', '').gsub('https://', '')}" uri << "?#{file_name}" if file_name return uri end def download if auth_is_ok # do some auth logic here headers['X-Accel-Redirect'] = x_accel_url('http://domain.com/file.ext') render :nothing => true end end
当通过浏览器打这个控制器的方法,我得到着名的nginx错误:
502 Bad Gateway
我的设置有什么问题? 谢谢!