前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Ingress错误代码友好页面(二)

Ingress错误代码友好页面(二)

原创
作者头像
玖叁叁
发布2023-05-05 09:38:18
5700
发布2023-05-05 09:38:18
举报
文章被收录于专栏:玖叁叁

接下来,需要将ConfigMap与Ingress关联起来。

代码语言:javascript
复制
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/custom-http-errors: "404,500"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      error_page 404 /custom_404.html;
      error_page 500 /custom_500.html;
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /example
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80

在上面的配置中,nginx.ingress.kubernetes.io/configuration-snippet注解用于指定在Nginx中使用自定义页面的配置。例如,上述配置指定了404和500错误代码的自定义页面,以及Nginx中的自定义页面路径。接下来,需要在ConfigMap中定义这些自定义页面。

代码语言:javascript
复制
apiVersion: v1
kind: ConfigMap
metadata:
  name: custom-http-errors
data:
  custom_404.html: |
    <!DOCTYPE html>
    <html>
    <head>
        <title>404 Not Found</title>
    </head>
    <body>
        <h1>404 Not Found</h1>
        <p>The requested URL was not found on this server.</p>
    </body>
    </html>
  custom_500.html: |
    <!DOCTYPE html>
    <html>
    <head>
        <title>500 Internal Server Error</title>
    </head>
    <body>
        <h1>500 Internal Server Error</h1>
        <p>The server encountered an internal error or misconfiguration and was unable to complete your request.</p>
    </body>
    </html>

在上面的配置中,data字段定义了404和500错误代码的自定义页面。这些页面的内容以HTML格式提供,并且文件名必须与Nginx中使用的自定义页面路径相匹配。最后,需要将ConfigMap与Ingress关联起来。

代码语言:javascript
复制
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  annotations:
    nginx.ingress.kubernetes.io/custom-http-errors: "404,500"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      error_page 404 /custom_404.html;
      error_page 500 /custom_500.html;
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /example
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80
  errorPages:
  - code: 404
    path: /custom_404.html
  - code: 500
    path: /custom_500.html

在上面的配置中,errorPages字段定义了404和500错误代码的自定义页面,并指定了页面的路径。这些页面路径必须与ConfigMap中定义的路径匹配。最后,应用配置即可,以便使这些配置生效。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档