【问题标题】:Grafana doesn't work with nginx proxy_pass variableGrafana 不适用于 nginx proxy_pass 变量
【发布时间】:2019-06-24 23:36:22
【问题描述】:

我们在 docker swarm 中运行 grafana 和 nginx,并将 url /foobar/ 代理到 grafana 的 swarm 实例。使用this guide,这适用于以下配置:

# nginx config
server {
    resolver 127.0.0.11 valid=30s;

    ...

    location /foobar/ {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://grafana:3000/;
        proxy_next_upstream error timeout http_502;
    }
}
# docker-compose
  grafana:
    image: ${REGISTRY}foo/grafana:${IMAGE_VERSION}
    networks:
      - foo
    volumes:
      - grafana:/var/lib/grafana
    environment:
      - GF_SERVER_ROOT_URL=%(protocol)s://%(domain)s:%(http_port)s/foobar/

但是,如果 grafana 服务不可用,这会导致 nginx 在启动时死掉。因此,为了解决这个问题,我们为proxy_pass 指令使用了一个变量并将其更改为:

server {
    resolver 127.0.0.11 valid=30s;

    ...

    location /foobar/ {
        set $grafana http://grafana:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass $grafana/;
        # proxy_pass http://grafana:3000/;
        proxy_next_upstream error timeout http_502;
    }

}

但是,这会导致 grafana 以某种方式拒绝请求。我可以验证 grafana 正在 实际接收到请求(使用GF_SERVER_ROUTER_LOGGING=true),并且它声称状态是200 ok,但是我在页面上看到的唯一内容是

If you're seeing this Grafana has failed to load its application files 

1. This could be caused by your reverse proxy settings.

2. If you host grafana under subpath make sure your grafana.ini root_path setting includes subpath

3. If you have a local dev build make sure you build frontend using: npm run dev, npm run watch, or npm run build

4. Sometimes restarting grafana-server can help

为什么 grafana 会出现这样的行为,以及如何设置代理通道,以便 nginx 可以在启动时无需尝试解析 grafana URL(如果它碰巧关闭)?

【问题讨论】:

    标签: docker nginx grafana


    【解决方案1】:

    当使用变量时,完整的 URL 是您在代理传递中的责任

        location /foobar/ {
            set $grafana http://grafana:3000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass $grafana$request_uri;
            # proxy_pass http://grafana:3000/;
            proxy_next_upstream error timeout http_502;
        }
    

    如果基本路径不同,则需要使用正则表达式发送部分路径

        location ~ /foobar/(.*) {
            set $grafana http://grafana:3000;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass $grafana/$1;
            proxy_next_upstream error timeout http_502;
        }
    

    【讨论】:

    • 我不明白为什么,但第二个版本是有效的。
    猜你喜欢
    • 2017-04-22
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    相关资源
    最近更新 更多