nginx - 如何自定义通过 nginx 入口 Controller 的默认后端提供的错误页面?

标签 nginx kubernetes

我正在运行一个通过 Helm 在我的 Kubernetes 集群上安装的 Nginx Ingress Controller。我想针对某些特定错误(例如 404)更改默认后端服务中的 HTML/CSS。

This链接提供了一些关于默认后端的一般信息。但是,没有提及如何实际自定义提供的 HTML/CSS 文件。

最佳答案

使用官方ingress-nginx/nginx-errors从 ConfigMap 图像和映射您的自定义页面。这样你就不必建立自己的形象。
如果使用 Helm 进行部署,这里是示例 values.yaml文件。

# See https://github.com/kubernetes/ingress-nginx/blob/main/charts/ingress-nginx/values.yaml
controller:
    custom-http-errors: "404,500,503" # See https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#custom-http-errors
defaultBackend:
  enabled: true
  image:
    registry: k8s.gcr.io
    image: ingress-nginx/nginx-errors # Source https://github.com/kubernetes/ingress-nginx/tree/main/images/custom-error-pages
    tag: "0.48.1" # Check latest version on https://github.com/kubernetes/ingress-nginx/blob/main/docs/examples/customization/custom-errors/custom-default-backend.yaml
  extraVolumes:
  - name: error_page
    configMap:
      name: error_page
      items:
      - key: "error_page"
        path: "404.html"
      - key: "error_page"
        path: "500.html"
      - key: "error_page"
        path: "503.html"
  extraVolumeMounts:
  - name: error_page
    mountPath: /www
示例错误页面配置映射文件:
apiVersion: v1
kind: ConfigMap
metadata:
  name: error_page
  namespace: ingress-nginx
data:
  error_page: |
    <!DOCTYPE html>
    <html>
        <head>
            <title>ERROR PAGE</title>
        </head>
        <body>
          ERROR PAGE
        </body>
    </html>

关于nginx - 如何自定义通过 nginx 入口 Controller 的默认后端提供的错误页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60233958/

相关文章:

python - uwsgi内存消耗逐渐增加但消耗的内存没有释放

javascript - 客户端支持双栈ipv4+ipv6时获取用户ipv4

kubernetes - 如何获取包含从 helm chart 填充的所有值的 list 文件?

java - jQuery 调用 REST API 在 Docker/Kubernetes 中不起作用

kubernetes - kubectl 删除具有部署名称的 pod

javascript - NodeJS、Express、Nginx 和 Jade...有什么关系?

node.js - "systemctl start pm2-user-name"给出错误

kubernetes - 无法使用 terraform cloud 在 Kubernetes 中创建 secret

docker - 通过 microk8s 获取 "x509: certificate signed by unknown authority"

python - 在 Tornado 中静默关闭 websockets