gpt4 book ai didi

Nginx proxy_pass 缺少错误正文

转载 作者:行者123 更新时间:2023-12-03 07:38:41 33 4
gpt4 key购买 nike

下面是一个非常标准的 nginx proxy_pass 设置:

server {
listen 80;
server_name ireport.jungdigital.com;
access_log /var/log/nginx/ireport.access.log;
root /var/www/ireport.jungdigital.com/dist;
index index.html index.htm;
location / {
}
location /api/ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
}
if ($request_method = 'PUT') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Reques
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-proxy true;
proxy_set_header Host ireport.somehost.org;
proxy_pass http://ireport_dyndns/api/;
proxy_ssl_session_reuse off;
proxy_redirect off;
}
}

我代理的 API 返回响应正文,其中包含 400、404 和 500 错误代码的错误信息。例如,在 404 上,我的响应正文可能如下所示:
{
"errorCode": "TOKEN_NOT_FOUND",
"errorMessages": [
"Could not find a matching authorization token."
]
}

如果我在没有代理的情况下执行请求,我会得到错误的响应正文。

如果我使用 nginx 代理,由于某种原因,响应正文被 nginx 吞并,我什至在我的 Web 浏览器“网络”选项卡中根本看不到响应。

有没有办法告诉 Nginx 在 proxy_pass 中返回错误代码的响应主体?

最佳答案

我最近遇到了同样的问题。

最后一个答案是:添加代理头升级

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';

我的完整配置如下:
upstream cloud-api {
server 127.0.0.1:8089;
}

client_max_body_size 20M;
client_header_buffer_size 8k;
large_client_header_buffers 4 16k;

server {
listen 8001;
access_log /data/senseid-cloud-log/senseid-cloud-api-access.log;
error_log /data/senseid-cloud-log/senseid-cloud-api-error.log warn;

location / {
proxy_http_version 1.1;
proxy_set_header Host $http_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-Proto $scheme;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';

proxy_read_timeout 300s;
proxy_pass http://cloud-api;
}
}

你可以捕获 500/401 .etc 错误正文

详情: http://nginx.org/en/docs/http/websocket.html

希望能给你一些帮助。

关于Nginx proxy_pass 缺少错误正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37049384/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com