How do I make case insensitive regex in nginx server?
A serious problem came up after migrating the server from Ubuntu to Debian. Debian does not allow two files, such as " a.html
" and " a.html
", to be used in the same directory.
My server receives three types of requests and this is the current status:
requests such as /archive/2014/www.Test.com
are supplied with the file:/archive/2014/blank.html
requests like /archive/2015/Test.com
and /archive/2015/www.Test.com
come with a file/archive/2015/T.html
requests like /archive/2015/Test.com
and /archive/2015/www.Test.com
come with a file/archive/2015/T.html
I want the last two types of queries to supply the file /archive/2015/T.html
in both cases (in case insensitive case).
How could I achieve this result?
Current server settings:
server {
listen 127.0.0.1:80;
server_name 127.0.0.1;
access_log /srv/siteone/logs/access.log;
error_log /srv/siteone/logs/error.log error;
location / {
root /srv/siteone/html;
index index.html index.htm;
expires 1d;
}
rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
rewrite ^/archive/2015/(www\.)*(.)(.+)$ /archive/2015/$2.html last;
error_page 403 /403.html;
error_page 404 /404.html;
}
source to share
There are many ways to solve this problem.
-
Since you only need to change one specific letter to lowercase, you can use a "map" with a case-insensitive regex:
map $request $letter { "~*^/archive/[0-9]{4}/(www\.)?a(.*)?$" a; "~*^/archive/[0-9]{4}/(www\.)?b(.*)?$" b; "~*^/archive/[0-9]{4}/(www\.)?c(.*)?$" c; "~*^/archive/[0-9]{4}/(www\.)?d(.*)?$" d; "~*^/archive/[0-9]{4}/(www\.)?e(.*)?$" e; "~*^/archive/[0-9]{4}/(www\.)?f(.*)?$" f; "~*^/archive/[0-9]{4}/(www\.)?g(.*)?$" g; "~*^/archive/[0-9]{4}/(www\.)?h(.*)?$" h; "~*^/archive/[0-9]{4}/(www\.)?i(.*)?$" i; "~*^/archive/[0-9]{4}/(www\.)?j(.*)?$" j; "~*^/archive/[0-9]{4}/(www\.)?k(.*)?$" k; "~*^/archive/[0-9]{4}/(www\.)?l(.*)?$" l; "~*^/archive/[0-9]{4}/(www\.)?m(.*)?$" m; "~*^/archive/[0-9]{4}/(www\.)?n(.*)?$" n; "~*^/archive/[0-9]{4}/(www\.)?o(.*)?$" o; "~*^/archive/[0-9]{4}/(www\.)?p(.*)?$" p; "~*^/archive/[0-9]{4}/(www\.)?q(.*)?$" q; "~*^/archive/[0-9]{4}/(www\.)?r(.*)?$" r; "~*^/archive/[0-9]{4}/(www\.)?s(.*)?$" s; "~*^/archive/[0-9]{4}/(www\.)?t(.*)?$" t; "~*^/archive/[0-9]{4}/(www\.)?u(.*)?$" u; "~*^/archive/[0-9]{4}/(www\.)?v(.*)?$" v; "~*^/archive/[0-9]{4}/(www\.)?w(.*)?$" w; "~*^/archive/[0-9]{4}/(www\.)?x(.*)?$" x; "~*^/archive/[0-9]{4}/(www\.)?y(.*)?$" y; "~*^/archive/[0-9]{4}/(www\.)?z(.*)?$" z; } server { listen 127.0.0.1:80; server_name 127.0.0.1; access_log /srv/siteone/logs/access.log; error_log /srv/siteone/logs/error.log error; root /srv/siteone/html; location / { index index.html index.htm; expires 1d; } rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last; rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$letter.html last; error_page 403 /403.html; error_page 404 /404.html; }
-
If you have the Embedded Perl module installed (sudo apt-get install nginx-extras), you can use Perl to get the lowercase query string:
perl_set $uri_lowercase 'sub { my $r = shift; return lc($r->uri); }'; server { listen 127.0.0.1:80; server_name 127.0.0.1; access_log /srv/siteone/logs/access.log; error_log /srv/siteone/logs/error.log error; root /srv/siteone/html; location / { index index.html index.htm; expires 1d; } rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last; rewrite ^/archive/2015/(www\.)?(.)(.+)$ $uri_lowercase; rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last; error_page 403 /403.html; error_page 404 /404.html; }
-
If you prefer Lua over Perl, you can do the same with Lua (again, you need to install nginx-extras):
server { listen 127.0.0.1:80; server_name 127.0.0.1; access_log /srv/siteone/logs/access.log; error_log /srv/siteone/logs/error.log error; root /srv/siteone/html; location / { index index.html index.htm; expires 1d; } rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last; rewrite_by_lua 'ngx.req.set_uri(string.lower(ngx.var.uri), false)'; rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last; error_page 403 /403.html; error_page 404 /404.html; }
-
If you don't like all of the above, there is always some dark Nginx trick that could help (but I really don't recommend it):
server { listen 127.0.0.1:8484; access_log off; rewrite ^.*$ /archive/2015/$host.html; root /srv/siteone/html; location / { index index.html index.htm; expires 1d; } } server { listen 127.0.0.1:80; server_name 127.0.0.1; access_log /srv/siteone/logs/access.log; error_log /srv/siteone/logs/error.log error; root /srv/siteone/html; location / { index index.html index.htm; expires 1d; } location ~* ^/archive/2015/(?<letter>[A-Z])\.html$ { proxy_set_header Host $letter; proxy_pass http://127.0.0.1:8484; } rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last; rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last; error_page 403 /403.html; error_page 404 /404.html; }
source to share