重定向/重写(在Nginx中)对子目录的请求到与S3兼容的存储桶

时间:2018-11-29 21:23:35

标签: nginx redirect amazon-s3 url-rewriting

我正在尝试让nginx将请求重定向/重写到特定的子目录,以使它们由与S3兼容的存储桶(而不是服务器)提供服务。这是我当前的服务器块:

{snip}(请参见下文。)

尽管对此摆弄了一段时间,但我只能使它返回404。

其他信息

https://omnifora.com/t/redirect-rewrite-in-nginx-requests-to-subdirectory-to-s3-compatible-bucket/402

到目前为止尝试解决方案

  1. rewrite

    重写^ / security-now /(.*)$ scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;

  2. return

    返回302 $ scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;

  3. proxy_pass

    proxy_set_header主机s3.us-west-1.wasabisys.com;         proxy_pass $ scheme://s3.us-west-1.wasabisys.com/bits-podcasts/security-now/$1;

1 个答案:

答案 0 :(得分:2)

您不能使用rewrite进行跨域重定向,在这种情况下,您必须使用proxy_pass,例如:

    location ~ ^/directory1/(.*) {
        proxy_set_header Host s3.us-west-1.wasabisys.com;
        proxy_pass $scheme://s3.us-west-1.wasabisys.com/target-bucket/security-now/$1;
    }

请注意,如果用域名或IP地址指定服务器,则需要在server配置块中指定其他参数resolver,例如:

server {
    ...
    resolver 8.8.8.8;
    ...
}

更新。

似乎我说错了,您不能使用rewrite进行跨域重定向。您可以,但是在这种情况下,您的用户获得了HTTP 301重定向,而不是“透明”的内容传递。也许您收到404错误,是因为您在scheme变量之前错过了$号?