[英]nginx: unable to perform path based routing
I am creating a docker-compose
stack with prometheus
, grafana
and nginx
. 我正在用
prometheus
, grafana
和nginx
创建一个grafana
docker-compose
堆栈。
I want to provide host-based routing so that the /
location redirects to grafana
and /prometheus
to prometheus
. 我想提供基于主机的路由,以便
/
位置重定向到grafana
, /prometheus
重定向到prometheus
。
However the nginx.conf
below, does perofmrm /
--> grafana
redirection but not /prometheus
--> prometheus
(the later goes to a grafana
error page) 但是下面的
nginx.conf
执行perofmrm /
-> grafana
重定向,而不是/prometheus
> prometheus
(稍后转到grafana
错误页面)
Any suggestions? 有什么建议么?
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location /prometheus {
rewrite ^/prometheus(.*) /$1 break;
proxy_pass http://11.12.12.31:9090;
}
location / {
proxy_pass http://11.12.12.31:3000;
}
( prometheus
and grafana
are services exposed to 9090
and 3000
respectively) (
prometheus
和grafana
的服务受到9090
和3000
分别)
edit : 编辑 :
nginx_1 | 199.99.99.67 - - [17/Jan/2019:11:10:51 +0000] "GET /prometheus HTTP/1.1" 302 29 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
grafana_1 | t=2019-01-17T11:10:51+0000 lvl=info msg="Request Completed" logger=context userId=1 orgId=1 uname=admin method=GET path=/graph status=404 remote_addr=192.168.96.1 time_ms=8 size=25545 referer=
nginx_1 | 199.99.99.67 - - [17/Jan/2019:11:10:51 +0000] "GET /graph HTTP/1.1" 404 25581 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" "-"
I believe the rewrite ^/prometheus(.*) /$1 break;
我相信
rewrite ^/prometheus(.*) /$1 break;
clause is causing a redirection to /
. 子句导致重定向到
/
。
The nginx
documentation suggests that the rewrite
operation actually performs an regular expression against the requested url
and replace it with something else. nginx
文档建议rewrite
操作实际上针对请求的url
执行一个正则表达式,并将其替换为其他内容。
In this case /prometheus/
is been resolved as /
due to regex (.*)
. 在这种情况下
/prometheus/
由于正则表达式(.*)
被解析为/
。 This expression means to match 0 or more characters after the word "prometheus" and to capture/store whatever characters found as variable $1
. 此表达式的意思是匹配单词“ prometheus”之后的0个或更多字符,并捕获/存储被发现为变量
$1
任何字符。
So path /prometheus
is been replaced as /
which brings you to the Grafana
server. 因此,路径
/prometheus
被替换为/
,从而将您带到Grafana
服务器。
Removing the rewrite
rule will fix your problem. 删除
rewrite
规则将解决您的问题。
Reference: 参考:
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
https://regex101.com/r/vjbFdN/1 https://regex101.com/r/vjbFdN/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.