Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a POST request with X-Accel-Redirect with Rails?

I'm using rails 4 and I'm proxying a GET request to another server like this:

def proxy_video(path)
  self.status = 200
  response.headers["X-Accel-Redirect"] = "/proxy/#{path}"
  render text: 'ok'
end

In my nginx config, I have this:

location ~* ^/proxy/(.*?)/(.*) {
    internal;
    resolver 127.0.0.1;

    # Compose download url
    set $download_host $1;
    set $download_url http://$download_host/$2;

    # Set download request headers
    proxy_set_header Host $download_host;

    # Do not touch local disks when proxying content to clients
    proxy_max_temp_file_size 0;

    # Stream the file back send to the browser
    proxy_pass $download_url?$args;
  }

This works fine for proxying GET requests like:

proxy_image('http://10.10.0.7:80/download?path=/20140407_120500_to_120559.mp4')

However, I want to proxy a request that passes a list of files that will not fit in a GET request. So I need to pass what currently goes in $args as POST data.

How would I proxy this POST data? - Do I need to do something like response.method = :post or something? - Where would I provide the parameters of what I'm POSTing?

like image 744
Hackeron Avatar asked Nov 02 '22 02:11

Hackeron


1 Answers

I'm pretty sure you can't do this out-of-the-box with nginx. This feature is really designed for accelerating file downloads, so it's pretty focused on GET requests.

That said, you might be able to do something fancy with the lua module. After you've compiled a version of nginx that includes the module, something like this might work.

Ruby code:

def proxy_video(path)
  self.status = 200
  response.headers["X-Accel-Redirect"] = "/proxy/#{path}"
  response.headers["X-Accel-Post-Body"] = "var1=val1&var2=val2"
  render text: 'ok'
end

Nginx config:

location ~* ^/proxy/(.*?)/(.*) {
    internal;
    resolver 127.0.0.1;

    # Compose download url
    set $download_host $1;
    set $download_url http://$download_host/$2;

    rewrite_by_lua '
        ngx.req.set_method(ngx.HTTP_POST)
        ngx.req.set_body_data(ngx.header["X-Accel-Post-Body"])
        ';

    # Set download request headers
    proxy_set_header Host $download_host;

    # Do not touch local disks when proxying content to clients
    proxy_max_temp_file_size 0;

    # Stream the file back send to the browser
    proxy_pass $download_url?$args;
  }
like image 102
James Mason Avatar answered Nov 08 '22 07:11

James Mason