node.js - 如何使用nginx作为s3 aws的代理?

标签 node.js amazon-web-services docker nginx amazon-s3

我尝试在 docker 中构建 nginx 作为 aws s3 的代理。

问题是有一个变量我不明白它们来自哪里?

首先,这是我的水桶的样子:

enter image description here

在这个存储桶中,我有 pic.png 文件。

当我使用 nginx 时,我使用 docker-compose 从 docker 开始:

web:
  image: nginx
  volumes:
    - ./example.com.conf:/etc/nginx/conf.d/default.conf
  ports:
    - '8080:80'

我使用docker-compose up启动docker。

我有来自 IAM key 的 aws_access_key 和 aws_secret_key。

enter image description here

这就是我定义 example.com.conf 文件的方式:

server {                                                                                                
    listen       80;                                                                                    
    server_name  localhost;                                                                             

    location ~ '^/([^/]+)/(.*)$' {
            set $bucket 'my-bucket';
            set $key '';

            # Setup AWS Authorization header
            set $aws_signature '';

            # the only reason we need lua is to get the current date
            set_by_lua $now "return ngx.cookie_time(ngx.time())";

            #the  access key
            set $aws_access_key 'AKIA6*******';
            set $aws_secret_key '1wLXpiNN0***********';

            # the actual string to be signed
            # see: http://docs.amazonwebservices.com/AmazonS3/latest/dev/RESTAuthentication.html
            set $string_to_sign "$request_method\n\n\n\nx-amz-date:$now\n/$bucket/$key";

            # create the hmac signature
            set_hmac_sha1 $aws_signature $aws_secret_key $string_to_sign;
            # encode the signature with base64
            set_encode_base64 $aws_signature $aws_signature;
            proxy_set_header x-amz-date $now;
            proxy_set_header Authorization "AWS $aws_access_key:$aws_signature";

            rewrite .* /$key break;

            # we need to set the host header here in order to find the bucket
            proxy_set_header Host $bucket.s3.amazonaws.com;
            rewrite .* /$key break;

            # another solution would be to use the bucket in the url
            # rewrite .* /$bucket/$key break;

            proxy_pass http://s3.amazonaws.com;
        }

}                                                                                                       

但是当我使用 nginx 运行 docker 时出现错误:

 nginx: [emerg] unknown directive "set_by_lua" in /etc/nginx/conf.d/default.conf:13

所以我不确定我这样做是否正确。 我需要一个解释和一个如何正确做的例子。 例如 $key 是什么?请求应该是什么样的? http://localhost:8080/pic.png?

最佳答案

尝试使用安装了lua的nginx:

web:
  image: firesh/nginx-lua
  volumes:
    - ./example.com.conf:/etc/nginx/conf.d/default.conf
  ports:
    - '8080:80'

问题是set_by_lua需要使用ngx_devel_kit编译nginx

更新

看来你错过了很多模块,我建议你使用这个Dockerfile

示例:

docker run -v /path/to/example.com.conf:/etc/nginx/conf.d/default.conf openresty/openresty:centos

关于node.js - 如何使用nginx作为s3 aws的代理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57478234/

相关文章:

javascript - 使用 Grunt Bake 观看多个文件而无需一次全部编译

python - 无法编译 cuda_ndarray.cu : libcublas. so.7.5:无法打开共享对象文件

amazon-web-services - 有没有办法在 AWS 中使用自动缩放来停止而不是终止实例?

json - 将 cloudformation 模板移植到 terraform

docker - Traefik配置示例混合让我们加密和购买证书

javascript - 寻找在 Node.js 中发布数千个 HTTP 请求的有效方法

javascript - 任何基于 JavaScript/jQuery 的 html 数据处理器/美化器?

node.js - docker swarm 中容器之间的通信

docker - 如何减少构建上下文

gcc - 将 GCC 编译器安装到 Docker 容器上