我很难配置nginx充当公共S3端点的代理。我的用例需要更改S3响应的状态码,同时保留响应有效负载。
S3返回的可能状态码包括200和403。对于我的用例,我需要将这些状态码映射到503。
我尝试了以下无效的方法:
location ~* ^/.* {
[...]
proxy_intercept_errors on;
error_page 200 =503 $upstream_http_location
}
Nginx输出以下错误:
nginx:在/etc/nginx/nginx.conf:xx中,[emerg]值“ 200”必须介于300和599之间
这是更完整的代码段:
server {
listen 80;
location ~* ^/.* {
proxy_http_version 1.1;
proxy_method GET;
proxy_pass http://my-s3-bucket-endpoint;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header Connection "";
proxy_set_header Host my-s3-bucket-endpoint;
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_cache S3_CACHE;
proxy_cache_valid 200 403 503 1h;
proxy_cache_bypass $http_cache_purge;
add_header X-Cached $upstream_cache_status;
proxy_intercept_errors on;
error_page 200 =503 $upstream_http_location;
}
}
Nginx是否可以实现我所需要的?
我找到了或多或少合适的解决方案。有点破烂,但是可以用。
关键是将S3存储桶的索引文档设置为不存在的文件名。这会导致对/在S3存储桶端点上的请求导致403。
由于nginx代理将所有传入请求映射到S3存储桶端点上的/,因此结果始终是403,nginx代理可以拦截该结果。从那里,error_page指令告诉它通过在S3存储桶端点中请求特定文档(在本例中为error.json)进行响应,并使用503作为响应状态代码。
location ~* ^/. {
proxy_intercept_errors on;
error_page 403 =503 /error.json;
}
此解决方案涉及将两个请求发送到S3存储桶终结点(/,/error.json),但至少可以使用上面更完整的代码段中的配置为这两个请求启用缓存。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句