nginx - 代理服务返回 HTTP 202 时的反向代理

标签 nginx nginx-reverse-proxy

我正在尝试使用 Nginx 为 docker 镜像设置反向代理。我想反向代理此图像以在标题中添加内容以支持 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 上,我可以列出响应头,并且有鳍
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 值是用于检查作业状态的调用。调用此端点在响应 header 方面提供了良好的结果。

我唯一的问题是调用返回 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.

关于nginx - 代理服务返回 HTTP 202 时的反向代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57041647/

相关文章:

php - Nginx + Xamp + SSL 奇怪的行为

php - Nginx 错误 FastCGI 在 stderr : "Access to the script [folder] has been denied (see security.limit_extensions)" 中发送

ssl - https 需要 url 中的 80 端口

Docker Compose、Nginx、解析器不工作

redirect - traefik 中的 nginx `proxy_redirect` (重定向而不更改 url)

如果特定域和特定 url,NGINX 重写 url

php - 如何配置 Nginx 以将特定请求模式路由到托管 Symfony 应用程序的特定 php-fpm 上游?

nginx - 如何允许通过 CORS 访问 nginx 中的多个域

nginx - 如何使用 NGINX 作为反向代理来路由第一个 URI 段?

node.js - Elastic Beanstalk + Nginx + Node + React 502 网关