代理服务时反向代理 returns HTTP 202

Reverse proxy when proxied service returns HTTP 202

我正在尝试使用 Nginx 为 docker 图像设置反向代理。我想反向代理此图像以在 header 中添加内容以支持 cors。它适用于所有调用,但返回 HTTP 202(已接受)应答的调用除外。 header 似乎没有发回

我已经更改了几个参数,但我找不到最好的方法

这是我正在使用的nginx.conf

worker_processes 1;

events { worker_connections 1024; }

error_log /etc/nginx/error_log.log warn;

http {

    sendfile on;

    upstream docker-recognizetext {
        server recognizetext:5000;
    }

    server {
        listen 8080;

        location / {

          if ($request_method = OPTIONS) {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 200;
          }

          if ($request_method = 'POST') {
              add_header 'Access-Control-Allow-Origin' '*';
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
              add_header 'Access-Control-Allow-Headers' 'Operation-Location,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
              add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
          }
          if ($request_method = 'GET') {
              add_header 'Access-Control-Allow-Origin' '*';
              add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
              add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
              add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
          }

          proxy_pass         http://docker-recognizetext;
          proxy_redirect     off;
          proxy_set_header   Host $host;
          proxy_set_header   X-Real-IP $remote_addr;
          proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header   X-Forwarded-Host $server_name;
        }
    }
}

我的 Nginx 服务器侦听本地主机上的端口 8080。 上游 docker-recognizetext 监听端口 5000

此 docker 图片有一个用于查看调用的 swagger 页面。当我 运行 URL

http://localhost:8080/swagger/index.html

在Chrome上,我可以列出响应headers,还有鳍

HTTP/1.1 200 OK
Server: nginx/1.17.1
Date: Mon, 15 Jul 2019 14:10:23 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
Access-Control-Expose-Headers: Content-Length,Content-Range

当我post下面的请求时(我去掉了一些参数)

POST http://localhost:8080/vision/v2.0/recognizeText?mode=printed

回复header是:

HTTP/1.1 202 Accepted
Server: nginx/1.17.1
Date: Mon, 15 Jul 2019 13:58:09 GMT
Content-Length: 0
Connection: keep-alive
Operation-Location: http://localhost/vision/v2.0/textOperations/24a63f9d-e272-4c84-a062-f405f6ec64e4

其中 Operation-Location 值是检查作业状态的调用。调用此端点在响应 headers.

方面提供了良好的结果

我唯一的问题是调用返回 202。在我看来,Nginx 需要特定的设置来路由该调用 - 但我无法弄清楚!

来自 http://nginx.org/en/docs/http/ngx_http_headers_module.html :

Syntax: add_header name value [always];
Default:
Context: http, server, location, if in location

Adds the specified field to a response header provided that the response code equals 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). Parameter value can contain variables.

There could be several add_header directives. These directives are inherited from the previous level if and only if there are no add_header directives defined on the current level.

If the always parameter is specified (1.7.5), the header field will be added regardless of the response code.