Nginx case insensitive proxy_pass

血红的双手。 提交于 2019-12-10 23:51:58

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/46625593/nginx-case-insensitive-proxy-pass

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!