如何使用Nginx实现URL不区分大小写

25

我正在使用Nginx搭建一个简单的演示网站,我只是按照如下方式配置了Nginx:

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

    location / {
        index           index.html;
        root            /home/www.abc.com/;
    }
}
在我的 www.abc.com 文件夹中,有一个名为 Sub 的子文件夹,里面有一个 index.html 文件。所以当我尝试访问 www.abc.com/Sub/index.html 时,它可以正常工作。如果我访问 www.abc.com/sub/index.html,则返回 404
如何配置 Nginx 以在 URL 中忽略大小写?

这个回答解决了你的问题吗?如何实现Nginx不区分大小写的目录位置重定向301 - Michael Freidgeim
1个回答

35
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.
        }
    }
}

以上的解决方案对我不起作用。你能帮我吗?~*仅适用于if条件。 - Catmandu
7
我认为这将匹配所有包含 /sub/ 的网址(例如,它也会匹配 "example.com/yellow/sub/")。应该使用 location ~* ^/sub/(带有插入符号)来实现。 - Grant Birchmeier
我也遇到了同样的问题。可能是我实现得不对。服务器 { 监听 80; 服务器名称 xyz.com; 重写 ^/(.)/$ /$1 永久; 地址 / { 根目录 /home/abc; 地址 ~ ^/sub/ { } } } - Umar Hassan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接