正确启用GZIP

时间:2017-05-22 06:03:11

标签: javascript node.js performance optimization gzip

我无法在我的网站上启用GZIP。它托管在nodejs服务器上。

它显示在本地测试时启用了gzip,但是当在服务器上部署时,我在dev工具中看不到响应头。以下是代码:

var compression = require('compression')
var express = require('express');
var app = express();
var server = require('http').createServer(app);

app.use(compression(9));
app.get('/', function(req, res){
  res.setHeader('Content-Encoding', 'gzip')
});

如果我进行了以下更改,我可以看到gzip已启用且响应标头也来自服务器,但我的许多内容无法加载错误,如"http://127.0.0.1:3000/lib/jquery-ui.js net::ERR_CONTENT_DECODING_FAILED"

var compression = require('compression')
var express = require('express');
var app = express();
var server = require('http').createServer(app);

app.use(compression(9));
app.use(function(req, res, next) {
  res.setHeader('Content-Encoding', 'gzip, deflate');
return next();
});

更新
来自终端的curl命令显示gzip已启用,但在浏览器上,响应标头不会出现

Curl->

curl -H "Accept-Encoding: gzip" -I https://<MY_WEB_URL>

输出 - &GT;

HTTP/1.1 200 OK
Server: nginx
Date: Mon, 22 May 2017 08:50:52 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Powered-By: Express
ETag: W/"15457-1492724210000"
Cache-Control: public, max-age=0
Last-Modified: Thu, 20 Apr 2017 21:36:50 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin: *
Content-Encoding: gzip

开发人员工具会将某些文件显示为gzip,而某些文件则不显示。
enter image description here

YSlow显示gzip未启用
enter image description here

启用GZIP ???

0 个答案:

没有答案