Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx case insensitive proxy_pass

I've got a site called http://example.com, with an app running that can be accessed at http://example.com/app1. The app1 is sitting behind an nginx reverse proxy, like so:

location /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

Adding the trailing slash to the proxy_pass field lets me "remove" the /app1/ part of the URL, at least as far as the app is concerned. So app1 thinks that it's getting requests to the root url (as in, I have a route in app1 that sits on '/', not '/app1').

However, I'd like to have nginx make this case-insensitive. So whether I go to http://example.com/App1 or http://example.com/APP1, it should still just forward the request to app1, and remove the /app1/ part of the url.

When I attempt to use nginx's case insensitive rules, it does not let forward the rest of the URI to app1.

location ~* /app1/ {
    proxy_pass http://localhost:8080/;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

That gives me an nginx configuration error.

My goals are two fold:

  • Match /app1/ case insensitively
  • Remove the /app1/ part of the url when "passing" the url over to the app

I've tried rewriting the url, but it won't let me add the rest of the URI to proxy_pass.

Any help would be appreciated!

like image 722
JonLuca Avatar asked Oct 07 '17 22:10

JonLuca


1 Answers

You should capture rest of the url and then use it

location ~* /app1/(.*) {
    proxy_pass http://localhost:8080/$1$is_args$args;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}
like image 78
Tarun Lalwani Avatar answered Nov 15 '22 06:11

Tarun Lalwani