AKS集群中通过Nginx Ingress反向代理暴露Kibana遇404问题求助
Hey there, let's figure out why you're getting that 404 when trying to access Kibana through your Nginx Ingress. The issue is likely a mismatch between your Ingress rewrite rules and Kibana's path configuration—let's fix that step by step.
First, Fix the Ingress Rewrite Target
Looking at your Ingress.yaml, you're using an outdated generic rewrite annotation and setting it to /, which causes all requests to /kibana/* to be forwarded to Kibana's root path. Since you've set a base path for Kibana, we need to pass the correct path segment to the backend.
Update your Ingress configuration like this:
apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: ingress-dev annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/use-regex: "true" # Use nginx-specific rewrite annotation instead of the generic one nginx.ingress.kubernetes.io/rewrite-target: "/$2" nginx.ingress.kubernetes.io/backend-protocol: "HTTP" spec: rules: - host: example.com http: paths: - backend: serviceName: frontend-ui-service servicePort: 80 path: /(.*) - backend: serviceName: home-micro-service servicePort: 3333 path: /api-dev(/|$)(.*) - backend: serviceName: kibana-kibana servicePort: 5601 path: /kibana(/|$)(.*)
The key change is nginx.ingress.kubernetes.io/rewrite-target: "/$2". Your path regex /kibana(/|$)(.*) captures two groups: the first is / or empty, the second is the rest of the path. Using $2 tells Nginx to forward everything after /kibana to Kibana's backend, which matches what Kibana expects.
Next, Configure Kibana to Handle the Base Path
You added SERVER_BASEPATH but missed a critical setting: server.rewriteBasePath. When you set a base path, Kibana needs to know to rewrite incoming requests that don't include the base path (since your Ingress strips /kibana before forwarding).
Update your Kibana values.yaml with either of these options:
Option 1: Using Environment Variables
extraEnvs: - name: SERVER_BASEPATH value: "/kibana" - name: SERVER_REWRITEBASEPATH value: "true"
Option 2: Using kibanaConfig (more explicit)
kibanaConfig: kibana.yml: | server.basePath: "/kibana" server.rewriteBasePath: true
Why This Works
- The Ingress strips the
/kibanaprefix from requests and forwards the remaining path to Kibana. server.rewriteBasePath: truetells Kibana to adjust its internal routing to match the stripped path, so it doesn't look for resources under/kibanaanymore (the Ingress already handles that part).
Apply the Changes
Run these commands to update your resources:
# Update Kibana deployment helm upgrade kibana elastic/kibana -f your-kibana-values.yaml # Apply the updated Ingress kubectl apply -f Ingress.yaml
Wait a minute for the Kibana pod to restart and the Ingress to reload, then try accessing http://example.com/kibana again—it should load without the 404!
内容的提问来源于stack exchange,提问作者abh.bhardwaj

