简体   繁体   中英

nginx: unable to perform path based routing

I am creating a docker-compose stack with prometheus , grafana and nginx .

I want to provide host-based routing so that the / location redirects to grafana and /prometheus to prometheus .

However the nginx.conf below, does perofmrm / --> grafana redirection but not /prometheus --> prometheus (the later goes to a grafana error page)

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)

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

In this case /prometheus/ is been resolved as / due to regex (.*) . This expression means to match 0 or more characters after the word "prometheus" and to capture/store whatever characters found as variable $1 .

So path /prometheus is been replaced as / which brings you to the Grafana server.

Removing the rewrite rule will fix your problem.

Reference:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

https://regex101.com/r/vjbFdN/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM