【问题标题】:How to use nginx as proxy to s3 aws?如何使用 nginx 作为 s3 aws 的代理?
【发布时间】:2021-03-03 09:58:32
【问题描述】:

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

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

首先,这是我的存储桶的样子:

在这个桶里面我有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 密钥的 aws_access_key 和 aws_secret_key。

这就是我定义 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?

【问题讨论】:

  • ngx_devel_kit 模块是必需的,然后你可以使用 set_by_lua

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


【解决方案1】:

尝试使用安装了lua的nginx:

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

问题是set_by_lua需要nginxngx_devel_kit一起编译

更新

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

示例:

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

【讨论】:

  • 尝试运行docker pull firesh/nginx-lua然后运行您的撰写
  • 现在还有一个问题:`nginx: [emerg] unknown directive "set_hmac_sha1" in /etc/nginx/conf.d/default.conf:24`。
  • 你需要开始使用google然后.....github.com/openresty/set-misc-nginx-module#installation
  • 因为我不确定这个 dockerfile 应该是什么样子。您能否提供我需要使用的所有模块的示例,如果不是,我会看,但我不确定要看什么。唯一的问题是我缺少模块?
  • 你需要启动nginx然后你才能看到你得到了什么错误
【解决方案2】:

使用 lua 代理 S3 的另一种方法是使用 njs 进行代理。 This project 展示了一个使用 nginx 代理 S3 并在 Docker 中运行的示例。

【讨论】:

    猜你喜欢
    • 2015-05-22
    • 2018-05-10
    • 2017-07-21
    • 2017-05-11
    • 1970-01-01
    • 2015-02-09
    • 2019-12-11
    • 2018-12-02
    • 1970-01-01
    相关资源
    最近更新 更多