繁体   English   中英

如何设置 nginx 代理路径以使用特定 s3 存储桶文件夹的 static 内容?

[英]How to set nginx proxy path to use static contents of a specific s3 bucket folder?

我正在处理一个 nginx 反向代理容器图像来代理来自 s3 的前端文件,我试图从特定文件夹位置访问这些文件,而不仅仅是 s3 存储桶的基本路径。 到目前为止,我只能提供我正在使用重写的 index.html,但我在 js 和 css 文件上得到了 403。

我试过包括 mime.types

include       mime.types;

我尝试添加一个 s3 文件夹存储桶参数

proxy_pass http://YOURBUCKET.s3-website.eu-central-1.amazonaws.com/$1;

然后各种正则表达式选项

这是我的 nginx 配置文件

server {
    listen 80;
    listen  443 ssl;
    ssl_certificate     /etc/ssl/nginx-server.crt;
    ssl_certificate_key   /etc/ssl/nginx-server.key;

    server_name timemachine.com;
    sendfile on;
    default_type application/octet-stream;
    resolver        8.8.8.8;
    server_tokens   off;

    location ~ ^/app1/(.*) {
        set $s3_bucket_endpoint  "timemachineapp.s3-us-east-1.amazonaws.com";
        proxy_http_version     1.1;
        proxy_buffering        off;
        proxy_ignore_headers   "Set-Cookie";
        proxy_hide_header      x-amz-id-2;
        proxy_hide_header      x-amz-request-id;
        proxy_hide_header      x-amz-meta-s3cmd-attrs;
        proxy_hide_header      Set-Cookie;
        proxy_set_header       Authorization "";
        proxy_intercept_errors on;
        rewrite ^/app1/?$ /dev/app1/index.html;    <-- I can only access index.html and the other js and css files throw a 403
        proxy_pass https://timemachineapp.s3-us-east-1.amazonaws.com;
        break;
    }
}

如您所见,我正在尝试这样做,以便用户转到 https://timemachine/app1 这将 go 转到主页并加载所有 css 和 js 文件。 同样,我得到的是 403,有时是 404。Insight 赞赏。

从问题看起来

  • 有一个恒定的请求 url 前缀/app1/
  • 有一个常量代理 url 前缀/dev/app1/

在此基础上...

首先,启用调试日志

nginx config中已经有一个error_log指令,找到它并暂时改为调试:

error_log  /dev/stderr debug;

这将允许您查看这些请求是如何处理的。

首先尝试天真简单

让我们使用此配置(为简洁起见,省略了其他 header 指令):

location = /app1 { # redirect for consistency
    return 301 /app1/;
}

location = /app1/ { # explicitly handle the 'index' request
    proxy_pass https://example.com/dev/app1/index.html;
}

location /app1/ {
    proxy_pass https://example.com/dev/;
}

并向它发出请求:

$ ~ curl -I http://test-nginx/app1/some/path/some-file.txt
HTTP/1.1 403 Forbidden
...

请注意,对于不存在的请求,S3 会返回 403,nginx 只是在此处代理该响应。

让我们查看日志以了解发生了什么:

2023/01/28 14:46:10 [debug] 15#0: *1 test location: "/"
2023/01/28 14:46:10 [debug] 15#0: *1 test location: "app1/"
2023/01/28 14:46:10 [debug] 15#0: *1 using configuration "/app1/"
...
"HEAD /dev/some/path/some-file.txt HTTP/1.0
Host: example.com
Connection: close
User-Agent: curl/7.79.1
Accept: */*
"

所以我们的请求变成了https://example.com/dev/some/path/some-file.txt

那是因为proxy_pass的工作方式是:

如果使用 URI 指定 proxy_pass 指令,则当将请求传递到服务器时,与位置匹配的规范化请求 URI 部分将替换为指令中指定的 URI

意义:

Nginx receives:
/app1/some/path/some-file.txt
      ^ the normalized path starts here

Proxied-upstream receives:
/dev/some/path/some-file.txt
     ^ and was appended to proxy-pass URI

我指出这一点是因为重命名/移动 s3 上的东西可能会导致更简单的 nginx 设置。

重写所有路径,而不是特定请求

像这样修改上面的配置:

location = /app1 { # redirect for consistency
    return 301 /app1/;
}

location = /app1/ { # explicitly handle the 'index' request
    proxy_pass https://example.com/dev/app1/index.html;
}

location /app1/ {
    rewrite ^/(.*) /dev/$1 break;             # prepend with /dev/
    # rewrite ^/app1/(.*) /dev/app1/$1 break; # OR this
    proxy_pass https://example.com/;          # no path here
}

再次尝试该测试请求会产生以下日志:

"HEAD /dev/app1/some/path/some-file.txt HTTP/1.0
Host: example.com
Connection: close
User-Agent: curl/7.79.1
Accept: */*

"

通过这种方式,索引请求可以工作,但也可以使用任意路径 - 并且无需修改此配置来处理每个单独的 url 请求。

好的,所以找到了解决方案。 除非我遗漏了什么,否则这比想象的要容易。 对于我的用例,我所要做的就是简单地为传入的那些 css 文件添加多个写入(我确信有一种更简单的方法可以指定任何 .css 文件扩展名,而不管文件的命名。无论如何,这里是目前的解决方案:

server {
    listen 80;
    listen  443 ssl;
    ssl_certificate     /etc/ssl/nginx-server.crt;
    ssl_certificate_key   /etc/ssl/nginx-server.key;

    server_name timemachine.com;
    sendfile on;
    default_type application/octet-stream;
    resolver        8.8.8.8;
    server_tokens   off;

    location ~ ^/app1/(.*) {
        set $s3_bucket_endpoint  "timemachineapp.s3-us-east-1.amazonaws.com";
        proxy_http_version     1.1;
        proxy_buffering        off;
        proxy_ignore_headers   "Set-Cookie";
        proxy_hide_header      x-amz-id-2;
        proxy_hide_header      x-amz-request-id;
        proxy_hide_header      x-amz-meta-s3cmd-attrs;
        proxy_hide_header      Set-Cookie;
        proxy_set_header       Authorization "";
        proxy_intercept_errors on;
        rewrite ^/app1/?$ /dev/app1/index.html;   
        rewrite ^/app1/?$ /dev/app1/cssfile.css;  <- and keep adding, if needed
        proxy_pass https://timemachineapp.s3-us-east-1.amazonaws.com;
        break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM