Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse proxy when proxied service returns HTTP 202

I am trying to setup a reverse proxy with Nginx for a docker image. I want to reverse proxy this image to add things in the headers for cors support. It works for all calls, but the one returning an HTTP 202 (accepted) answer. It appears that headers are not send back

I have changed several parameters, but I cannot find the best approach

Here is the nginx.conf I am using

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;
        }
    }
}

My Nginx server listens on port 8080 on Localhost. Upstream docker-recognizetext listens on port 5000

This docker image has a swagger page to view the calls. When I run the URL

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

on Chrome, I can list the response headers, and there are fins

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

When I post the following request (I stripped out some parameters)

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

The respone header are:

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

Where the Operation-Location value is the call to make to check the job's status. Calling this endpoint provides the good results in term of response headers.

My only issue is the the call returning a 202. It appears to me that Nginx needs a specific settings to route that call - but I cannot figure it out!

like image 417
Julien Chomarat Avatar asked Sep 18 '25 19:09

Julien Chomarat


1 Answers

from 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.

like image 83
Dusan Bajic Avatar answered Sep 21 '25 13:09

Dusan Bajic