【发布时间】: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 需要特定设置来路由该呼叫 - 但我无法弄清楚!
【问题讨论】: