Last active
February 18, 2020 14:23
-
-
Save mariocesar/8e1ef0bd620105e9654cc0471c4da5b5 to your computer and use it in GitHub Desktop.
NGINX / Rewrite all Uppercase urls to the lowercase form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Testing with docker: | |
# docker run --rm -it -p 8000:80 -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf nginx:stable-perl | |
load_module modules/ngx_http_perl_module.so; | |
user nginx; | |
worker_processes 1; | |
http { | |
include /etc/nginx/mime.types; | |
# Load perl module library from nginx | |
perl_modules perl/lib; | |
# Define a perl function | |
perl_set $rewrite_to_lowercase ' | |
sub { | |
# shift will get the first argument of the functio, that will be the request | |
my $r = shift; | |
# Extract just the uri | |
my $uri = $r->uri; | |
# lc converts to lower case LowerCase | |
$uri = lc($uri); | |
return $uri; | |
} | |
'; | |
server { | |
listen 80 default_server; | |
location ~ [A-Z] { | |
set $new_request_uri $rewrite_to_lowercase; | |
return 301 $scheme://$http_host$new_request_uri$is_args$args; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment