简体   繁体   English

我怎么能gzip我的JavaScript和CSS文件?

[英]How can I gzip my JavaScript and CSS files?

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 / compressed / js / 1.js 2.js 3.js

I'm calling these files for a test in this file 我在这个文件中调用这些文件进行测试

/compresses/index.php /compresses/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? 那我该怎么办? :) :)

You can use apache's mod_deflate to 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. 要检查您的apache服务器是否已启用输出压缩,请将上面的示例放入.htaccess文件中。 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. 然后通过服务器加载html或js文件并检查标题为“Content-Encoding”,如果它说gzip或deflate,则启用它。

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

I above example similar to this worked great but didn't compress javascript. 我上面的例子类似于这个工作很好,但没有压缩javascript。 I needed to add application/javascript. 我需要添加application / javascript。

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

Hi If you are using Nginx server the above will not help. 嗨如果您使用的是Nginx服务器,则上述功能无济于事。 Please edit the with command vi /etc/nginx/nginx.conf and add the below lines. 请使用命令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 . 使用命令/etc/init.d/nginx reload重新启动nginx。 It will compress the JS and CSS files. 它将压缩JS和CSS文件。

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

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 / Apache?

Following is link for compression with the IIS 以下是与IIS压缩的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM