Apache的Mod_rewrite学习(五)

今天主要列出一些例子。由于有些例子是针对特殊路径或特别情况的,列出供大家在思路上参考。因为它们就是些例子。

目标 重写设置 说明
规范化URL RewriteRule ^/~([^/>+)/?(.*) /u/$1/$2 [R] 将/~user重写为/u/user的形式
RewriteRule ^/([uge])/([^/>+)$ /$1/$2/ [R] 将/u/user末尾漏掉的/补上
规范化HostName RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC] 域名不合格
RewriteCond %{HTTP_HOST} !^$ 不空
RewriteCond %{SERVER_PORT} !^80$ 不是80端口
RewriteRule ^/(.*) http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R] 重写
RewriteCond %{HTTP_HOST} !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://fully.qualified.domain.name/$1 [L,R]
URL根目录转移 RewriteEngine on
RewriteRule ^/$ /e/www/ [R] 从/移到/e/www/
末尾目录补斜线 RewriteEngine on
(目录范围内) RewriteBase /~quux/
RewriteRule ^foo$ foo/ [R] /~quux/foo是一个目录,补/
RewriteEngine on
RewriteBase /~quux/
RewriteCond %{REQUEST_FILENAME} -d 如果请文件名是个目录
RewriteRule ^(.+[^/>)$ $1/ [R] URL末尾不是斜线时补上
Web集群 RewriteEngine on
RewriteMap user-to-host txt:/path/to/map.user-to-host 用户-服务器映射
RewriteMap group-to-host txt:/path/to/map.group-to-host 组-服务器映射
RewriteMap entity-to-host txt:/path/to/map.entity-to-host 实体-服务器映射
RewriteRule ^/u/([^/>+)/?(.*) http://${user-to-host:$1|server0}/u/$1/$2 用户均衡
RewriteRule ^/g/([^/>+)/?(.*) http://${group-to-host:$1|server0}/g/$1/$2 组均衡
RewriteRule ^/e/([^/>+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2 实体均衡
RewriteRule ^/([uge])/([^/>+)/?$ /$1/$2/.www/
RewriteRule ^/([uge])/([^/>+)/([^.]+.+) /$1/$2/.www/$3\
URL根目录搬迁 RewriteEngine on
RewriteRule ^/~(.+) http://newserver/~$1 [R,L] 到其它服务器
所用户名首字母分 RewriteEngine on
RewriteRule ^/~(([a-z])[a-z0-9]+)(.*) /home/$2/$1/.www$3 内一层括号为$2
NCSA imagemap移 RewriteEngine on
植为mod_imap RewriteRule ^/cgi-bin/imagemap(.*) $1 [PT]
多目录查找资源 RewriteEngine on
# first try to find it in custom/...
RewriteCond /your/docroot/dir1/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/dir1/$1 [L]
# second try to find it in pub/...
RewriteCond /your/docroot/dir2/%{REQUEST_FILENAME} -f
RewriteRule ^(.+) /your/docroot/dir2/$1 [L]
# else go on for other Alias or ScriptAlias directives,
RewriteRule ^(.+) - [PT]
据URL设置环境变量 RewriteEngine on
RewriteRule ^(.*)/S=([^/>+)/(.*) $1/$3 [E=STATUS:$2]
虚拟主机 RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$ 基于用户名
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2
内外人有别 RewriteEngine on
RewriteCond %{REMOTE_HOST} !^.+\.ourdomain\.com$ 基于远程主机
RewriteRule ^(/~.+) http://www.somewhere.com/$1 [R,L]
错误重定向 RewriteEngine on
RewriteCond /your/docroot/%{REQUEST_FILENAME} !-f 不是regular文件
RewriteRule ^(.+) http://webserverB.dom/$1
程序处理特殊协议 RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \ Xredirect协议
[T=application/x-httpd-cgi,L]
最近镜像下载 RewriteEngine on
RewriteMap multiplex txt:/path/to/map.cxan 顶级域名与最近ftp服务器映射
RewriteRule ^/CxAN/(.*) %{REMOTE_HOST}::$1 [C]
RewriteRule ^.+\.([a-zA-Z]+)::(.*)$ ${multiplex:$1|ftp.default.dom}$2 [R,L] 据顶级域名不同提供不同的FTP服务器
基于时间重写 RewriteEngine on
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule ^foo\.html$ foo.day.html 白天为早晚7点间
RewriteRule ^foo\.html$ foo.night.html 其余为夜间
向前兼容扩展名 RewriteEngine on
RewriteBase /~quux/
# parse out basename, but remember the fact
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# rewrite to document.phtml if exists
RewriteCond %{REQUEST_FILENAME}.phtml -f 如果存在$1.phtml则重写
RewriteRule ^(.*)$ $1.phtml [S=1]
# else reverse the previous basename cutout
RewriteCond %{ENV:WasHTML} ^yes$ 如果不存在$1.phtml,则保持不变
RewriteRule ^(.*)$ $1.html
文件改名(目录级) RewriteEngine on 内部重写
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html
RewriteEngine on 重定向由客户端再次提交
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html [R]
据浏览器类型重写 RewriteCond %{HTTP_USER_AGENT} ^Mozilla/3.*
RewriteRule ^foo\.html$ foo.NS.html [L]
RewriteCond %{HTTP_USER_AGENT} ^Lynx/.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/[12].*
RewriteRule ^foo\.html$ foo.20.html [L]
RewriteRule ^foo\.html$ foo.32.html [L]
动态镜像远程资源 RewriteEngine on
RewriteBase /~quux/
RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P] 利用了代理模块
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^usa-news\.html$ http://www.quux-corp.com/news/index.html [P]
反向动态镜像 RewriteEngine on
RewriteCond /mirror/of/remotesite/$1 -U
RewriteRule ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1
负载均衡 RewriteEngine on 利用代理实现round-robin效果
RewriteMap lb prg:/path/to/lb.pl
RewriteRule ^/(.+)$ ${lb:$1} [P,L]
#!/path/to/perl
$| = 1;
$name = "www"; # the hostname base
$first = 1; # the first server (not 0 here, because 0 is myself)
$last = 5; # the last server in the round-robin
$domain = "foo.dom"; # the domainname
$cnt = 0;
while (<STDIN>) {
$cnt = (($cnt+1) % ($last+1-$first));
$server = sprintf("%s%d.%s", $name, $cnt+$first, $domain);
print "http://$server/$_";
}
##EOF##
静态页面变脚本 RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ foo.cgi [T=application/x-httpd-cgi]
阻击机器人 RewriteCond %{HTTP_USER_AGENT} ^NameOfBadRobot.*
RewriteCond %{REMOTE_ADDR} ^123\.45\.67\.[8-9]$
RewriteRule ^/~quux/foo/arc/.+ - [F]
阻止盗连你的图片 RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC] 自己的连接可不能被阻止
RewriteRule .*\.gif$ - [F]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !.*/foo-with-gif\.html$
RewriteRule ^inlined-in-foo\.gif$ - [F]
拒绝某些主机访问 RewriteEngine on
RewriteMap hosts-deny txt:/path/to/hosts.deny
RewriteCond ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^/.* - [F]
用户授权 RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$
RewriteRule ^/~quux/only-for-friends/ - [F]
外部重写程序模板 RewriteEngine on
RewriteMap quux-map prg:/path/to/map.quux.pl
RewriteRule ^/~quux/(.*)$ /~quux/${quux-map:$1}
#!/path/to/perl
$| = 1;
while (<>) {
s|^foo/|bar/|;
print $_;
}
搜索引擎友好 RewriteRule ^/products$ /content.php
RewriteRule ^/products/([0-9]+)$ /content.php?id=$1
RewriteRule ^/products/([0-9]+),([ad]*),([0-9]{0,3}),([0-9]*),([0-9]*$) /marso/content.php?id=$1&sort=$2&order=$3&start=$4

参考文献:
URL Rewriting Guide
http://httpd.apache.org/docs/misc/rewriteguide.html

mod_rewrite: A Beginner's Guide to URL Rewriting
http://www.sitepoint.com/article/910

找不到网卡的时候,修改了一下snmpd.conf就好了

http://blog.sina.com.cn/s/blog_6a03f09f0100xcf8.html

Cacti snmp找不到网卡的问题,请帮忙解决net-snmp版本5.0.9
编译配置:
./configure --with-default-snmp-version="3" --with-sys-contact="root@localhost" --with-sys-location="beijing" --with-logfile
="/var/log/snmpd.log" --with-persistent-directory="/var/net-snmp" --with-libwrap

snmp安装完成后,配置了一下snmpd.conf
内容为:

rocommunity abc_snmp
com2sec notConfigUserdefault abc_snmp
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.2
view systemview included .1.3.6.1.2.1.25.1.1
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.2
view systemview included .1.3.6.1.2.1.2.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
view systemview included .1.3.6.1.4.1.2021.10.1.3.1
view systemview included .1.3.6.1.4.1.2021.10.1.3.2
view systemview included .1.3.6.1.4.1.2021.10.1.3.3
view systemview included .1.3.6.1.4.1.2021.4.3.0
view systemview included .1.3.6.1.4.1.2021.4.4.0
view systemview included .1.3.6.1.4.1.2021.4.5.0
view systemview included .1.3.6.1.4.1.2021.4.6.0
view systemview included .1.3.6.1.4.1.2021.4.11.0
view all included .1 80
accessnotConfigGroup "" any noauth exactall none none
syslocation abc
syscontact abc@abcd
pass .1.3.6.1.4.1.4413.4.1 /usr/bin/ucd5820stat

然后把编译目录下/dist/snmpd-init.d cp到/etc/rc.d/init.d/snmpd
然后修改/etc/rc.d/init.d/snmpd
#!/bin/sh
#
# snmpd This shell script takes care of starting and stopping
# the net-snmp SNMP daemon
#
# chkconfig: - 26 74
# description: snmpd is net-snmp SNMP daemon.

# Source function library.
. /etc/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

RETVAL=0
name="snmpd"
prog="/usr/local/sbin/snmpd"

[ -x $prog -a -f /usr/local/share/snmp/snmpd.conf ] || exit 0

start() {
# Start daemons.
echo -n $"Starting $name: "
daemon $prog -Cc /etc/snmp/snmpd.conf
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$name
return $RETVAL
}

stop() {
# Stop daemons.
echo -n $"Shutting down $name: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$name
return $RETVAL
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $name
RETVAL=$?
;;
restart|reload)
stop
start
RETVAL=$?
;;
condrestart)
if [ -f /var/lock/subsys/$name ]; then
stop
start
RETVAL=$?
fi
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

exit $RETVAL

修改的内容主要是把启动时的命令添加了参数 -Cc /etc/snmp/

请建议vpn方案...

snmpd.conf 修改snmpd读取的配置文件

在cacti添加了这个服务器以后http://www.2uphone.com,取不到数据,不知道为什么,请高人指导http://www.vaexcellent.com!佳能600DCacti版本是0.8.6h for linux,数据获取方式是Cactid,Cactid默认编译.
我觉得问题应该不是Cactid,因为我以前用cmd.php取数据也是没有取到请看cacti下 的log/cacti.log查找问题你要抓的网卡是千M还是百M,是32位还是64位?
下一个新的net-snmp从新编译安装试试。
configure 时加个--enable-mfd-rewrites我现在严重怀疑是我的cacti端的net-snmp版本太低.正在重新编译5.4版的.希望有些奇迹. 我的网卡32位1000M的

shell> rpm --import http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
shell> vi /etc/yum.repos.d/utterramblings.repo

[utterramblings]
name=Jason's Utter Ramblings Repo
baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka

(3) 关闭yum fastestmirror 功能
shell> vi /etc/yum/pluginconf.d/fastestmirror.conf

enabled=0

介于机房不提供用户流量查询服务,故自行安装。

yum install mysql mysql-server php php-mysql zlib libpng freetype libjpeg fontconfig gd libxml2 rrdtool net-snmp net-snmp-utils net-snmp-devel cacti

 

以上便于记录

CentOS5(X86)  http://mirrors.sohu.com/fedora-epel/5/i386/epel-release-5-4.noarch.rpm

CentOS6(X64) http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm

http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-7.noarch.rpm

其他版本以此地址自行搜索即可

-----------------------------

CentOS 5.5 yum更新源推荐--163、搜狐、中科大

更新方法如下:

先进入yum源配置目录cd /etc/yum.repos.d备份系统自带的yum源
mv CentOS-Base.repo CentOS-Base.repo.save

下载其他更快的yum源
中科大的yum源:
wget http://centos.ustc.edu.cn/CentOS-Base.repo

163的yum源:
wget http://mirrors.163.com/.help/CentOS-Base-163.repo

sohu的yum源
wget http://mirrors.sohu.com/help/CentOS-Base-sohu.repo

更新玩yum源后,建议更新一下,使操作立即生效
yum makecache

近日服务器出现Disabling IRQ #xxx 的错误,根据网络搜索得知,是IRQ冲突所致。

通过修改 acpi=off noapic 仍然不时出现网络ping值增高到100ms左右的现象。

换块网卡,治标治本。

或者,尝试通过重启网卡可临时解决,但仍会出现异常。

所以写了如下脚本,当ping一个ip延迟超过10ms的时候,则判断irq冲突,网卡异常,则重启网卡。

放到crontab里,每隔5分钟检查一次。

*/5 * * * * /check_irq.sh

#!/bin/sh
d=`ping -c 1 192.168.1.100 |awk -F '[= ]+' '/^64/{print $((NF-1))}'`
#echo $d
if [[ $d > 10 ]]; then
#重启网卡
/sbin/ifconfig eth1 down
/sbin/ifconfig eth1 up
fi

原先在hyper-v下安装linux,只能搭配旧的网络适配器,不支持hyper-v内建的。所以我们需要安装Linux Integration Services。

Linux Integration Services 3.2下载地址

http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=216de3c4-f598-4dff-8a4e-257d4b7a1c12

用hyper-v管理工具加载Linux IC v3.2.iso,将里面的文件复制到一个目录下,如:/usr/local/data-original/cdrom

执行./install.sh

安装完毕后会提示reboot,我们这时执行halt,关闭服务器。

然后我们需要对虚拟机进行设置,移除旧的网络适配器,加入新的,这里必须选择手动指定MAC地址,记住这个MAC地址,一会儿要用到。

开启虚拟机

vi /etc/sysconfig/network-scripts/ifcfg-eth0

我们要注意一点,DEVICE这里要填seth0,HWADDR填写刚刚在hyper-v管理器中手动指定的MAC地址。如下:

DEVICE="seth0"
HWADDR="<Default MAC address>"
NM_CONTROLLED="no"
ONBOOT="yes"
TYPE=Ethernet
NAME="Seth0"
IPADDR="<IP Address>"
NETMASK="<Subnet Mask>"
GATEWAY="<Gatewat IP>"

好了,保存后,就可以重启了。

然后就没有然后了。

When installed on a virtual machine that is running a supported Linux operating system, Linux Integration Services for Hyper-V provides the following functionality:

  • Driver support for synthetic devices: Linux Integration Services supports the synthetic network controller and the synthetic storage controller that were developed specifically for Hyper-V.
  • Fastpath Boot Support for Hyper-V: Boot devices now take advantage of the block Virtualization Service Client (VSC) to provide enhanced performance.
  • Timesync: The clock inside the virtual machine will remain synchronized with the clock on the host.
  • Integrated Shutdown: Virtual machines running Linux can be shut down from either Hyper-V Manager or System Center Virtual Machine Manager, using the "Shut Down" command.
  • Symmetric Multi-Processing (SMP) Support: Supported Linux distributions can use up to 4 virtual processors (VP) per virtual machine.

 

1. Download Linux Integration Components v2.1

a. Download Linux Integration Components v2.1 from Microsoft Download site.

b. Extract the .exe file into a temp folder. You will need only LinuxIC v21.iso file.

c. Place the LinuxIC v21.iso file into your VM host server where you can mount the ISO as a CDROM for your Centon Virtual Machine.

 

2. Installation

You will need Development Tools installed to be able to compile the Integration Components. You could do that by running yum:

yum groupinstall "Development Tools"

NOTE: This command will try to use your network connection to download and if you used Synthetic Network Adapter for your Hyper-V machine you will not have internet connection. So make sure to install Development Tools during your Centos initial installation.

Now make sure you added LinuxIC v21.iso file to your Hyper-V CDROM as Image.

As the root user, mount the CD in the virtual machine by issuing the following commands at a shell prompt:

mkdir /mnt/cdrom
mount /dev/cdrom /mnt/cdrom

Copy Linux Integration Services to the virtual machine and unmount CDROM that no longer needed:

mkdir /opt/linux_ic_v21_rtm
cp –R /mnt/cdrom/* /opt/linux_ic_v21_rtm
umount /mnt/cdrom

As the root user, run the following command to compile and install the synthetic drivers:

cd /opt/linux_ic_v21_rtm/
make
make install

NOTE: If you're using the x64 version of Centos, you have perform an extra step.

转:http://hi.baidu.com/%C0%B4%C1%BD%B8%F9%CF%E3%B3%A6/blog/item/a59a610fa26207ed37d12224.html

各文档所在位置
日志文件 /var/log/varnish/varnish.log
可执行程序 /usr/local/varnish/bin
缓冲文件 /var/vcache
配置文件 /usr/local/varnish/vcl.conf
启动参数 /etc/sysconfig/varnish
启动脚本 /etc/rc.d/init.d/varnish
/etc/rc.d/init.d/varnishlog

创建www用户和组,以及Varnish缓存文件存放目录(/var/vcache):
/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www
mkdir -p /var/vcache
chmod +w /var/vcache
chown -R www:www /var/vcache

创建日志文件,并授予www用户权限访问
mkdir -p /var/log/varnish
chmod +w /var/log/varnish
chown -R www:www /var/log/varnish

可能需要安装如下包
[CENTOS]yum install ncurses-devel
[UBUNTU]apt-get install libncurses5-dev

编译安装
./configure –prefix=/usr/local/varnish
make
make install

转:http://www.uplook.cn/kbase-Index-show-view10393.html

#!/bin/bash
# BY kerryhu
# MAIL:king_819@163.com
# BLOG:http://kerry.blog.51cto.com
# Please manual operation yum of before Operation.....
#============================ 更新系统时间 ============================
yum install -y ntp
ntpdate time.nist.gov
echo "00 01 * * * ntpdate time.nist.gov" >> /etc/crontab

#============================ Varnish安装 =============================

如果是RedHat/CentOS系统,在安装varnish的时候首先要安装以下软件包
automake
autoconf
libtool
ncurses-devel
libxslt
groff
pcre-devel
pkgconfig

groupadd www
useradd www -g www -s /sbin/nologin
mkdir -p /data/varnish/{cache,logs}
chmod +w /data/varnish/{cache,logs}
chown -R www:www /data/varnish/{cache,logs}
cd /opt
yum install -y automake autoconf libtool ncurses-devel libxslt groff pcre-devel pkgconfig
wget http://sourceforge.net/projects/varnish/files/varnish/2.1.3/varnish-2.1.3.tar.gz/download
tar -zxvf varnish-2.1.3.tar.gz
cd varnish-2.1.3
./configure --prefix=/usr/local/varnish
make;make install