如何 gzip 我的 JavaScript 和 CSS 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2666120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:22:49  来源:igfitidea点击:

How can I gzip my JavaScript and CSS files?

javascriptcssapachehttpgzip

提问by Fribu - Smart Solutions

I have a problem, I have to gzip a prototype Lib, but i totaly have no idea how to do this, where to start and how does it works. :)

我有一个问题,我必须 gzip 一个原型 Lib,但我完全不知道如何做到这一点,从哪里开始以及它是如何工作的。:)

I find some tutorials but that wasn't helpful...

我找到了一些教程,但这没有帮助...

So I have a folder with my JS Files:

所以我有一个包含我的 JS 文件的文件夹:

/compressed/js/ 1.js 2.js 3.js

/压缩/js/ 1.js 2.js 3.js

I'm calling these files for a test in this file

我正在调用这些文件以在此文件中进行测试

/compresses/index.php

/压缩/index.php

<link rel="javascript" type="text/js" href="js/tabs.js" />
<link rel="javascript" type="text/js" href="js/fb.js" />

So what do I have to do? :)

那我该怎么办?:)

回答by elias

You can use apache's mod_deflateto automatically compress your files on the fly.

您可以使用 apache 的mod_deflate自动动态压缩文件。

Example:

例子:

AddOutputFilterByType DEFLATE text/html text/xml text/css text/javascript 

[edit]

[编辑]

To check if your apache server has already output compression enabled, put the example above into an .htaccess file. Then load an html or js file via the server and check the headers for "Content-Encoding", if it says gzip or deflate, it is enabled.

要检查您的 apache 服务器是否已启用输出压缩,请将上面的示例放入 .htaccess 文件中。然后通过服务器加载一个 html 或 js 文件并检查“Content-Encoding”的标题,如果它显示 gzip 或 deflate,则它已启用。

回答by Waiyl Karim

add this code to your .htaccess it will gzip all your css and js files.

将此代码添加到您的 .htaccess 它将 gzip 您所有的 css 和 js 文件。

# BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
# END GZIP

回答by Peter Wiseman

I above example similar to this worked great but didn't compress javascript. I needed to add application/javascript.

我上面类似的例子很好用,但没有压缩 javascript。我需要添加应用程序/javascript。

AddOutputFilterByType DEFLATE text/html text/xml text/css text/javascript application/javascript 

回答by Ratna

Hi If you are using Nginx serverthe above will not help. Please edit the with command vi /etc/nginx/nginx.confand add the below lines.

嗨,如果您使用的是Nginx 服务器,上述内容将无济于事。请编辑 with 命令vi /etc/nginx/nginx.conf并添加以下行。

gzip on;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types    text/plain text/html text/css
          application/x-javascript text/xml
          application/xml application/xml+rss
          text/javascript;

Restart the nginx with command /etc/init.d/nginx reload. It will compress the JS and CSS files.

使用命令重新启动 nginx /etc/init.d/nginx reload。它将压缩 JS 和 CSS 文件。

回答by kovshenin

Perhaps you should look at the mod_deflate module for Apache: http://httpd.apache.org/docs/2.0/mod/mod_deflate.html

也许你应该看看 Apache 的 mod_deflate 模块:http: //httpd.apache.org/docs/2.0/mod/mod_deflate.html

回答by Anil Namde

You need to handle this stuff using the configuration for HTTP server that you are using.

您需要使用您正在使用的 HTTP 服务器的配置来处理这些东西。

Could you please tell me which server you are using ? IIS/Apache ?

你能告诉我你使用的是哪个服务器吗?IIS/阿帕奇?

Following is linkfor compression with the IIS

以下是使用 IIS 进行压缩的链接