问题描述
我有一个名为http://example.com
的网站,该应用程序正在运行,可以在http://example.com/app1
进行访问. app1位于nginx反向代理后面,如下所示:
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;
}
在proxy_pass
字段中添加斜杠使我可以删除" URL的/app1/部分,至少就应用程序而言.因此,app1认为它正在获取到根URL的请求(例如,我在app1中有一条路由位于'/'
而不是'/app1'
上).
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'
).
但是,我想让Nginx区分大小写.因此,无论我转到http://example.com/App1
还是http://example.com/APP1
,它都应该只是将请求转发到app1, 并删除网址的/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.
当我尝试使用nginx的不区分大小写的规则时,它不会将URI的其余部分转发给app1.
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;
}
这给了我一个Nginx配置错误.
That gives me an nginx configuration error.
我的目标有两个:
- 不区分大小写匹配
/app1/
- 将网址传递"到应用程序时,删除网址的
/app1/
部分
- Match
/app1/
case insensitively - Remove the
/app1/
part of the url when "passing" the url over to the app
我尝试重写url,但不允许我将其余的URI添加到proxy_pass.
I've tried rewriting the url, but it won't let me add the rest of the URI to proxy_pass.
任何帮助将不胜感激!
推荐答案
您应该捕获其余的URL,然后使用它
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;
}
这篇关于Nginx不区分大小写proxy_pass的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!