nginx中不区分大小写的URL

时间:2016-03-24 02:48:34

标签: url nginx server config

我希望能够访问www.mrmerch.com/TERMS和www.mrmerch.com/terms。我目前有两个子文件夹设置来完成此任务 - 条款和条款。我知道,效率低且不正确。

这是我目前的配置。我添加了最后一个位置块,但我仍然无法点击任何不完全匹配的位置(例如,“条款”)。任何帮助是极大的赞赏。谢谢!

server   {
server_name www.mrmerch.com;
return 301 $scheme://mrmerch.com$request_uri;
}

server {
listen 80 default_server;
server_name mrmerch.com;
server_tokens off;

access_log /var/log/nginx/mrmerch.access_log;
error_log /var/log/nginx/mrmerch.error_log;

root /home/paul/www/mistermerch.com/;
try_files $uri $uri/ /index.php$is_args$args;

gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length  1000;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6].";

location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

location /artwork {
    alias /home/mistermerch/www/;
}

location ~ \.php$ {
    try_files   $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param  SCRIPT_FILENAME /home/paul/www/mistermerch.com$fastcgi_script_name;
    include fastcgi_params;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }


    location ~* ^/Terms/ {
    }



}

1 个答案:

答案 0 :(得分:2)

您可以使用不区分大小写的正则表达式位置在内部重写URI:

location ~* ^/terms {
    rewrite ^ /static/terms.html last;
}

在这种情况下,/terms/terms//TeRmS/tErMs/等都会被重写到 outside 之外的位置正则表达式位置匹配。

如果您希望使用也与正则表达式位置匹配的目标URI,则可以使用完全匹配位置提取它:

location ~* ^/terms {
    rewrite ^ /terms/index.html last;
}
location = /terms/index.html {
}

有关详细信息,请参阅this document

相关问题