nginx+php+minify 压缩你的css、js 详解

Minify 用于服务器端的JavaScript 和 CSS的合并压缩。


1. 首先从 Google code下载 Minify

http://code.google.com/p/minify/wiki/UserGuide 


2. 上传至网站根目录下,当然,也可以你所指定的位置,但是需要注意的是需要修改Minify。


3. 修改Nginx配置,可以参见我前面的文章:

Minify在Nginx上的rewrite配置 


 P.S. 添加重定向目的在于合并压缩后的js和css代码路径不携带?

 

4.  修改Minify配置文件

 

[php]  view plain copy
  1. <?php  
  2.   
  3. /** 
  4.  
  5.  * Configuration for default Minify application 
  6.  
  7.  * @package Minify 
  8.  
  9.  */  
  10.   
  11.    
  12.   
  13.    
  14.   
  15. /** 
  16.  
  17.  * In 'debug' mode, Minify can combine fileswith no minification and 
  18.  
  19.  * add comments to indicate line #s of theoriginal files. 
  20.  
  21.  * 
  22.  
  23.  * To allow debugging, set this option to trueand add "&debug=1" to 
  24.  
  25.  * a URI. E.g./min/?f=script1.js,script2.js&debug=1 
  26.  
  27.  */  
  28.   
  29. $min_allowDebugFlag= false;  
  30.   
  31.    
  32.   
  33.    
  34.   
  35. /** 
  36.  
  37.  * Set to true to log messages to FirePHP(Firefox Firebug addon). 
  38.  
  39.  * Set to false for no error logging (Minifymay be slightly faster). 
  40.  
  41.  * @link http://www.firephp.org/ 
  42.  
  43.  * 
  44.  
  45.  * If you want to use a custom error logger,set this to your logger 
  46.  
  47.  * instance. Your object should have a methodlog(string $message). 
  48.  
  49.  * 
  50.  
  51.  * @todo cache system does not have errorlogging yet. 
  52.  
  53.  */  
  54.   
  55. $min_errorLogger =false;  
  56.   
  57.    
  58.   
  59.    
  60.   
  61. /** 
  62.  
  63.  * Allow use of the Minify URI Builder app. Ifyou no longer need 
  64.  
  65.  * this, set to false. 
  66.  
  67.  **/  
  68.   
  69. $min_enableBuilder =true;  
  70.   
  71.    
  72.   
  73.    
  74.   
  75. /** 
  76.  
  77.  * For best performance, specify your tempdirectory here. Otherwise Minify 
  78.  
  79.  * will have to load extra code to guess. Someexamples below: 
  80.  
  81.  */  
  82.   
  83. //$min_cachePath ='c:\\WINDOWS\\Temp';  
  84.   
  85. $min_cachePath'/tmp';  
  86.   
  87. //$min_cachePath =preg_replace('/^\\d+;/', '', session_save_path());  
  88.   
  89.    
  90.   
  91.    
  92.   
  93. /** 
  94.  
  95.  * Leave an empty string to use PHP's$_SERVER['DOCUMENT_ROOT']. 
  96.  
  97.  * 
  98.  
  99.  * On some servers, this value may bemisconfigured or missing. If so, set this 
  100.  
  101.  * to your full document root path with notrailing slash. 
  102.  
  103.  * E.g. '/home/accountname/public_html' or'c:\\xampp\\htdocs' 
  104.  
  105.  * 
  106.  
  107.  * If /min/ is directly inside your documentroot, just uncomment the 
  108.  
  109.  * second line. The third line might work onsome Apache servers. 
  110.  
  111.  */  
  112.   
  113. $min_documentRoot'';  
  114.   
  115. //$min_documentRoot =substr(__FILE__, 0, strlen(__FILE__) - 15);  
  116.   
  117. //$min_documentRoot =$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'];  
  118.   
  119. //$min_documentRoot ='/var/www/cychai/Cache/';  
  120.   
  121.    
  122.   
  123.    
  124.   
  125. /** 
  126.  
  127.  * Cache file locking. Set to false iffilesystem is NFS. On at least one 
  128.  
  129.  * NFS system flock-ing attempts stalled PHPfor 30 seconds! 
  130.  
  131.  */  
  132.   
  133. $min_cacheFileLocking= true;  
  134.   
  135.    
  136.   
  137.    
  138.   
  139. /** 
  140.  
  141.  * Combining multiple CSS files can place@import declarations after rules, which 
  142.  
  143.  * is invalid. Minify will attempt to detectwhen this happens and place a 
  144.  
  145.  * warning comment at the top of the CSSoutput. To resolve this you can either 
  146.  
  147.  * move the @imports within your CSS files, orenable this option, which will 
  148.  
  149.  * move all @imports to the top of the output.Note that moving @imports could 
  150.  
  151.  * affect CSS values (which is why this optionis disabled by default). 
  152.  
  153.  */  
  154.   
  155. $min_serveOptions['bubbleCssImports']= false;  
  156.   
  157.    
  158.   
  159.    
  160.   
  161. /** 
  162.  
  163.  * Maximum age of browser cache in seconds.After this period, the browser 
  164.  
  165.  * will send another conditional GET. Use alonger period for lower traffic 
  166.  
  167.  * but you may want to shorten this beforemaking changes if it's crucial 
  168.  
  169.  * those changes are seen immediately. 
  170.  
  171.  * 
  172.  
  173.  * Note: Despite this setting, if you include anumber at the end of the 
  174.  
  175.  * querystring, maxAge will be set to one year.E.g. /min/f=hello.css&123456 
  176.  
  177.  */  
  178.   
  179. $min_serveOptions['maxAge']= 315360000;  
  180.   
  181.    
  182.   
  183.    
  184.   
  185. /** 
  186.  
  187.  * If you'd like to restrict the "f"option to files within/below 
  188.  
  189.  * particular directories below DOCUMENT_ROOT,set this here. 
  190.  
  191.  * You will still need to include the directoryin the 
  192.  
  193.  * f or b GET parameters. 
  194.  
  195.  * 
  196.  
  197.  * // = shortcut for DOCUMENT_ROOT 
  198.  
  199.  */  
  200.   
  201. //$min_serveOptions['minApp']['allowDirs']= array('//js', '//css');  
  202.   
  203.    
  204.   
  205. /** 
  206.  
  207.  * Set to true to disable the "f" GETparameter for specifying files. 
  208.  
  209.  * Only the "g" parameter will beconsidered. 
  210.  
  211.  */  
  212.   
  213. $min_serveOptions['minApp']['groupsOnly']= false;  
  214.   
  215.    
  216.   
  217. /** 
  218.  
  219.  * Maximum # of files that can be specified inthe "f" GET parameter 
  220.  
  221.  */  
  222.   
  223. $min_serveOptions['minApp']['maxFiles']= 50;  
  224.   
  225.    
  226.   
  227.    
  228.   
  229. /** 
  230.  
  231.  * If you minify CSS files stored in symlink-eddirectories, the URI rewriting 
  232.  
  233.  * algorithm can fail. To prevent this, providean array of link paths to 
  234.  
  235.  * target paths, where the link paths arewithin the document root. 
  236.  
  237.  * 
  238.  
  239.  * Because paths need to be normalized for thisto work, use "//" to substitute 
  240.  
  241.  * the doc root in the link paths (the arraykeys). E.g.: 
  242.  
  243.  * <code> 
  244.  
  245.  * array('//symlink' => '/real/target/path')// unix 
  246.  
  247.  * array('//static' =>'D:\\staticStorage')  // Windows 
  248.  
  249.  * </code> 
  250.  
  251.  */  
  252.   
  253. $min_symlinks =array();  
  254.   
  255.    
  256.   
  257.    
  258.   
  259. /** 
  260.  
  261.  * If you upload files from Windows to anon-Windows server, Windows may report 
  262.  
  263.  * incorrect mtimes for the files. This maycause Minify to keep serving stale 
  264.  
  265.  * cache files when source file changes aremade too frequently (e.g. more than 
  266.  
  267.  * once an hour). 
  268.  
  269.  * 
  270.  
  271.  * Immediately after modifying and uploading afile, use the touch command to 
  272.  
  273.  * update the mtime on the server. If the mtimejumps ahead by a number of hours, 
  274.  
  275.  * set this variable to that number. If themtime moves back, this should not be 
  276.  
  277.  * needed. 
  278.  
  279.  * 
  280.  
  281.  * In the Windows SFTP client WinSCP, there'san option that may fix this 
  282.  
  283.  * issue without changing the variable below.Under login > environment, 
  284.  
  285.  * select the option "Adjust remotetimestamp with DST". 
  286.  
  287.  * @linkhttp://winscp.net/eng/docs/ui_login_environment#daylight_saving_time 
  288.  
  289.  */  
  290.   
  291. $min_uploaderHoursBehind= 0;  
  292.   
  293.    
  294.   
  295.    
  296.   
  297. /** 
  298.  
  299.  * Path to Minify's lib folder. If you happento move it, change 
  300.  
  301.  * this accordingly. 
  302.  
  303.  */  
  304.   
  305. $min_libPath =dirname(__FILE__) . '/lib';  
  306.   
  307.    
  308.   
  309.    
  310.   
  311. // try to disableoutput_compression (may not have an effect)  
  312.   
  313. ini_set('zlib.output_compression','0');  


5. 重启Nginx服务。


6. 使用Minify其它页面的JavaScript文件,src地址格式为 /min/f=js/[javascript文件名]

 

如:

<scripttype="text/javascript" src="/min/f=js/common.js"></script>

 

对于合并多个JavaScript文件,可以通过Minify服务进行合并:

开发环境访问地址:

http://localhost/min/builder/

填写需要合并的JavaScript文件,Update后,会生成请求的URI地址,在相应页面引用即可。



参考:

在服务端合并和压缩JavaScript和CSS文件

Minify优化网站:合并多个CSS或者js文件


更多配置CookBook: http://code.google.com/p/minify/wiki/CookBook 




前提:服务器上nginx和php都已经装了,所以要做两步工作:让nginx支持php和安装minify。


1.让nginx支持php

安装php5-fpm:

ubuntu有的版本是没有php-fpm的源的,所以要把它加到apt的源里面

[html]  view plain copy
  1. sudo vi /etc/apt/source.list  
  2.   
  3. deb http://ppa.launchpad.net/jdub/devel/ubuntu maverick main  
  4. deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main  
  5. deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main  

安装

[html]  view plain copy
  1. sudo apt-get update  
  2. sudo apt-get install php5-fpm  

启动

[html]  view plain copy
  1. sudo /etc/init.d/php5-fpm start  

然后是修改nginx的配置文件:

/etc/nginx/fastcgi_params

加入这行(有的nginx已经有了,就不用加了):

[html]  view plain copy
  1. fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;  

在你的某个要用的虚拟主机里面加入以下代码,我的是/etc/nginx/conf.d/mysite.conf

[html]  view plain copy
  1. location ~ \.php$ {  
  2.                 fastcgi_pass 127.0.0.1:9000;  
  3.                 fastcgi_index index.php;  
  4.                 include /etc/nginx/fastcgi_params;  
  5.         }  

然后重启nginx,就可以了。

可以在你的网站根目录放个phpinfo的文件,验证以下nginx支持php是否成功:

info.php

[html]  view plain copy
  1. <?php  
  2. // Show all information, defaults to INFO_ALL  
  3. phpinfo();  
  4. ?>  

2.安装minify

这一步就很简单了,minify已经加入google code,到它的google code主页就能下载:http://code.google.com/p/minify/

下载地址:http://minify.googlecode.com/files/minify_2.1.4_beta.zip

然后解压缩,把其中的min目录放到你的网站根目录就可以了


3.minify简单使用说明

这个网上资料很多,一般这样访问你的css,假如你有个css:http://www.abc.com/style/global.css,那么你应该这样访问:

[html]  view plain copy
  1. http://www.abc.com/min/index.php?f=/style/global.css  
"f="后面的地址才是你的真实文件路径

但是配合nginx可以改进一下,把这条rewrite规则加到nginx的配置文件中:

[html]  view plain copy
  1. location / {  
  2.   
  3.                 rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;  
  4. ......  
  5.         }  

然后你可以这样访问了:

[html]  view plain copy
  1. http://www.abc.com/min/f=/style/global.css  
[html]  view plain copy
  1. http://www.abc.com/min/f=/style/global.css,/style/index.css,/css/abc.css  

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值