【问题标题】:Reverse proxy when proxied service returns HTTP 202代理服务器返回 HTTP 202 时的反向代理
【发布时间】:2019-07-15 22:56:58
【问题描述】:

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

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

这是我正在使用的 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 服务器在 Localhost 上的 8080 端口上侦听。 上游 docker-recognizetext 监听 5000 端口

这个 docker 镜像有一个招摇页面来查看调用。当我运行 URL 时

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

在 Chrome 上,我可以列出响应头,并且有鳍

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

当我发布以下请求时(我去掉了一些参数)

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

响应头是:

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 值是用于检查作业状态的调用。调用此端点在响应标头方面提供了良好的结果。

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

【问题讨论】:

    标签: nginx nginx-reverse-proxy


    【解决方案1】:

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

    语法:add_header 名称 [always];
    默认值:
    上下文:http, server, location, if in location

    将指定字段添加到 响应标头,前提是响应代码等于 200、201 (1.3.10)、204、206、301、302、303、304、307 (1.1.16、1.0.13) 或 308 (1.13.0)。参数值可以包含变量。

    可能有多个 add_header 指令。这些指令是 从上一层继承当且仅当没有 在当前级别上定义的 add_header 指令。

    如果指定了always 参数(1.7.5),则标头字段将为 无论响应代码如何都添加。

    【讨论】:

    • 噗,我的错!添加“always”关键字节省了一天!非常感谢
    • 花了几个小时在这...我真的不明白为什么默认情况下会忽略合法的 2xx 状态代码
    猜你喜欢
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2017-08-16
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多