javascript - Nodejs 代理脚本,不适用于 mod_deflate

标签 javascript apache proxy node.js

我创建了一个小代理 Node 脚本,它查找 request.url 并将请求传递到我的 apache 服务器或使用 Node 处理/响应此请求。到目前为止,我已经成功了, 一切正常,但是当我为 apache 启用 mod_deflate 时, “奇怪的事情会发生”。

看起来 Node 只是提前“取消”或“停止”响应方式。 我正在听我请求的“数据”事件,在某个时候 Node 只是决定响应已经结束(这是错误的)并且 触发“结束”事件。

代码片段:

var apache = http.createClient(82, 'localhost');

function pass_to_apache(req, res){
    var request = apache.request(req.method, req.url, req.headers);

    req.addListener('end', function() {
        request.end();
    });

    req.addListener('data', function(chunk) {
        request.write(chunk);
        sys.puts('writting chunk\n');
    });

    request.addListener('response', function(response) {
        res.writeHead(response.statusCode, response.headers);
        response.addListener('data', function(chunk) {
            sys.puts('writting data..\n');
            res.write(chunk);
        });
        response.addListener('end', function() {
            sys.puts('end of request');
            res.end();
        });
    });
}

var MainServer = http.createServer(function(request, response) {
    sys.puts('received '+request.method+' '+request.url + "\n"+JSON.stringify(request.headers));
    if(/^\/node/.test(request.url)) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.end("Hi, it's node =)\n");
    }
    else if(/^\/exit/.test(request.url)) {
        sys.puts('closing..\n');
        MainServer.close();
        throw new Error('forced');
    }
    else {
        pass_to_apache(request, response);
    }
});

MainServer.listen(80, 'typeofnan.com');

您可以在 www.typeofnan.com 上“看到”这个 Action 。 && www.typeofnan.com/node/anything
编辑:暂时禁用 nodejs。

请记住,如果没有使用 gzip/deflate Apache 。我试图在我的回复中将编码设置为“二进制”,但没有 要么成功。

我在这里遗漏了什么吗?有人可以确认这种行为吗? 我使用的是最新版本 (0.2.0)。

是否有其他(更好的)解决方案来使用这样的代理脚本?

最佳答案

我很感兴趣。我启动了您的代码并将其指向 www.typeofnan.com。它工作正常,但我注意到服务器没有返回压缩响应。因此,我将其设置为代理 apache.org,我的浏览器也使用 gzip 压缩内容呈现它!对于“/”上的 GET,我得到了以下响应 header :

{"date":"Mon, 13 Sep 2010 11:03:45 GMT","server":"Apache/2.3.8 (Unix) mod_ssl/2.3.8 OpenSSL/1.0.0a","last-modified":"Sat, 11 Sep 2010 19:38:09 GMT","etag":"\"c9489a-4ada-4900100c32a40-gzip\"","accept-ranges":"bytes","vary":"Accept-Encoding","content-encoding":"gzip","cache-control":"max-age=86400","expires":"Tue, 14 Sep 2010 11:03:45 GMT","content-length":"5359","keep-alive":"timeout=5, max=100","connection":"Keep-Alive","content-type":"text/html"}

嗯……我是不是运气太好了,没有得到导致您出现问题的 gzipped 响应?您是否有一个页面可以可靠地导致“奇怪的事情发生”,我可以对其进行测试?实际上,您可能需要定义“奇怪的事情将会发生”:)

作为 hack,您可以让您的代理更改 accept-encoding header ,这样 apache 将永远不会返回压缩的响应。将以下内容添加到您的 apache 请求中将强制 apache 返回未压缩的响应:

req.headers['accept-encoding'] = '*;q=1,gzip=0';

关于javascript - Nodejs 代理脚本,不适用于 mod_deflate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3642531/

相关文章:

javascript - 根据文本调整 Bootstrap Accordion 按钮高度

javascript - 无法使用ajax和php上传多个文件

.net - 如何在 .Net Core 控制台应用程序中配置代理

Python:如何使用 urllib 或从公司域(防火墙、代理、cntlm 等)请求模块

Java URLConnection NTLM 代理认证 - linux

javascript - 从多个 Controller 更新服务中的变量

javascript - 通过 Selenium 抓取动态内容?

python - 在用于 Python 开发的 Apache 2 中禁用缓存

php - PHP(Apache)的静态缓存?

java - org.apache.ws.commons.schema.XmlSchemaForm.schemaValueOf(Ljava/lang/String;)Lorg/apache/ws/commons/schema/XmlSchemaForm;