nginx php 大小写问题,Nginx实现url请求不区分大小写

如果你将跑在Windows下的项目(如:php)迁移到

解决方法有大概4种:

1、 url rewrite

2、 perl模块

3、 lua模块

4、 ngx_http_lower_upper_case

第一种方法适用于有规则的或者较少的url需要转换,如果有大量并无规则的请用下面几种方法

第二、三、四种方法前提是Linux系统本地文件是小写,原理是将url请求转换成小写来处理

perl模块(不推荐!),方法如下(《lnmp一键安装包》安装后执行下面):

cd lnmp/src/nginx-1.4.4

make clean #清除已经编译出的nginx

# /usr/local/nginx/sbin/nginx -V #获取已编译参数

nginx version: nginx/1.4.4

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-ld-opt='-ljemalloc'

在已经编译的参数后面加上 –with-http_perl_module ,如下:

./configure--prefix=/usr/local/nginx--user=www--group=www--with-http_stub_status_module \--with-http_ssl_module--with-http_flv_module--with-http_gzip_static_module--with-ld-opt='-ljemalloc'\--with-http_perl_module

可能会报如下错误:

Can't locate ExtUtils/Embed.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .).

BEGIN failed--compilation aborted.

./configure: error: perl module ExtUtils::Embed is required

解决方法(

yum-y install perl-devel perl-ExtUtils-Embed

再次编译:

make clean./configure--prefix=/usr/local/nginx--user=www--group=www--with-http_stub_status_module \--with-http_ssl_module--with-http_flv_module--with-http_gzip_static_module--with-ld-opt='-ljemalloc'\--with-http_perl_module

make

cp/usr/local/nginx/sbin/nginx/usr/local/nginx/sbin/nginx$(date+%m%d)#备份nginx原文件service nginx stop

make install#直接安装,如果只覆盖nginx,会有报错/usr/local/nginx/sbin/nginx-t

修改配置主文件(/usr/local/nginx/conf/nginx.conf):

perl_set $url'

sub {

my $r = shift;

my $re = lc($r->uri);

return $re;

}

';

修改虚拟主机配置文件(如:/usr/local/nginx/conf/vhost/demo.linuxeye.com.conf):

if($uri~[A-Z]){rewrite^(.*)$ $url last;}

lua模块(推荐!)

lua-nginx-module来自大牛agentzh的开源项目,在

cd lnmp/src

wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz

wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz#ngx_devel_kitwget https://github.com/chaoslawful/lua-nginx-module/archive/v0.9.2.tar.gz#nginx_lua_moduletar xzfLuaJIT-2.0.2.tar.gz

tar xzf v0.2.19.tar.gz

tar xzf v0.9.2.tar.gz

cdLuaJIT-2.0.2make&&make install

export LUAJIT_LIB=/usr/local/lib

export LUAJIT_INC=/usr/local/include/luajit-2.0

cd nginx-1.4.4make clean#清除已经编译出的nginx# /usr/local/nginx/sbin/nginx -V #获取已编译参数nginx version:nginx/1.4.4built by gcc4.4.720120313(RedHat4.4.7-3)(GCC)TLS SNI support enabled

configure arguments:--prefix=/usr/local/nginx--user=www--group=www--with-http_stub_status_module--with-http_ssl_module--with-http_flv_module--with-http_gzip_static_module--with-ld-opt='-ljemalloc'

重新编译

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module \

--with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-ld-opt=-ljemalloc \

--add-module=../lua-nginx-module-0.9.2 --add-module=../ngx_devel_kit-0.2.19

make

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx$(date +%m%d) #备份nginx原文件

service nginx stop

make install #直接安装,如果只覆盖nginx,可能会报错

ldconfig #重新读取库文件,否则报错

/usr/local/nginx/sbin/nginx -t

修改配置文件(如:/usr/local/nginx/conf/vhost/demo.linuxeye.com.conf):

location / {

if ($uri ~ [A-Z]){

rewrite_by_lua 'return ngx.redirect(string.lower(ngx.var.uri),ngx.HTTP_MOVED_PERMANENTLY)';

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值