可能很多网站长都遇到了这样的情况,在linux 系统下nginx运行的网站程序URL如何实现不区分大小写或者忽略大小写?这个问题确实是非常麻烦,比如我的一个网站页面是这样的:https://www.extapps.com/drivers/HP_LaserJet_1020_Plus.html,这是一个驱动页面,其中的url有大写也有小写,如何在linux系统下nginx运行环境实现:https://www.extapps.com/drivers/hp_laserjet_1020_plus.html,全小写或者大小写混输呢?
1、首先我们不得不说的就是,linux系统本身就是区分大小写的,哪怕有一个字母输入错误了和本身网站最终生成的url不一样,都是会出现404错误的,而windows系统却原生就不区分大小写,也不用额外的配置。
2、如果你一定要使用linux系统在nginx搭建环境下使url不分大小写,那么只能这样做:
解决方法有大概4种:
1、 url rewrite
2、 perl模块
3、 lua模块
4、 ngx_http_lower_upper_case
第一种方法适用于有规则的或者较少的url需要转换,如果有大量并无规则的请用下面几种方法
第二、三、四种方法前提是Linux系统本地文件是小写,原理是将url请求转换成小写来处理
perl模块(不推荐!Nginx官网已申明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.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'
在已经编译的参数后面加上 –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
解决方法(CentOS):
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的开源项目,在Nginx中嵌入Lua语言,使之可以支持强大Lua语法,如下:
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_kit
wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.9.2.tar.gz #nginx_lua_module
tar xzf LuaJIT-2.0.2.tar.gz
tar xzf v0.2.19.tar.gz
tar xzf v0.9.2.tar.gz
cd LuaJIT-2.0.2
make && make install
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0
cd nginx-1.4.4
make clean #清除已经编译出的nginx# /usr/local/nginx/sbin/nginx -V #获取已编译参数
nginx version: nginx/1.4.4
built by gcc 4.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'
重新编译Nginx:
./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)';}}
ngx_http_lower_upper_case模块
参考:https://github.com/replay/ngx_http_lower_upper_case
温馨提示:以上方法步骤需要根据您linux文件下载的路径或nginx...等配置来做对应修改再编译,不过经过小编实战测试,这方法并不科学,还是推荐大家使用windows服务器原生就支持url大小写混输,不区分大小写,其实windows服务器系统也多耗用不了多少配置。
正在阅读:
如何实现Linux nginx网站url不分大小写05-29
2016年10月证券从业资格统考报名考点设39个城市03-30
军训领悟作文500字05-23
自我鉴定大学生500字10篇06-29
小学五年级想象作文500字左右11-03
八月十五中秋节祝福简短句子(5篇)09-10
学会分享800字高中作文议论文|学会分享800字高中作文04-23
[中班语言教案小蚂蚁坐汽车ppt]中班语言教案:小蚂蚁去水果城10-18
2017年广东省广州市工商局直属事业单位招事业编制【13人】02-25
2020年河南注册化工工程师网上报名方法01-11