【发布时间】:2016-05-05 11:25:19
【问题描述】:
下面是一个非常标准的 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 吞没了,我什至在我的网络浏览器“网络”选项卡中都看不到响应。
有没有办法告诉 Nginx 在 proxy_pass 中返回错误代码的响应正文?
【问题讨论】:
-
@AlexeyTen 这不是我需要的标头,而是响应正文本身。我做了更多的挖掘,看起来身体实际上是返回的,但被浏览器本身吞噬了。
-
那是因为浏览器需要标头来允许访问响应正文。我猜,
add_header需要always标志 -
@AlexeyTen 所以我实际上比较了来自直接请求和代理请求的响应标头,它们是相同的。唯一不同的是,来自浏览器的代理请求包含 localhost 的 Origin 和 Referrer 标头,而非代理请求包含 X-Requested-With 标头。
-
显示完整的服务器配置\
标签: nginx error-handling http-status-code-404 httpresponse