如何更改 nginx 中代理服务器响应的状态代码?

Wen*_*zil 5 proxy nginx proxypass

我很难将 nginx 配置为充当公共 S3 端点的代理。我的用例需要更改 S3 响应的状态代码,同时保留响应负载。

S3 返回的可能状态代码包括 200 和 403。对于我的用例,我需要将这些状态代码映射到 503。

我尝试了以下不起作用的方法:

location ~* ^/.* {
  [...]
  proxy_intercept_errors on;
  error_page             200 =503 $upstream_http_location
}
Run Code Online (Sandbox Code Playgroud)

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能用 nginx 实现我所需要的?

Wen*_*zil 6

我找到了一个或多或少合适的解决方案。这有点hackish,但它有效。

关键是将我的 S3 存储桶的索引文档设置为不存在的文件名。这会导致对 S3 存储桶端点的 / 请求导致 403。

由于 nginx 代理将所有传入请求映射到 S3 存储桶端点上的 /,因此结果始终为 403,nginx 代理可以拦截。从那里, error_page 指令告诉它通过请求S3 存储桶端点中的特定文档(在本例中为error.json)来响应,并使用 503 作为响应状态代码。

location ~* ^/. {
  proxy_intercept_errors on;
  error_page             403 =503 /error.json;
}
Run Code Online (Sandbox Code Playgroud)

此解决方案涉及将两个请求发送到 S3 存储桶端点(/、/error.json),但至少似乎使用上面更完整片段中的配置为这两个请求启用了缓存。