Nginx的传递上游头的远程请求 [英] Nginx pass upstream headers to the remote request

查看:270
本文介绍了Nginx的传递上游头的远程请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题类似于<一href="http://stackoverflow.com/questions/24507157/adding-custom-http-headers-to-nginx-x-accel-redirect">this 之一,但做 X-加速重定向 AWS S3 资源。前者试图上游头设置为本地请求。这工作得很好。但现在我提出我的内容到Amazon S3和我想要实现相同的限制下载到S3的对象。

这是我的nginx的配置

  proxy_pass_header的X阿塞尔 - 重定向;
passenger_pass_header的X阿塞尔 - 重定向;

位置〜* ^ / download_zip /(.*){
    内部;
    解析8.8.8.8有效= 300秒;



    proxy_buffering关闭;
    proxy_set_header内容长度;
    proxy_set_header曲奇;

    proxy_hide_header的X AMZ-请求ID;
    proxy_hide_header的X AMZ-元UID;
    proxy_hide_header的x AMZ-ID-2;
    proxy_hide_header的x AMZ-元模式;
    proxy_hide_header的x AMZ-元修改时间;
    proxy_hide_header的X AMZ-元GID;
    proxy_hide_header的X AMZ-版本ID;
    proxy_hide_header接受-范围;

    #进行代理时,请勿触摸本地磁盘
    #的内容给客户
    proxy_method搞定;
    proxy_pass_request_body关闭;
    proxy_max_temp_file_size 0;

    proxy_pass_header内容-MD5;
    add_header内容-MD5 $ upstream_http_content_md5;

   #proxy_set_header内容-MD5123123123123123;


     proxy_pass https://a_b_c_assets.s3-east.amazonaws.com/$1$is_args$args;


     }
 

和导轨的部分是

 标题[X-加速重定向'] ='/ download_zip /上传/'+参数[:story_id] +'/'+参数[:story_id] +'的.zip ? + secure_part
标题[X-加速-过期'] ='最大'
标题[内容-MD5'] =校验
 

一切工作正常,除了内容-MD5 不传递给代理的请求。

任何帮助,真是AP preciated。

另一个<一href="http://serverfault.com/questions/382121/how-do-i-pass-headers-from-nginx-x-accel-redirect-back-to-the-browser">similar 的问题在这里

解决方案

  proxy_pass_header的X阿塞尔 - 重定向;
passenger_pass_header的X阿塞尔 - 重定向;

#这是存储头的中间位置
#变量并重写到另一个位置
位置^〜/ download_zip / {
    内部;
    设置$ Content_MD5 $ upstream_http_content_md5;
    改写^ / / amazon_ $ 1(+);
}

位置^〜/ amazon_download_zip / {
    内部;
    解析8.8.8.8有效= 300秒;

    proxy_buffering关闭;
    proxy_set_header内容长度;
    proxy_set_header曲奇;

    proxy_hide_header的X AMZ-请求ID;
    proxy_hide_header的X AMZ-元UID;
    proxy_hide_header的x AMZ-ID-2;
    proxy_hide_header的x AMZ-元模式;
    proxy_hide_header的x AMZ-元修改时间;
    proxy_hide_header的X AMZ-元GID;
    proxy_hide_header的X AMZ-版本ID;
    proxy_hide_header接受-范围;

    #进行代理时,请勿触摸本地磁盘
    #的内容给客户
    proxy_method搞定;
    proxy_pass_request_body关闭;
    proxy_max_temp_file_size 0;

    proxy_pass_header内容-MD5;
    add_header内容-MD5 $ Content_MD5;

    proxy_pass https://a_b_c_assets.s3-east.amazonaws.com/;
}
 

This question is similar to this one but doing x-accel-redirect on aws s3 resource. The former tried to set the upstream headers to the local request. This works fine. But now i have moved my contents to the amazon s3 and i wanted to implement the same restricted download to the s3 objects.

This is my nginx config

proxy_pass_header X-Accel-Redirect;
passenger_pass_header X-Accel-Redirect;

location ~* ^/download_zip/(.*) {
    internal;
    resolver 8.8.8.8 valid=300s;



    proxy_buffering off;
    proxy_set_header Content-Length ""; 
    proxy_set_header Cookie ""; 

    proxy_hide_header x-amz-request-id;
    proxy_hide_header x-amz-meta-uid;
    proxy_hide_header x-amz-id-2;
    proxy_hide_header x-amz-meta-mode;
    proxy_hide_header x-amz-meta-mtime;
    proxy_hide_header x-amz-meta-gid;
    proxy_hide_header x-amz-version-id;
    proxy_hide_header accept-ranges;

    # Do not touch local disks when proxying
    # content to clients
    proxy_method GET;
    proxy_pass_request_body off;
    proxy_max_temp_file_size 0;

    proxy_pass_header Content-MD5;
    add_header Content-MD5 $upstream_http_content_md5; 

   #  proxy_set_header Content-MD5 "123123123123123";


     proxy_pass https://a_b_c_assets.s3-east.amazonaws.com/$1$is_args$args; 


     }

and the rails part is

headers['X-Accel-Redirect'] = '/download_zip/uploads/' + params[:story_id] +'/' +    params[:story_id] + '.zip?' + secure_part
headers['X-Accel-Expires'] = 'max'
headers['Content-MD5'] = checksum

Everything works fine except the Content-MD5 is not passed to the proxied request.

Any help is really appreciated.

Another similar question here

解决方案

proxy_pass_header X-Accel-Redirect;
passenger_pass_header X-Accel-Redirect;

# this is intermediate location that stores header
# to variable and rewrites to another location
location ^~ /download_zip/ {
    internal;
    set $Content_MD5 $upstream_http_content_md5;
    rewrite ^/(.+) /amazon_$1;
}

location ^~ /amazon_download_zip/ {
    internal;
    resolver 8.8.8.8 valid=300s;

    proxy_buffering off;
    proxy_set_header Content-Length ""; 
    proxy_set_header Cookie ""; 

    proxy_hide_header x-amz-request-id;
    proxy_hide_header x-amz-meta-uid;
    proxy_hide_header x-amz-id-2;
    proxy_hide_header x-amz-meta-mode;
    proxy_hide_header x-amz-meta-mtime;
    proxy_hide_header x-amz-meta-gid;
    proxy_hide_header x-amz-version-id;
    proxy_hide_header accept-ranges;

    # Do not touch local disks when proxying
    # content to clients
    proxy_method GET;
    proxy_pass_request_body off;
    proxy_max_temp_file_size 0;

    proxy_pass_header Content-MD5;
    add_header Content-MD5 $Content_MD5; 

    proxy_pass https://a_b_c_assets.s3-east.amazonaws.com/;
}

这篇关于Nginx的传递上游头的远程请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆