expressjs / compression

Node.js compression middleware
MIT License
2.77k stars 242 forks source link

Cannot activate compression in production server #110

Closed FgfdCBVCbnsebtgf closed 7 years ago

FgfdCBVCbnsebtgf commented 7 years ago

Hello, In my developer computer, i can activate using :

app = express();
app.use(compression());

I can see in the http headers response: Content-Encoding:gzip

But in my production server it does not activate. I have the exact same version in both computer and the code is exactly the same: node 7.9.0 compression 1.6.2 express 4.15.2 The only diferrence is that the server is ubuntu server 16.04.02 LTs and my developer computer is Ubuntu desktop 16.10. I have other appliction in a difference server and there i can also use the compression without a problem. I do not understand why in this case it does not work. Can you help me? Thank you

FgfdCBVCbnsebtgf commented 7 years ago

I made this small app for testing:

var express = require('express'),
        app = express(),
        compression = require('compression');
app.use(compression());
app.get('/', function (pedido, resposta) {
    resposta.status(200).send(pedido.headers);
});
app.listen(8080);

If i run in my local computer, show that my browser is sending to the server (both local server as remote server): "accept-encoding":"gzip, deflate"

But the response is without gzip from local server and remote server. What am i doing wrong?

FgfdCBVCbnsebtgf commented 7 years ago

I have remove all node modules and even removed nodejs. Then i only installed node e expressjs and compression. The problem remains.

FgfdCBVCbnsebtgf commented 7 years ago

If i cnahge the testing application to:

var compression = require('compression'),
        express = require('express'),
        app = express();
app.use(compression());
app.get('/', function (pedido, resposta) {
    resposta.sendFile('p.html', {
        root: __dirname + "/../p/d"
    });
});
app.listen(8080);

then in my local computer the transfer uses gzip but in my production server keeps not using gzip.

FgfdCBVCbnsebtgf commented 7 years ago

I made some changes in the testing application:

var compression = require('compression'),
        express = require('express'),
        compressible = require("compressible"),
        app = express();
app.use(compression({filter: shouldCompress}));

function shouldCompress(req, res) {
    var type = res.get('Content-Type');
    console.log('Content-Type:' + type);
    if (type === undefined || !compressible(type)) {
        debug('%s not compressible', type);
        return false;
    }
    return true;
}

app.get('/', function (pedido, resposta) {
    resposta.set("Content-Type", "text/plain");
    resposta.send("olá");
});
app.get('/lixo', function (pedido, resposta) {
        resposta.sendFile('p.html', {
     root: __dirname + "/../p/d"
     });
});
app.listen(8080);

Now in my local computer the curl -i --compressed localhost:8080/lixo return the html file compressed. The curl -i --compressed localhost:8080/ returns olá uncompressed. In my production server, never returns with gzip compression!

dougwilson commented 7 years ago

Hi @FgfdCBVCbnsebtgf the reason why curl -i --compressed localhost:8080/ is not compressed in your example is because the result is only 3 bytes, below the cut-off threshold of 1kb (https://github.com/expressjs/compression#threshold).

As for your other route, I don't have your p.html file to try out, but I created my own and it's compressed both locally and on a variety of production servers I tried it on. Something is definitely going on, and there isn't a lot of information provided to figure out what is different between our setups, as I tried

(1) A fresh, brand new install of Ubuntu 16.04.02 (2) An install of node 7.9.0 from nodejs.org (3) compression 1.6.2 and express 4.15.2 (4) your exact code above

I then called it from another machine on the same network as the Ubuntu machine and then again from the other side of the globe (with the machine in Tokyo and the caller in London) and it was compressed every time.

Perhaps can you gather some packet captures of the response at various points in the flow to pinpoint what may be happening?

FgfdCBVCbnsebtgf commented 7 years ago

One difference: i use Ubuntu server 16.04.02 LTS I installed the testing script here: http://62.138.1.143:8080/ (keeps not sending in gzip). Made a wireshark capture of my ethernet while making the http request: www.op3racional.eu/capture.pcap Thank you.

dougwilson commented 7 years ago

Hi @FgfdCBVCbnsebtgf thanks for putting up that server. Both responses are not compressed, because as I said above the reason why curl -i --compressed localhost:8080/ is not compressed in your example is because the result is only 3 bytes, below the cut-off threshold of 1kb (https://github.com/expressjs/compression#threshold). It looks like your p.html file is also less than 1kb in size, which is why it's also not compressing from your server.

I hope that helps!