配置 nginx 多个位置不区分大小写

Config nginx multiple locations case insensitive

我将我的 nginx 作为反向代理设置如下:

server {
    listen 80;
    server_name mydomain.com anotherdomain.es 20.18.4.140;
        location ^~ /service/ {
        proxy_pass http://20.18.4.146/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ^~ /vm2/ {
        proxy_pass http://20.18.4.146/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ^~ /vm3/ {
        proxy_pass http://20.18.4.142:6001/;
        #proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Proto $scheme;
    }       location / {
        proxy_pass http://20.18.4.148/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    } }

server {
    listen 443 ssl;
    server_name mydomain.com anotherdomain.es 20.18.4.140;

    fastcgi_param HTTPS on;
    include /etc/nginx/include/ssl;
        location ^~ /service/ {
        proxy_pass https://20.18.4.146/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ^~ /vm2/ {
        proxy_pass https://20.18.4.146/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }       

    location ^~ /vm3/ {
        proxy_pass https://20.18.4.142:6000/;
        #proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Proto $scheme;
    }

    location / {
        proxy_pass https://20.18.4.148/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    } 
}

现在我可以这样正确访问它了:

mydomain.com/service/... anotherdomain.es/service/...

mydomain.com/vm2/... anotherdomain.es/vm2/...

mydomain.com/vm3/... anotherdomain.es/vm3/...

mydomain.com/... anotherdomain.es/...

将所有流量从一个位置重定向到适当的服务器。其余的url(包括参数)没有问题,有大小写。

但我需要位置不区分大小写。并且能够像那样访问它(或任何其他组合):

mydomain.com/Service/... anotherdomain.es/SERVICE/...

mydomain.com/VM2/... anotherdomain.es/Vm2/...

我不想重复位置或重复每个位置的配置。我想做得更好,更有效率。

我尝试过使用正则表达式,但检查语法时出现错误,而且它不起作用。例如这个:

~* ^/(vm2|Vm2|VM2)/$
~* ^/vm2/
~* /(<vm2>/g)
~* /app1/(.*)
...

错误:

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/sites-enabled/my-sites:4 nginx: configuration file /etc/nginx/nginx.conf test failed

也可能是这样:mydomain.com/vm2/service/15(作为正则表达式应该是 2)

我已经查看并尝试了很多,但我没有看到任何适合我的东西。有任何想法吗?谢谢。

Richard Smith's clue , what I'd seen and new readings (nginx-case-insensitive-location) 我就这样了:

location ~* /service(?<stuff>.*)$  {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    rewrite ^ /$stuff break;
    proxy_pass http://20.18.4.146;
}

... 
...ssl
...

location ~* /service(?<stuff>.*)$  {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    rewrite ^ /$stuff break;
    proxy_pass https://20.18.4.146;
}

...

location / {
    proxy_pass https://20.18.4.148/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
}

And now "service" or "vmX" with Case Insentive, meaning Service or SERVICE or serVice or VM2.

希望对像我一样没有nginx经验的人有所帮助