首页 > 系统相关 >NGINX Ingress Controller 设置未配置过的域名增加默认路由

NGINX Ingress Controller 设置未配置过的域名增加默认路由

时间:2024-04-17 21:44:06浏览次数:14  
标签:Ingress nginx default ingress 404 NGINX Controller html backend

背景

k8s 集群对应的公网 slb ip 经常被人绑定域名,监控侧经常会收集到 502 相关状态码的异常告警,着手处理这种bad case

策略

1.  所有没有在ingress 配置过的域名要进行处理,即不是公司的、非法绑定到slb 上的域名要加上一条策略
2. NGINX Ingress Controller 设置未配置过的域名增加一条默认路由,指向一个固定的服务

方法

  • Dockerfile
#cat Dockerfile 
FROM nginx:latest #一个nginx镜像即可
ADD defalut.conf /opt/openresty/nginx/conf/vhost/  #拷贝一个nginx 自定义的配置文件进去
RUN mkdir /export/html  #创建 404 图片放置的目录
COPY 404.jpeg /export/html/  #拷贝一张随意的404 图片
EXPOSE 80
CMD ["/opt/openresty/nginx/sbin/nginx","-g","daemon off;"]

  • default.conf 配置文件
#cat defalut.conf 
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    location / {
        root   /user/share/nginx/html;  #目录不用创建,且该目录也不用存在
        index  index.html index.htm;
        error_page  404              /404.jpeg; #定义404 的方法
        proxy_set_header HOST   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-FOR $proxy_add_x_forwarded_for;
    }

     location = /404.jpeg { #处理404 的方法
       root  /export/html;
     }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • 打镜像
  • 准备yaml 配置文件
# cat default-dp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: default-backend
  namespace: default-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: default-backend
  template:
    metadata:
      labels:
        app: default-backend
    spec:
      containers:
        - name: default-backend
          image: registry-vpc.cn-xxx.aliyuncs.com/xxx/nginx:default-server-v002
          ports:
            - containerPort: 80


# cat default-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: default-backend
  namespace: default-backend
spec:
  selector:
    app: default-backend
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

# cat default-ingress.yaml  (nginx.ingress.kubernetes.io/default-backend注解,并结合*通配符来匹配所有未配置的域名)
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default-backend
  annotations:
    nginx.ingress.kubernetes.io/default-backend: default-backend
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: default-backend
            port:
              number: 80
        path: /
        pathType: Prefix
  • 生成配置文件
# kubectl apply  -f default-dp.yaml 
# kubectl apply  -f default-svc.yaml 
# kubectl apply  -f default-ingress.yaml
  • 查看 ingress (* nginx.ingress.kubernetes.io/default-backend注解,并结合*通配符来匹配所有未配置的域名 )
# kubectl  get ingress -n default-backend 
NAME              CLASS   HOSTS   ADDRESS       PORTS   AGE
example-ingress   nginx   *       172.16.4.41   80      3h37m
  • 本地访问测试
# 随意一个域名绑定到k8s 对外的slb 地址上,显示 404 的图片

标签:Ingress,nginx,default,ingress,404,NGINX,Controller,html,backend
From: https://www.cnblogs.com/lixinliang/p/18141843

相关文章

  • docker部署nginx
    拉取镜像dockerpullnginx创建映射目录mkdir-p/data/server/nginx/{conf,log,html}先创建一个nginx容器,然后将容器中的nginx.conf文件和conf.d文件夹复制到宿主机创建容器dockerrun-d--namenginx01-p20080:80nginx将容器中的nginx.conf文件复制到宿主机docker......
  • docker compose部署nginx-proxy-manager
    dockercompose部署nginx-proxy-managerdocker-compose.yamlversion:'3'services:npm:image:jc21/nginx-proxy-manager:latestcontainer_name:npmrestart:alwaysenvironment:-TZ=Asia/Shanghaiports:-'30080......
  • Nginx部署安装
     #部署前准备工作#配置阿里云的yum源yum-yinstallwgetcd/etc/yum.repos.d/wgethttp://mirrors.aliyun.com/repo/Centos-7.repomvCentOS-Base.repoCentOS-Base.repo.bakmvCentos-7.repoCentOS-Base.repoyumcleanallyummakecacheyumupdate yum-y......
  • CentOS 7配置Nginx反向代理的步骤
    安装Nginx:如果你的服务器还没有安装Nginx,你可以使用以下命令来安装:sudoyuminstallepel-releasesudoyuminstallnginx创建或修改Nginx配置文件:默认的Nginx配置文件通常位于/etc/nginx/conf.d/目录下。你可以在这个目录下创建一个新的配置文件,例如yourdomain.conf:sudo......
  • nginx报错:bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a soc
    问题:1.nginx启动失败2.在logs/error.log文件下,出现报错信息:bind()to0.0.0.0:80failed(10013:Anattemptwasmadetoaccessasocketinawayforbiddenbyitsaccesspermissions) 目录:1、cmd输入命令netstat-aon|findstr"80"2.、查看80端口7532对应的任务3、......
  • Flask 生产环境部署(Falsk + uWSGI + nginx)
    前言最近自己做了个Falsk小项目,在部署上服务器的时候,发现虽然不乏相关教程,但大多都是将自己项目代码复制出来,不讲核心逻辑,不太简洁,于是将自己部署的经验写成内容分享出来。uWSGI简介uWSGI:一种实现了多种协议(包括uwsgi、http)并能提供服务器搭建功能的Python包Differenc......
  • 13、web 中间件加固-Nginx 加固
    1.隐藏版本信息避免被针对版本直接使用漏洞修改nginx.conf文件在http模块中添加信息:server_tokensoff;2.限制目录权限某些目录为运维页面,不要公开访问编辑nginx.conf文件在server标签内添加内容:location~/attachments/.*\.(php|php5)?${denyall;}locatio......
  • nginx的反向代理upstream
    一、安装nginx查看这篇博客 https://www.cnblogs.com/zrl66/p/14118063.html二、负载均衡upstream下面就是访问www.qwe.com域名反向代理,然后请求转到upstreambackend权重配置,根据配置进行访问。http{......upstreambackend{serverwww.......
  • 安装nginx时报错解决(configure error: the HTTP gzip module requires the zlib libra
    安装nginx时报错解决下载地址nginx源码包下载地址:https://nginx.org/en/download.html安装环境Ubuntu20.04LTSnginx-1.23.4安装步骤#解压缩包tar-zxvfnginx-1.23.4.tar.gz#进入包目录cdnginx-1.23.4#生成makefile./configure就在./configure这一步出现了依......
  • 统一controller返回对象
    统一controller返回对象packagecom.xxx.jscaffold.handler;importcom.xxx.jscaffold.api.dto.common.Result;importorg.springframework.core.MethodParameter;importorg.springframework.http.MediaType;importorg.springframework.http.converter.HttpMessageConver......