Url requests implemented by Nginx in CentOS are case-insensitive.

Source: Internet
Author: User

Url requests implemented by Nginx in CentOS are case-insensitive.

If you migrate a project running in Windows (such as php) to Linux, the file names in Windows are case-insensitive while in Linux, this may cause 404 errors on some webpages.

There are four solutions:
1. url rewrite
2. perl Module
3. lua Module
4. ngx_http_lower_upper_case

The first method is applicable to url conversion with rules or a small number of URLs. If there are a large number of URLs without rules, use the following methods:

The second, third, and fourth methods are provided that the local file in Linux is in lowercase, And the principle is to convert the url request to lowercase for processing.

Perl module (not recommended! The Nginx official website has declared the possibility of a memory vulnerability in the perl module. The method is as follows (install the lnmp one-key installation package and execute the following code ):

 

Cd lnmp/src/nginx-1.4.4make clean # Clear compiled nginx #/usr/local/nginx/sbin/nginx-V # Get compiled parameters nginx version: nginx/1.4.4built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) tls sni support enabledconfigure arguments: -- prefix =/usr/local/nginx -- user = www -- group = www -- with-http_stub_status_module -- with-http_ssl_module -- with-http_flv_module -- with-ld-opt = '-ljemalloc'

Add-with-http_perl_module after compiled parameters, as shown below:

./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

The following error may be reported:

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

Solution (CentOS ):

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

Re-compile:

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_modulemakecp/usr/local/nginx/sbin/nginx $ (date + % m % d) # Back up the original nginx file service nginx stop make install # install nginx directly. If only nginx is overwritten, an error/usr/local/nginx/sbin/nginx-t will be reported.

Modify the configuration master file (/usr/local/nginx/conf/nginx. conf ):

        perl_set $url '        sub {                my $r = shift;                my $re = lc($r->uri);                return $re;        } ';

Modify the VM configuration file (for example,/usr/local/nginx/conf/vhost/demo.linuxeye.com. conf ):

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

Lua module (recommended !)
Lua-nginx-module is an open-source project of Daniel agentzh. The Lua language is embedded in Nginx to support powerful Lua syntax, as shown below:

cd lnmp/srcwget 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 xzf LuaJIT-2.0.2.tar.gztar xzf v0.2.19.tar.gztar xzf v0.9.2.tar.gzcd LuaJIT-2.0.2make && make installexport LUAJIT_LIB=/usr/local/libexport LUAJIT_INC=/usr/local/include/luajit-2.0

 

Cd nginx-1.4.4make clean # Clear compiled nginx #/usr/local/nginx/sbin/nginx-V # Get compiled parameters nginx version: nginx/1.4.4built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) tls sni support enabledconfigure arguments: -- prefix =/usr/local/nginx -- user = www -- group = www -- with-http_stub_status_module -- with-http_ssl_module -- with-http_flv_module -- with-ld-opt = '-ljemalloc'

Recompile 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.19makecp/usr/local/nginx/sbin/nginx $ (date + % m % d) # Back up the original nginx file service nginx stop make install # install nginx directly. If only nginx is overwritten, the following error may be reported: ldconfig # re-read the library file, otherwise, the error/usr/local/nginx/sbin/nginx-t is returned.

Modify the configuration file (for example,/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 Module
Reference: https://github.com/replay/ngx_http_lower_upper_case

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.