【发布时间】:2012-03-29 14:56:20
【问题描述】:
我在 nginx 后面使用 node.js。node.js 应用程序使用 http-proxy。
基本上,请求到达 nginx,然后被代理到节点应用程序:
- if static files are requested, the node.js app serves them
- if non static files are requested, the node.js app proxyies the request to a second node.js app (using http-proxy npm).
当 nginx 不在图片中时,第二种情况完美运行。添加 nginx 后,响应很奇怪:被奇怪的东西包围:
"4c\r\n{ json stuff }\r\n0"
而不是
{ json stuff }
总结一下:
-
(没有 nginx):请求动态内容 -> node.js -> 代理到另一个 node.js 应用程序 -> 发送回用户的响应是正确的:{ json stuff }
李> (使用 nginx):请求动态内容 -> node.js -> 代理到另一个 node.js 应用程序 -> 发送回用户的响应不正确:“4c\r\n{ json stuff }\r\n0"
我还是不明白这里发生了什么……有什么想法吗?
更新
嗯....似乎它与 nginx 相关联,向请求中添加了额外的标头...
我的 nginx 配置是:
upstream my_sock {
server unix:/tmp/test.sock
fail_timeout=0;
}
server {
listen 8099;
client_max_body_size 4G;
server_name localhost;
keepalive_timeout 5;
location / {
proxy_pass http://my_sock;
proxy_redirect off;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
}
知道如何删除这些额外的标题吗?
更新 2
添加的 4c 似乎是我期望的响应的长度(json 的东西)。然后我补充说:
proxy_set_header Content-Length '';
在 nginx conf 中但没有任何改变...我仍然显示 4c...Arg...
【问题讨论】:
-
与您的问题没有特别相关,但如果第一个节点实例仅提供静态文件,为什么不使用 NginX 来执行此操作,然后让 nginx 代理到第二个节点实例? NginX 擅长处理静态文件。
-
你说得对,这也是我正在考虑的一个不错的选择,事实上我已经尝试过并且遇到了同样的问题(添加了内容长度)。一旦我解决了这个问题,我可能会回到你所说的解决方案。
-
是否可以在nginx conf中添加一些逻辑,以便根据查询字符串的格式代理请求?