若是你將跑在Windows下的項目(如:php)遷移到Linux下,因爲Windows操做系統中,文件名是不區分大小寫的;而Linux系統是大小寫敏感,會致使有些網頁出現404狀況。 php
解決方法有大概4種:
一、 url rewrite
二、 perl模塊
三、 lua模塊
四、 ngx_http_lower_upper_case html
第一種方法適用於有規則的或者較少的url須要轉換,若是有大量並沒有規則的請用下面幾種方法 linux
第2、3、四種方法前提是Linux系統本地文件是小寫,原理是將url請求轉換成小寫來處理 nginx
perl模塊(不推薦!Nginx官網已申明perl模塊存在內存漏洞的可能),方法以下(《lnmp一鍵安裝包》安裝後執行下面): git
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 ,以下: github
./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
可能會報以下錯誤: shell
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): ui
yum -y install perl-devel perl-ExtUtils-Embed
再次編譯: lua
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): url
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