sub_filter 由于 gzip 不能插入内容

本文介绍如何使用Nginx的sub_filter模块在不修改应用程序的情况下为返回文件添加额外内容,例如监控标志或统计数据收集的JavaScript代码。文章还探讨了解决gzip压缩问题的方法。

Nginx 的 sub_filter 模块(http://wiki.nginx.org/HttpSubModule)来替换返回文件中的文本。可以用来不修改应用程序的同时,为文件增加一些监控标志,或增加额外的 javascript 用于数据统计等,使用方式如下:

location / {
   sub_filter      < /head >
   '</head><script language="javascript" src="$script"></script>' ;
   sub_filter_once on;
}

当然方式可以更灵活,比如插入 google analytics 代码等等。

但如果后端返回的文件是已经 gzip 压缩过的文件,因为需要解压缩,然后再压缩,sub_filter 不支持gzip。为了避免此种情况,我们需要后端不压缩文件,做法就是去除 HTTP 请求头中的 压缩头,指导后端不压缩:

     
proxy_set_header Accept-Encoding "" ;

当然,为了保证到浏览器的数据是压缩的,sub_filter 前端还是需要配置 gzip on 的。


via: http://www.w3c.com.cn/sub_filter-由于-gzip-不能插入内容

<?php /** * 鸿宇多用户商城 动态内容函数库 - 已修复兼容 PHP 5.6 * ============================================================================ * 版权所有 2005-2010 鸿宇多用户商城科技有限公司,并保留所有权利。 * 网站地址: http://bbs.hongyuvip.com; * ---------------------------------------------------------------------------- * 仅供学习交流使用,如需商用请购买正版版权。鸿宇不承担任何法律责任。 * 踏踏实实做事,堂堂正正做人。 * ============================================================================ * $Author: liuhui $ * $Id: lib_insert.php 17063 2010-03-25 06:35:46Z liuhui $ */ if (!defined('IN_ECS')) { die('Hacking attempt'); } /** * 获得查询次数以及查询时间 * * @access public * @return string */ function insert_query_info() { if (empty($GLOBALS['db']->queryTime)) { $query_time = 0; } else { if (PHP_VERSION >= '5.0.0') { $start = microtime(true); $query_time = number_format($start - $GLOBALS['db']->queryTime, 6); } else { list($now_usec, $now_sec) = explode(' ', microtime()); list($start_usec, $start_sec) = explode(' ', $GLOBALS['db']->queryTime); $query_time = number_format(($now_sec - $start_sec) + ($now_usec - $start_usec), 6); } } /* 内存占用情况 */ $memory_usage = ''; if ($GLOBALS['_LANG']['memory_info'] && function_exists('memory_get_usage')) { $memory_usage = sprintf($GLOBALS['_LANG']['memory_info'], memory_get_usage() / 1048576); } /* 是否启用了 gzip */ $gzip_enabled = gzip_enabled() ? $GLOBALS['_LANG']['gzip_enabled'] : $GLOBALS['_LANG']['gzip_disabled']; $online_count = intval($GLOBALS['db']->getOne("SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('sessions'))); /* 加入触发cron代码 */ $cron_method = empty($GLOBALS['_CFG']['cron_method']) ? '<img src="api/cron.php?t=' . gmtime() . '" alt="" style="width:0px;height:0px;" />' : ''; return sprintf($GLOBALS['_LANG']['query_info'], $GLOBALS['db']->queryCount, $query_time, $online_count) . $gzip_enabled . $memory_usage . $cron_method; } /** * 调用浏览历史 * * @access public * @return string */ function insert_history() { $str = ''; if (!empty($_COOKIE['ECS']['history'])) { $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return ''; } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 5"; $res = $GLOBALS['db']->query($sql); while ($row = $GLOBALS['db']->fetch_array($res)) { $goods = array(); $goods['goods_id'] = $row['goods_id']; $goods['goods_name'] = htmlspecialchars($row['goods_name']); $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name']; $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $goods['shop_price'] = price_format($row['shop_price']); $goods['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); $str .= '<li> <div class="p-img"><a target="_blank" href="' . $goods['url'] . '"> <img src="' . $goods['goods_thumb'] . '" alt="' . $goods['goods_name'] . '" width="50" height="50" /> </a></div> <div class="p-name"><a target="_blank" href="' . $goods['url'] . '">' . $goods['goods_name'] . '</a></div> <div class="p-price"><strong>' . $goods['shop_price'] . '</strong></div> </li>'; } } return $str; } /** * 获取“猜你喜欢”商品列表(基于浏览历史) * * @return array */ function get_cainixihuan() { if (empty($_COOKIE['ECS']['history'])) { return array(); } $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return array(); } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT cat_id FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 1"; $cat_id = intval($GLOBALS['db']->getOne($sql)); if (!$cat_id) { $cat_id = 0; } $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('goods') . " WHERE cat_id = '$cat_id' AND is_best = 1 AND is_delete = 0 LIMIT 8"; $list = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($list as $key => $row) { $arr[$key]['goods_id'] = $row['goods_id']; $arr[$key]['goods_name'] = $row['goods_name']; $arr[$key]['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $arr[$key]['shop_price'] = price_format($row['shop_price']); $arr[$key]['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); $arr[$key]['evaluation'] = get_evaluation_sumss($row['goods_id']); } return $arr; } /** * 获取商品评论总数 * * @param int $goods_id * @return int */ function get_evaluation_sumss($goods_id) { $goods_id = intval($goods_id); $sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('comment') . " WHERE status=1 AND comment_type=0 AND id_value='$goods_id'"; return intval($GLOBALS['db']->getOne($sql)); } /* 代码增加_start By bbs.hongyuvip.com */ /** * 调用浏览历史列表(带比价按钮) * * @access public * @return string */ function insert_history_list() { $str = ''; if (!empty($_COOKIE['ECS']['history'])) { $goods_ids = array_filter(array_map('intval', explode(',', $_COOKIE['ECS']['history']))); if (empty($goods_ids)) { return ''; } $where = db_create_in($goods_ids, 'goods_id'); $sql = 'SELECT goods_id, goods_name, goods_thumb, shop_price, promote_price, market_price, goods_type FROM ' . $GLOBALS['ecs']->table('goods') . " WHERE $where AND is_on_sale = 1 AND is_alone_sale = 1 AND is_delete = 0 LIMIT 10"; $res = $GLOBALS['db']->query($sql); $str .= '<ul>'; while ($row = $GLOBALS['db']->fetch_array($res)) { $goods = array(); $goods['goods_id'] = $row['goods_id']; $goods['goods_name'] = htmlspecialchars($row['goods_name']); $goods['goods_type'] = $row['goods_type']; $goods['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name']; $goods['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); $goods['shop_price'] = price_format($row['shop_price']); $goods['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']); // 会员价 or 市场价 if ($_SESSION['user_id'] > 0) { $showprice = !empty($row['promote_price']) ? price_format($row['promote_price']) : $goods['shop_price']; } else { $showprice = price_format($row['market_price']); } $str .= '<li><div class="item_wrap"> <div class="dt"><a href="' . $goods['url'] . '"><img width="50" height="50" src="' . $goods['goods_thumb'] . '" /></a></div> <div class="dd"> <a class="name" href="' . $goods['url'] . '">' . $goods['goods_name'] . '</a> <div class="btn" style="padding-top:15px;"> <a class="compare-btn" data-goods="' . $goods['goods_id'] . '" onClick="Compare.add(' . $goods['goods_id'] . ',\'' . addslashes($goods['goods_name']) . '\',' . $goods['goods_type'] . ', \'' . $goods['goods_thumb'] . '\', \'' . $showprice . '\')"></a> <span class="price" style="float:left"><strong>' . $showprice . '</strong></span> </div> </div> </div></li>'; } $str .= '</ul>'; } return $str; } /* 代码增加_end By bbs.hongyuvip.com */ /** * 调用购物车信息 * * @access public * @return string */ function insert_cart_info() { $sess_id = SESS_ID; $sql_where = $_SESSION['user_id'] > 0 ? "c.user_id = '" . intval($_SESSION['user_id']) . "'" : "c.session_id = '$sess_id' AND c.user_id = 0"; $sql = 'SELECT c.*, IF(c.extension_code = "package_buy", pa.act_name, g.goods_name) AS goods_name, ' . 'IF(c.extension_code = "package_buy", "package_img", g.goods_thumb) AS goods_thumb, ' . 'g.goods_id, c.goods_number, c.goods_price ' . 'FROM ' . $GLOBALS['ecs']->table('cart') . ' AS c ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON g.goods_id = c.goods_id ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('goods_activity') . ' AS pa ON pa.act_id = c.goods_id ' . "WHERE $sql_where AND rec_type = '" . CART_GENERAL_GOODS . "'"; $rows = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($rows as $k => $v) { $arr[$k]['goods_thumb'] = get_image_path($v['goods_id'], $v['goods_thumb'], true); $arr[$k]['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($v['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $v['goods_name']; $arr[$k]['url'] = ($v['extension_code'] == 'package_buy') ? '' : build_uri('goods', array('gid' => $v['goods_id']), $v['goods_name']); $arr[$k]['goods_number'] = $v['goods_number']; $arr[$k]['goods_name'] = $v['goods_name']; $arr[$k]['goods_price'] = price_format($v['goods_price']); $arr[$k]['rec_id'] = $v['rec_id']; } $sql = 'SELECT SUM(goods_number) AS number, SUM(goods_price * goods_number) AS amount ' . 'FROM ' . $GLOBALS['ecs']->table('cart') . " AS c " . "WHERE $sql_where AND rec_type = '" . CART_GENERAL_GOODS . "'"; $row = $GLOBALS['db']->getRow($sql); $number = !empty($row['number']) ? intval($row['number']) : 0; $amount = !empty($row['amount']) ? floatval($row['amount']) : 0; $GLOBALS['smarty']->assign('str', sprintf($GLOBALS['_LANG']['cart_info'], $number, price_format($amount, false))); $GLOBALS['smarty']->assign('goods', $arr); $output = $GLOBALS['smarty']->fetch('library/cart_info.lbi'); return $output; } /* 代码增加_start By bbs.hongyuvip.com */ /** * 调用三方客服信息 * * @access public * @return string */ function insert_customer_service() { $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('chat_third_customer') . ' ORDER BY cus_type, cus_name'; $rows = $GLOBALS['db']->getAll($sql); $arr = array(); foreach ($rows as $k => $v) { $arr[$k]['cus_id'] = intval($v['cus_id']); $arr[$k]['cus_name'] = htmlspecialchars($v['cus_name']); $arr[$k]['cus_no'] = htmlspecialchars($v['cus_no']); $arr[$k]['cus_type'] = intval($v['cus_type']); $arr[$k]['is_master'] = intval($v['is_master']); } $GLOBALS['smarty']->assign('customer', $arr); $output = $GLOBALS['smarty']->fetch('library/customer_service.lbi'); return $output; } /* 代码增加_end By bbs.hongyuvip.com */ /** * 调用指定广告位的广告 * * @param array $arr 包含 id 和 num * @return string */ function insert_ads($arr) { static $static_res = null; $time = gmtime(); $position_id = intval($arr['id']); $num = isset($arr['num']) ? max(1, min(10, intval($arr['num']))) : 1; if ($num != 1) { $sql = "SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, p.ad_height, p.position_style, RAND() AS rnd " . "FROM " . $GLOBALS['ecs']->table('ad') . " AS a " . "LEFT JOIN " . $GLOBALS['ecs']->table('ad_position') . " AS p ON a.position_id = p.position_id " . "WHERE a.position_id = '$position_id' AND enabled = 1 AND start_time <= '$time' AND end_time >= '$time' " . "ORDER BY rnd LIMIT $num"; $res = $GLOBALS['db']->getAll($sql); } else { if (!isset($static_res[$position_id])) { $sql = "SELECT a.ad_id, a.position_id, a.media_type, a.ad_link, a.ad_code, a.ad_name, p.ad_width, p.ad_height, p.position_style, RAND() AS rnd " . "FROM " . $GLOBALS['ecs']->table('ad') . " AS a " . "LEFT JOIN " . $GLOBALS['ecs']->table('ad_position') . " AS p ON a.position_id = p.position_id " . "WHERE a.position_id = '$position_id' AND enabled = 1 AND start_time <= '$time' AND end_time >= '$time' " . "ORDER BY rnd LIMIT 1"; $static_res[$position_id] = $GLOBALS['db']->getAll($sql); } $res = $static_res[$position_id]; } $ads = array(); $position_style = ''; foreach ($res as $row) { if ($row['position_id'] != $position_id) continue; $position_style = $row['position_style']; switch ($row['media_type']) { case 0: // 图片广告 $src = strpos($row['ad_code'], 'http://') === 0 || strpos($row['ad_code'], 'https://') === 0 ? $row['ad_code'] : DATA_DIR . "/afficheimg/" . $row['ad_code']; $ads[] = "<a href='affiche.php?ad_id={$row['ad_id']}&uri=" . urlencode($row["ad_link"]) . "' target='_blank'>" . "<img src='$src' width='{$row['ad_width']}' height='{$row['ad_height']}' border='0' /></a>"; break; case 1: // Flash $src = strpos($row['ad_code'], 'http://') === 0 || strpos($row['ad_code'], 'https://') === 0 ? $row['ad_code'] : DATA_DIR . "/afficheimg/" . $row['ad_code']; $ads[] = "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width='{$row['ad_width']}' height='{$row['ad_height']}'> <param name='movie' value='$src'> <param name='quality' value='high'> <embed src='$src' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='{$row['ad_width']}' height='{$row['ad_height']}'></embed> </object>"; break; case 2: // CODE $ads[] = $row['ad_code']; break; case 3: // TEXT $ads[] = "<a href='affiche.php?ad_id={$row['ad_id']}&uri=" . urlencode($row["ad_link"]) . "' target='_blank'>" . htmlspecialchars($row['ad_code']) . '</a>'; break; } } if (empty($position_style)) { return ''; } $need_cache = $GLOBALS['smarty']->caching; $GLOBALS['smarty']->caching = false; $GLOBALS['smarty']->assign('ads', $ads); $val = $GLOBALS['smarty']->fetch("str:$position_style"); $GLOBALS['smarty']->caching = $need_cache; return $val; } /** * 调用会员信息 * * @access public * @return string */ function insert_member_info() { $need_cache = $GLOBALS['smarty']->caching; $GLOBALS['smarty']->caching = false; if ($_SESSION['user_id'] > 0) { $GLOBALS['smarty']->assign('user_info', get_user_info()); } else { if (!empty($_COOKIE['ECS']['username'])) { $GLOBALS['smarty']->assign('ecs_username', stripslashes($_COOKIE['ECS']['username'])); } $captcha = intval($GLOBALS['_CFG']['captcha']); if (($captcha & CAPTCHA_LOGIN) && ((!($captcha & CAPTCHA_LOGIN_FAIL)) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0) { $GLOBALS['smarty']->assign('enabled_captcha', 1); $GLOBALS['smarty']->assign('rand', mt_rand()); } } $output = $GLOBALS['smarty']->fetch('library/member_info.lbi'); $GLOBALS['smarty']->caching = $need_cache; return $output; } // 其他函数省略(结构相同),仅列出关键修复点... ?> 是这个文件吗
11-10
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值