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

I am using rails 4 and I am proxying a GET request to another server as follows:

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

In my nginx configuration, I have the following:

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 is great for proxying GET requests, for example:

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 into a GET request. Therefore, I need to pass what is currently included in $ args as POST data.

How can I proxy this POST data? - Do I need to do something, for example response.method =: post or something else? - Where can I specify the parameters of what I get?

+4
source share
1 answer

, nginx. , GET.

, - lua. , nginx, , , - .

Ruby:

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;
  }
+1

Source: https://habr.com/ru/post/1535386/


All Articles