说到rewrite重写已经是众所周知的了,但是这里不单纯是rewrite,而是将uri改写成全部小写。这要归责于公司的历史遗留问题,因为公司要逐步从windows过度到linux,要一下子全部过度过去是不现实的,因为不仅仅是架构,还有web应用程序代码重构。由于公司流量剧增,iis已经显得有些吃力,甚至出现错误,而且nginx的出现改变了web,所以我们打算部署nginx服务器,但是由于当时是用asp开发的基于iis的web应用(我说的是静态文件,而非asp动态文件,asp在linux上运行也不配套啊,呵呵),所以没考虑文件和目录大小写问题,以至于酿成今日之祸。如果将程序迁移到linux里nginx上,会出现404,有些文件或目录无法访问,这显得方案不完美,这种情况下需要用到第三方nginx模块Lower Upper Case 或者使用lua脚本


架构如下:
192.168.18.249 windows iis asp
192.168.18.240 nginx1 前端
192.168.18.241 nginx2 后端,放置静态文件

1、准备工作
在nginx运行前我们需要拷贝 文件 ,把windows服务器上的静态文件拷贝到linux服务器上,同时将文件和目录全部转换为小写,可以按如下方法做:
将windows目标目录共享,在linux下mount, (在192.168.18.241上操作) 如:
[root@vm4 ~]# mount -o username=username,password=pwd //192.168.18.249/c/tmp/webroot /mnt
开始拷贝
[root@vm4 ~]# find /mnt/ | xargs -n1|while read s_name
do
d_name=$(echo /data/webroot/${s_name#/mnt/}|tr 'A-Z' 'a-z')
[ -d "$s_name" ] && install -d "$d_name" && continue
cp $s_name $d_name
chmod 644 $d_name
done
如果没有错误的话,现在本地的文件和目录应该都是小写的了,如有更高效的拷贝方法请朋友告知!3ks

2、安装nginx模块ngx_http_lower_upper_case (在192.168.18.240上操作)
[root@vm3 ~]# git clone http://github.com/replay/ngx_http_lower_upper_case.git
[root@vm3 ~]# cd nginx-1.2.6
[root@vm3 ~]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --add-module=../ngx_http_lower_upper_case/
[root@vm3 ~]# make
[root@vm3 ~]# make install

3、配置nginx (在192.168.18.240上操作)
[root@vm3 ~]# grep -v '^[[:space:]]*#\|^$' /usr/local/nginx/conf/nginx.conf
[root@vm3 ~]# egrep -v '^[[:space:]]*#|^$' /usr/local/nginx/conf/nginx.conf
user        apache;
worker_processes        1;
events {
               worker_connections        1024;
}
http {
               include                         mime.types;
               default_type        application/octet-stream;
               sendfile                                on;
               keepalive_timeout        65;
               server {
                               listen                         80;
                               server_name        localhost;
                               root         html;
                               index        index.html index.htm;
                               location ~* "\.php$" {
                                 fastcgi_pass 127.0.0.1:9000;
                                 include fastcgi.conf;
                               }
                               location / {
                                 lower $lower_uri "$request_uri";
       rewrite .* $lower_uri break;
       proxy_pass http://192.168.18.241;
                               }
                               error_page         500 502 503 504        /50x.html;
                               location = /50x.html {
                                               root         html;
                               }
               }
}
用-t测试配置文件,启动或重启nginx
[root@vm3 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
4、测试
190611236.png
ok了,现在不管是访问大写还是小写都没问题了,大功告成!

如果不用代理,本地也可以,就是把proxy_pass删掉即可!


使用如下lua脚本,也可以实现:

location / {
           proxy_pass http://192.168.18.241;
           rewrite_by_lua '
                ngx.req.set_uri(string.lower(ngx.var.uri))
            ';
}