我如何在nginx web服务器的重写规则中将大写字母翻译成小写字母?

问题描述 投票:23回答:5

我需要翻译地址:

www.example.com/TEST in --- www.example.com/test

nginx rewrite uppercase lowercase
5个回答
14
投票

是的,你将需要perl。如果您使用的是Ubuntu,而不是apt-get install nginx-full,请使用apt-get install nginx-extras,它将具有嵌入式perl模块。然后,在您的配置文件中:

  http {
  ...
    # Include the perl module
    perl_modules perl/lib;
    ...
    # Define this function
    perl_set $uri_lowercase 'sub {
      my $r = shift;
      my $uri = $r->uri;
      $uri = lc($uri);
      return $uri;
    }';
    ...
    server {
    ...
      # As your first location entry, tell nginx to rewrite your uri,
      # if the path contains uppercase characters
      location ~ [A-Z] {
        rewrite ^(.*)$ $scheme://$host$uri_lowercase;
      }
    ...

7
投票

我设法使用嵌入式perl实现目标:

location ~ [A-Z] {
  perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}

4
投票
location ~*^/test/ {
  return 301 http://www.example.com/test;
}

位置可以由前缀字符串或正则表达式定义。使用前面的“〜*”修饰符(对于不区分大小写的匹配)或“〜”修饰符(对于区分大小写的匹配)指定正则表达式。

Soruce:http://nginx.org/en/docs/http/ngx_http_core_module.html#location


4
投票
location /dupa/ {
    set_by_lua $request_uri_low "return ngx.arg[1]:lower()" $request_uri;
    rewrite ^ https://$host$request_uri_low;
}

1
投票

基于Adam的回答,我最终使用了lua,因为它可以在我的服务器上使用。

set_by_lua $request_uri_low "return ngx.arg[1]:lower()" $request_uri;
if ($request_uri_low != $request_uri) {
   set $redirect_to_lower 1;
}
if (!-f $request_uri) {
    set $redirect_to_lower "${redirect_to_lower}1";
}
if ($redirect_to_lower = 11) {
    rewrite . https://$host$request_uri_low permanent;
}
© www.soinside.com 2019 - 2024. All rights reserved.