JonLuca
JonLuca

Reputation: 919

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:

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!

Upvotes: 6

Views: 11124

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146530

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

Upvotes: 10

Related Questions