How to make URL case insensitive with Nginx

2020-02-26 05:00发布

问题:

I am using Nginx for a simple demo website, and I just configure the Nginx like this:

server {
    listen          80;
    server_name     www.abc.com;

    location / {
        index           index.html;
        root            /home/www.abc.com/;
    }
}

In my www.abc.com folder, I have sub-folder named Sub, and inside has index.html file. So when I try to visit www.abc.com/Sub/index.html, then it works fine. If I visit www.abc.com/sub/index.html, it returns 404.

How to configure the Nginx to case-insensitive in URL?

回答1:

server {
    # Default, you don't need this!
    #listen          80;

    server_name     www.abc.com;

    # Index and root are global configurations for the whole server.
    index           index.html;
    root            /home/www.abc.com/;

    location / {
        location ~* ^/sub/ {
            # The tilde and asterisks ensure that this location will
            # be matched case insensitive. nginx does not support
            # setting absolutely everything to be case insensitive.
            # The reason is easy, it's costly in terms of performance.
        }
    }
}


标签: nginx