0

I have my nginx as a reverse proxy set up like this:

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;
    } 
}

Now I'm accessing it correctly this way:

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

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

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

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

Redirects all traffic from a location to the appropriate server. The rest of the url (including the parameters) are unproblematic and have upper and lower case.

But I would need the locations to be case insensitive. And to be able to access it like that (or any other combination):

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

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

I don't want to repeat the locations or repeat the configuration of each one. I would like to do it better and more efficiently.

I have tried with regular Expressions but checking the syntax gives me errors and it doesn't work. For example this one:

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

Error:

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

And also it could be the case: mydomain.com/vm2/service/15 (being regular expressions it would be 2)

I've looked and tried a lot but I don't see anything that works for me. Any ideas? Thanks.

sanmolhec
  • 396
  • 3
  • 11
  • 1
    See [this answer](https://stackoverflow.com/questions/53353572/proxy-pass-cannot-have-uri-part-in-location-given-by-regular-expression/53354944#53354944). – Richard Smith Apr 22 '20 at 10:51
  • Hello. Thank you very much. The article you recommended gave me the insight into why it couldn't be done the way I was trying. With what I had looked at and researched a little more, I got it. Thank you very much. – sanmolhec Apr 23 '20 at 15:00

1 Answers1

0

With Richard Smith's clue , what I'd seen and new readings (nginx-case-insensitive-location) I've left it at that:

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.

I hope it helps other nginx inexperienced people like me.

sanmolhec
  • 396
  • 3
  • 11