How to do case insensitive regex on an Nginx server? -
a serious problem happened after migrating server ubuntu debian. debian won't allow 2 files, example "a.html
" , "a.html
" in same directory.
my server gets 3 types of requests , current status:
requests such /archive/2014/www.test.com
supplied file: /archive/2014/blank.html
requests such /archive/2015/test.com
, /archive/2015/www.test.com
supplied file /archive/2015/t.html
requests such /archive/2015/test.com
, /archive/2015/www.test.com
supplied file /archive/2015/t.html
i want last 2 types of requests supply file /archive/2015/t.html
in both cases (in case insensitive way).
how achieve outcome?
the current server settings are:
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; }
there plenty of ways can go problem.
since need change 1 specific letter lowercase, use "map" case insensitive regular expression:
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 have embedded perl module installed (sudo apt-get install nginx-extras), use perl request line lowercase:
perl_set $uri_lowercase 'sub { $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 prefer lua before perl, same lua (again, need nginx-extras installed):
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 don't of above, there dark nginx trickery (but 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; }
Comments
Post a Comment