Nginx proxy_pass缺少错误体

时间:2016-05-05 11:25:19

标签: nginx error-handling http-status-code-404 httpresponse

下面是一个非常标准的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中返回错误代码的响应主体?

3 个答案:

答案 0 :(得分:2)

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

最后一个问题是:添加代理标题升级

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

希望能给你一些帮助。

答案 1 :(得分:0)

今天我将Laravel 5.2从IIS 8.5迁移到Ubuntu 16.04(Nginx 1.10.0 - PHP-FPM 7.0.10),我遇到了同样的问题。当来自Angular2的请求时,来自服务器的身体响应始终为空。但邮递员仍然得到回应。 因此,请求标题必须存在问题。

这个配置帮我解决了上面的问题:

    add_header 'Access-Control-Allow-Origin' '*' 'always';
    add_header 'Access-Control-Allow-Credentials' 'true' 'always';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS, HEAD' 'always';
    add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, X-Requested-With' 'always';

add_header的第三个参数仅在最近的nginx版本中可用。

答案 2 :(得分:0)

请参阅How to add a response header on nginx when using proxy_pass?

  1. 根据Alexey的评论 - “浏览器需要标题才能访问响应正文。我想,您需要始终标记为ResponseEntity<Object> response = restTemplate.getForEntity(url, Object.class); String json = response.getBody().toString();
  2. 从nginx 1.7.5开始,即使在错误响应中也可以使用关键字add_header来包含标题 - 因此您可以按照以下方式设置nginx.conf:
  3. -

    always