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

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


1.让nginx支持php

安装php5-fpm:

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

sudo vi /etc/apt/source.list

deb http://ppa.launchpad.net/jdub/devel/ubuntu maverick main
deb http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main
deb-src http://ppa.launchpad.net/brianmercer/php/ubuntu lucid main

安装

sudo apt-get update
sudo apt-get install php5-fpm

启动

sudo /etc/init.d/php5-fpm start

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

/etc/nginx/fastcgi_params

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

fastcgi_param   SCRIPT_FILENAME         $document_root$fastcgi_script_name;

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

location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;
        }

然后重启nginx,就可以了。

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

info.php

<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>

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,那么你应该这样访问:

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

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

location / {

                rewrite ^/min/([a-z]=.*) /min/index.php?$1 last;
......
        }

然后你可以这样访问了:

http://www.abc.com/min/f=/style/global.css
http://www.abc.com/min/f=/style/global.css,/style/index.css,/css/abc.css


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值