通过minikube tunnel访问localhost/grafana时Grafana报404错误求助
问题分析
你碰到的404错误核心原因是Ingress规则的host与实际访问的host不匹配,再加上Grafana的路径配置没和Ingress的rewrite规则对齐:
- 你的Ingress配置的host是
chart-example.local,但用minikube tunnel后访问的是localhost,Nginx Ingress Controller会因为host不匹配找不到对应的转发规则,直接返回404。 - Grafana的
root_url和domain配置没和实际访问的域名/路径对应,就算Ingress匹配上,也可能导致页面资源加载异常。
解决方案
1. 调整Ingress配置,适配localhost访问
修改custom-values.yml里的Ingress部分,把localhost加到hosts列表,保证访问localhost时能匹配到Ingress规则:
ingress: enabled: true annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/rewrite-target: /$1 nginx.ingress.kubernetes.io/use-regex: "true" path: /grafana/?(.*) pathType: Prefix hosts: - chart-example.local - localhost # 添加该行,让Ingress同时匹配两个host
2. 修正Grafana的root_url和domain配置
Grafana的root_url要和实际访问的URL一致,domain设为你实际访问的域名(这里是localhost),同时保持serve_from_sub_path: true:
grafana.ini: server: domain: "localhost" # 直接设置为实际访问的域名 root_url: "%(protocol)s://%(domain)s/grafana" # 自动适配协议和域名,带上子路径 serve_from_sub_path: true
3. 重新部署Grafana
执行helm升级命令,应用修改后的配置:
helm upgrade grafana grafana/grafana -f custom-values.yml
4. 验证配置
- 先确认Ingress资源已更新:
kubectl get ingress
查看输出里是否包含localhost的host。
- 启动
minikube tunnel后,在浏览器访问http://localhost/grafana,应该能正常加载Grafana页面。
补充说明
如果只需要用localhost访问,可以直接把Ingress的hosts只保留localhost,避免多余规则匹配。另外要确保你是通过minikube addons enable ingress启用的Nginx Ingress Controller,且minikube tunnel运行正常,能把Ingress Controller的LoadBalancer IP映射到本地。
内容的提问来源于stack exchange,提问作者Cake
相关产品推荐
相关产品推荐

