编辑
我已经能够使用curl来直接联系后端,并且肯定是发送尾标。它似乎在正文之后的输出流中,因此,也许我的api例程需要稍后对其进行检查,否则它可能无法通过nginx。
第二编辑我使用curl直接访问后端,但看到的是尾标头。联系前端(Nginx),我看不到
原始问题
我正在尝试使节点服务器响应来自客户端的API请求。
此服务器是nginx代理的后端,可能会剥离标头,但...这是它的反向代理配置,所以我认为不是
location /api/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://localhost:2040; proxy_redirect off; proxy_buffering off; proxy_cache off; }
服务器正在处理这样的特定api请求
router.post('/api/process', async (req,res) => { res.writeHead(200, { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache', 'X-Accel-Buffering': 'no', 'Trailer': 'API-Status' }); try { response = await callApiProcessor(req.params); res.write(JSON.stringify(response)); res.addTrailers({'API-Status': 'OK'}) catch (e) { res.addTrailers({'API-Status': e.toString()}); } res.end(); });
在浏览器端,我正在使用这样的访存API
export default function api(url, params, signal) { const options = { credentials: 'same-origin', method: 'post', headers: new Headers({ 'content-type': 'application/json' }), body: JSON.stringify(params) }; if (signal) options.signal = signal; return window.fetch('/api/' + url, options).then(response => { if (!response.ok || response.headers['API-Status'] !== 'OK') { if (response.ok) console.warn('SERVER API Error:', response.headers['API-Status']); window.dispatchEvent(new LogoffRequest()); //put us back to home window.history.pushState({}, null, '/'); window.dispatchEvent(new LocationAltered()); return {}; } else { return response.json(); } }); }
通过在api模块中放置断点,我无法在响应中看到任何标头。因此,尽管
response.ok
为true,但我仍在应用程序中强制客户端注销。
虽然我确实看到了Trailer标头,但查看Chrome的Dev Tools的Networking标签,我看不到API状态。
编辑,我已经能够使用curl直接与后端联系,并且肯定是在发送尾部标头。它似乎在正文之后的输出流中,所以也许我的api例程需要...
如上所述,nginx是预告片。我在nginx邮件列表上问了这个问题,答案是它没有实现传递尾标。但是,我已经解决了这个问题,那就是您如何在服务器中,在发送标头之后注意到一个问题(因为您已经开始发送正文),并传递回本质上是403或500服务器响应的内容。