将服务器从Ubuntu迁移到Debian后发生了一个严重的问题。 Debian不允许两个文件(例如“a.html”和“A.html”)位于同一目录中。

我的服务器收到三种类型的请求,这是当前状态:

文件提供了/archive/2014/www.Test.com之类的请求:/archive/2014/blank.html
文件/archive/2015/Test.com提供了诸如/archive/2015/www.Test.com/archive/2015/T.html之类的请求

文件/archive/2015/test.com提供了诸如/archive/2015/www.test.com/archive/2015/t.html之类的请求

我希望后两种请求在两种情况下都以不区分大小写的方式提供文件/archive/2015/t.html

我怎么能达到这个结果?

当前服务器设置为:

server {
    listen   127.0.0.1:80;
    server_name 127.0.0.1;

    access_log /srv/siteone/logs/access.log;
    error_log /srv/siteone/logs/error.log error;

    location / {
        root   /srv/siteone/html;
        index  index.html index.htm;
        expires 1d;
    }

    rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
    rewrite ^/archive/2015/(www\.)*(.)(.+)$ /archive/2015/$2.html last;

    error_page  403  /403.html;
    error_page  404  /404.html;

}

最佳答案

有很多方法可以解决此问题。

  • 由于您只需要将一个特定的字母更改为小写,因此可以对不区分大小写的正则表达式使用“map”:
    map $request $letter {
        "~*^/archive/[0-9]{4}/(www\.)?a(.*)?$"  a;
        "~*^/archive/[0-9]{4}/(www\.)?b(.*)?$"  b;
        "~*^/archive/[0-9]{4}/(www\.)?c(.*)?$"  c;
        "~*^/archive/[0-9]{4}/(www\.)?d(.*)?$"  d;
        "~*^/archive/[0-9]{4}/(www\.)?e(.*)?$"  e;
        "~*^/archive/[0-9]{4}/(www\.)?f(.*)?$"  f;
        "~*^/archive/[0-9]{4}/(www\.)?g(.*)?$"  g;
        "~*^/archive/[0-9]{4}/(www\.)?h(.*)?$"  h;
        "~*^/archive/[0-9]{4}/(www\.)?i(.*)?$"  i;
        "~*^/archive/[0-9]{4}/(www\.)?j(.*)?$"  j;
        "~*^/archive/[0-9]{4}/(www\.)?k(.*)?$"  k;
        "~*^/archive/[0-9]{4}/(www\.)?l(.*)?$"  l;
        "~*^/archive/[0-9]{4}/(www\.)?m(.*)?$"  m;
        "~*^/archive/[0-9]{4}/(www\.)?n(.*)?$"  n;
        "~*^/archive/[0-9]{4}/(www\.)?o(.*)?$"  o;
        "~*^/archive/[0-9]{4}/(www\.)?p(.*)?$"  p;
        "~*^/archive/[0-9]{4}/(www\.)?q(.*)?$"  q;
        "~*^/archive/[0-9]{4}/(www\.)?r(.*)?$"  r;
        "~*^/archive/[0-9]{4}/(www\.)?s(.*)?$"  s;
        "~*^/archive/[0-9]{4}/(www\.)?t(.*)?$"  t;
        "~*^/archive/[0-9]{4}/(www\.)?u(.*)?$"  u;
        "~*^/archive/[0-9]{4}/(www\.)?v(.*)?$"  v;
        "~*^/archive/[0-9]{4}/(www\.)?w(.*)?$"  w;
        "~*^/archive/[0-9]{4}/(www\.)?x(.*)?$"  x;
        "~*^/archive/[0-9]{4}/(www\.)?y(.*)?$"  y;
        "~*^/archive/[0-9]{4}/(www\.)?z(.*)?$"  z;
    }
    
    server {
        listen   127.0.0.1:80;
        server_name 127.0.0.1;
    
        access_log /srv/siteone/logs/access.log;
        error_log /srv/siteone/logs/error.log error;
    
        root   /srv/siteone/html;
    
        location / {
            index  index.html index.htm;
            expires 1d;
        }
    
        rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
        rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$letter.html last;
    
        error_page  403  /403.html;
        error_page  404  /404.html;
    }
    
  • 如果您安装了嵌入式Perl模块(sudo apt-get install nginx-extras),则可以使用Perl将请求行转换为小写:
    perl_set $uri_lowercase 'sub {
        my $r = shift;
        return lc($r->uri);
    }';
    
    server {
        listen   127.0.0.1:80;
        server_name 127.0.0.1;
    
        access_log /srv/siteone/logs/access.log;
        error_log /srv/siteone/logs/error.log error;
    
        root   /srv/siteone/html;
    
        location / {
            index  index.html index.htm;
            expires 1d;
        }
    
        rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
        rewrite ^/archive/2015/(www\.)?(.)(.+)$ $uri_lowercase;
        rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last;
    
        error_page  403  /403.html;
        error_page  404  /404.html;
    }
    
  • 如果在Perl之前更喜欢Lua,则可以对Lua进行相同的操作(同样,您将需要安装nginx-extras):
    server {
        listen   127.0.0.1:80;
        server_name 127.0.0.1;
    
        access_log /srv/siteone/logs/access.log;
        error_log /srv/siteone/logs/error.log error;
    
        root   /srv/siteone/html;
    
        location / {
            index  index.html index.htm;
            expires 1d;
        }
    
        rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
        rewrite_by_lua 'ngx.req.set_uri(string.lower(ngx.var.uri), false)';
        rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last;
    
        error_page  403  /403.html;
        error_page  404  /404.html;
    }
    
  • 如果您不喜欢以上所有内容,那么总会有一些黑暗的Nginx技巧可能会有所帮助(但我真的不建议这样做):
    server {
        listen   127.0.0.1:8484;
    
        access_log off;
    
        rewrite ^.*$ /archive/2015/$host.html;
    
        root /srv/siteone/html;
    
        location / {
            index  index.html index.htm;
            expires 1d;
        }
    }
    
    server {
        listen   127.0.0.1:80;
        server_name 127.0.0.1;
    
        access_log /srv/siteone/logs/access.log;
        error_log /srv/siteone/logs/error.log error;
    
        root   /srv/siteone/html;
    
        location / {
            index  index.html index.htm;
            expires 1d;
        }
    
        location ~* ^/archive/2015/(?<letter>[A-Z])\.html$ {
            proxy_set_header Host $letter;
            proxy_pass http://127.0.0.1:8484;
        }
    
        rewrite ^/archive/2014/(.+)$ /archive/2014/blank.html last;
        rewrite ^/archive/2015/(www\.)?(.)(.+)$ /archive/2015/$2.html last;
    
        error_page  403  /403.html;
        error_page  404  /404.html;
    }
    
  • 关于regex - 如何在Nginx服务器上区分大小写的正则表达式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29945344/

    10-12 14:27