Windows 下路径默认忽略大小写,而 UNIX/Linux 下是区分大小写的,而 NGINX 的访问地址与路径息息相关,因此需要忽略访问链接中的大小写。
三种实现方式
perl module
lua module
ngx_http_lower_upper_case module
perl 模块
安装模块
sudo apt install nginx-module-perl
导入模块
在配置文件的 MAIN 段加入以下配置
load_module modules/ngx_http_perl_module.so;
使用模块
在配置文件的 HTTP 段加入以下配置
http {
perl_modules perl/lib;
perl_set $url_lowercase 'sub {
my $r = shift;
my $uri = $r->uri;
$uri = lc($uri);
return $uri;
}';
}
在配置文件需要的段落加入以下配置模板
location /windows {
if ($uri ~ [A-Z]){ rewrite ^(.*)$ $url_lowercase last; }
}
lua 模块
lua_nginx_module 是一个 NGINX HTTP 模块,把 lua 解析器内嵌到 NGINX,用来解析并执行 lua 语言编写的网页后台脚本。
若没有安装过 NGINX 可以直接部署 OpenResty 预编译包。
若已经安装过 NGINX ,可以重新编译 NGINX 增加 Lua 语言支持。
源码下载
下载 LuaJIT
cd /usr/local/src
wget http://luajit.org/download/LuaJIT-2.1.0-beta3.tar.gz
下载 NDK(ngx_devel_kit)
cd /usr/local/src
wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
下载 Lua
cd /usr/local/src
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.14.tar.gz
下载 NGINX
wget https://nginx.org/download/nginx-1.14.2.tar.gz
构建环境
编译 LuaJIT
tar xf LuaJIT-2.1.0-beta3.tar.gz
cd luajit2-2.1-20190302
make && sudo make install
查看当前编译参数
# nginx -V
nginx version: nginx/1.14.2
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f 25 May 2017 (running with OpenSSL 1.1.1b 26 Feb 2019)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.14.2/debian/debuild-base/nginx-1.14.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
导入模块重新编译
tar xf nginx-1.14.2.tar.gz
tar xf v2.1-20190302.tar.gz
tar xf v0.10.14.tar.gz
cd nginx-1.14.2/
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.1/
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.14.2/debian/debuild-base/nginx-1.14.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-module=../ngx_devel_kit-0.3.1rc1/ --add-module=../lua-nginx-module-0.10.14/
小贴士:实际上就增加了以下两句--add-module=/path/to/ngx_devel_kit
--add-module=/path/to/lua-nginx-module
报错集锦
若出现以下报错
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决办法
sudo apt install libpcre3 libpcre3-dev
若出现以下报错
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
解决办法
sudo apt install openssl libssl-dev
若出现以下报错
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
解决办法
sudo apt install zlib1g-dev
开始编译
make -j$(($(nproc) + 1))
小贴士:若 CPU 为多核,使用 -j
参数来多线程编译,加快速度。
安装
make install
测试
在配置文件中新增一段
location /hello_lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}
测试配置文件
nginx -t
然后访问域名 domain.com/hello_lua
即可测试
使用
location / {
if ($uri ~ [A-Z]){
rewrite_by_lua 'return ngx.redirect(string.lower(ngx.var.uri),ngx.HTTP_MOVED_PERMANENTLY)';
}
root /var/www/html;
index index.html;
}
ngx_http_lower_upper_case 模块
访问其 Github
server {
listen 80;
server_name _;
charset utf-8;
location / {
if ($uri ~ [A-Z]){
lower $url "$uri";
rewrite ^(.*)$ $url redirect;
}
root /usr/share/tengine/html;
index index.html;
}
}
附录
参考链接
- NGINX实现网址大小写转换 - 开源中国
- unknown directive “perl_modules” - StackOverFlow
- HTTP PERL MODULE - NGINX
- NGINX 安装 Lua 模块 - CSDN
- How to install libpng and zlib - AskUbuntu
- Debian中安装PCRE库 - CSDN
- Lua-nginx-module - GitHub
本文由 柒 创作,采用 知识共享署名4.0
国际许可协议进行许可。
转载本站文章前请注明出处,文章作者保留所有权限。
最后编辑时间: 2019-03-05 16:40 PM