[nodejs] Problem with gzip when using nodejs as proxy to apache

147 views
Skip to first unread message

Floby

unread,
May 19, 2010, 7:49:27 PM5/19/10
to nodejs
Hello everybody,

I tried to use Nodejs as a proxy to Apache so it can serve everything
that is static while dynamic content is generated by node. The main
request handler decides which one should process the request based on
url patterns.

The problem is that, when using Firefox, apache sends back gzipped
content. I thought it wouldn't matter so I just made Node send every
chunk to the client. But Firefox says that there is an encoding error
and can't display the page.

Chromium and lynx are fine since they don't even send the Accept-
Encoding header (well, Chromium does but for some reason, Apache
doesn't encode the response).

Is there something I'm missing ?

here is a sample of my code:

var apache = http.createClient(80, 'localhost');
function passToApache(req, res) {
sys.puts('transfering to Apache: '+req.method+' '+req.url);
var request = apache.request(req.method, req.url, req.headers);

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

req.addListener('data', function(chunk) {
request.write(chunk);
});

request.addListener('response', function(response) {
sys.puts('received response');
sys.puts(''+response.statusCode+'
'+JSON.stringify(response.headers));
res.writeHead(response.statusCode, response.headers);
response.setEncoding('binary');
response.addListener('data', function(chunk) {
sys.puts('received data: '+chunk);
res.write(chunk);
});
response.addListener('end', function() {
sys.puts('end of request');
res.end();
});
});
}


var MainServer;
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 {
passToApache(request, response);
}
});

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

r...@tinyclouds.org

unread,
May 19, 2010, 9:05:54 PM5/19/10
to nod...@googlegroups.com
On Wed, May 19, 2010 at 4:49 PM, Floby <floren...@gmail.com> wrote:
> Hello everybody,
>
> I tried to use Nodejs as a proxy to Apache so it can serve everything
> that is static while dynamic content is generated by node. The main
> request handler decides which one should process the request based on
> url patterns.
>
> The problem is that, when using Firefox, apache sends back gzipped
> content. I thought it wouldn't matter so I just made Node send every
> chunk to the client. But Firefox says that there is an encoding error
> and can't display the page.
>
> Chromium and lynx are fine since they don't even send the Accept-
> Encoding header (well, Chromium does but for some reason, Apache
> doesn't encode the response).
>
> Is there something I'm missing ?

Remove the setEncoding('binary') to use raw buffers.

Joran Greef

unread,
May 20, 2010, 2:55:34 AM5/20/10
to nodejs
Are you shipping an Application Cache manifest file as well? There are
versions of Firefox that incorrectly give a Content Encoding error for
applications that use the HTML 5 Application Cache together with Gzip.
In this case, the Content Encoding error is not shown the first time
you load the app from the network, but only on subsequent loads when
it is loaded from the Application Cache. Fixed at least in Firefox
3.6+ (I have not managed to find the ticket).

Floby

unread,
May 22, 2010, 7:09:52 AM5/22/10
to nodejs
Hello again everybody

first, thanks for you answers. Removing the setEncoding call didn't do
anything, as well as any other value than binary. And Joran, I use
Firefox 3.6+ so I don't know if what you said is still applicable.
Anyway, I understood less than a half of what you suggested (I don't
know what an Application Cache Manifest is) so it seems a little too
much trouble.

So here's the solution I found, which doesn't quite solve the initial
node problem with gzip but solves what I wanted to do: use different
applications to serve static or dynamic content.
I just inverted everything, so now Apache is the front request
processor and just pass to nodejs what has to be generated. It is
really easy to do with mod_proxy enabled.
It is also possible (even easier I think) with nginx.
Reply all
Reply to author
Forward
0 new messages