售前咨询
技术支持
渠道合作

Nginx实现url请求不区分大小写

如果你将跑在Windows下的项目(如:php)迁移到Linux下,由于Windows操作系统中,文件名是不区分大小写的;而Linux系统是大小写敏感,会导致有些网页出现404情况。

解决方法有大概4种:

  1. url rewrite
  2. perl模块
  3. lua模块
  4. ngx_http_lower_upper_case

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

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

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

  1. cd lnmp/src/nginx-1.4.4
  2. make clean #清除已经编译出的nginx
  3. # /usr/local/nginx/sbin/nginx -V #获取已编译参数
  4. nginx version: nginx/1.4.4
  5. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
  6. TLS SNI support enabled
  7. 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 ,如下:

  1. ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module \
  2. –with-http_ssl_module –with-http_flv_module –with-http_gzip_static_module –with-ld-opt=‘-ljemalloc’ \
  3. –with-http_perl_module

可能会报如下错误:

  1. 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 .).
  2. BEGIN failed–compilation aborted.
  3. ./configure: error: perl module ExtUtils::Embed is required

解决方法(CentOS):

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

再次编译:

  1. make clean
  2. ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module \
  3. –with-http_ssl_module –with-http_flv_module –with-http_gzip_static_module –with-ld-opt=‘-ljemalloc’ \
  4. –with-http_perl_module
  5. make
  6. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx$(date +%m%d)    #备份nginx原文件
  7. service nginx stop
  8. make install #直接安装,如果只覆盖nginx,会有报错
  9. /usr/local/nginx/sbin/nginx -t

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

  1. perl_set $url ‘
  2. sub {
  3.         my $r = shift;
  4.         my $re = lc($r->uri);
  5.         return $re;
  6. }
  7. ‘;

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

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

lua模块

lua-nginx-module来自大牛agentzh的开源项目,在Nginx中嵌入Lua语言,使之可以支持强大Lua语法,如下:

  1. cd lnmp/src
  2. wget https://github.com/simpl/ngx_devel_kit/archive/v0.2.19.tar.gz #ngx_devel_kit
  3. wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.9.2.tar.gz #nginx_lua_module
  4. tar xzf LuaJIT-2.0.2.tar.gz
  5. tar xzf v0.2.19.tar.gz
  6. tar xzf v0.9.2.tar.gz
  7. cd LuaJIT-2.0.2
  8. make && make install
  9. export LUAJIT_LIB=/usr/local/lib
  10. export LUAJIT_INC=/usr/local/include/luajit-2.0
  11. cd nginx-1.4.4
  12. make clean #清除已经编译出的nginx
  13. # /usr/local/nginx/sbin/nginx -V #获取已编译参数
  14. nginx version: nginx/1.4.4
  15. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)
  16. TLS SNI support enabled
  17. 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’

重新编译Nginx:

  1. ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module \
  2. –with-http_ssl_module –with-http_flv_module –with-http_gzip_static_module –with-ld-opt=-ljemalloc \
  3. –add-module=../lua-nginx-module-0.9.2 –add-module=../ngx_devel_kit-0.2.19
  4. make
  5. cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx$(date +%m%d)    #备份nginx原文件
  6. service nginx stop
  7. make install #直接安装,如果只覆盖nginx,可能会报错
  8. ldconfig #重新读取库文件,否则报错
  9. /usr/local/nginx/sbin/nginx -t

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

  1. location / {
  2.         if ($uri ~ [A-Z]){
  3.                 rewrite_by_lua ‘return ngx.redirect(string.lower(ngx.var.uri),ngx.HTTP_MOVED_PERMANENTLY)’;
  4.                 }
  5.         }

 

上一篇:

下一篇:

相关新闻

 

领取优惠
免费预约

申请试用SSL证书

提交成功!

咨询客服