502 плохих шлюза с Nginx, X-Accel-Redirect и Rails

Я хочу разрешить пользователям загружать файлы из удаленного хранилища, но сначала я хочу авторизовать запрос через мое приложение rails. Я хочу передать прокси удаленного файла в nginx, когда rails аутентифицирует запрос, чтобы освободить поток ruby ​​/ rails.

У меня есть этот файл конфигурации nginx с именем proxy_download.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

Журналы ошибок показывают только это:

2016/04/22 16:47:27 [error] 11335#0: *150842 connect() failed (111: Connection refused) while connecting to upstream, client: 159.203.238.12, server: domain-removed.com, request: "GET / HTTP/1.1", upstream: "http://172.17.0.34:5000/", host: "domain-removed.com

Что не так с моей настройкой? Спасибо!

0 ответов

Другие вопросы по тегам