ingres nginx 反向代理常见配置(持续更新)

一、匹配到的location的关键字不传递给后端

如果想实现访问的url 匹配到的location,不携带给后端服务

类似nginx的配置如下

可以看到 proxy_pass http://chatbot_flask/; 后面是带了"/"的,这代表反向代理给后端的upstream节点不携带"im_chat"这个关键字.

假设我的请求是https://xxx.xxx.com/im_chat/v1/chat/create

当请求到达nginx时,nginx会将/v1/chat/create 这段url反向代理访问后端的节点

   location /im_chat {
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-Port $server_port;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://chatbot_flask/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_connect_timeout   60s;
            proxy_send_timeout      60s;
            proxy_read_timeout      60s;
            proxy_buffering off;
    }

重点来了对应的k8s ingress nginx 的配置如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: chatbot
  namespace: bmm-system
  annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: 100M
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-next-upstream: error timeout http_500 http_502 http_503 http_504
    nginx.ingress.kubernetes.io/proxy-read-timeout: "600"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "600"
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  ingressClassName: "ingress-xxx-ext-nginx1"
  tls:
   - hosts:
     - xxx.xxx.cn
     secretName: xxx-tls
  rules:
    - host: xxx.xxx.cn
      http:
        paths:
          - pathType: ImplementationSpecific  
            backend:
              service:
                name: chatbot
                port:
                  number: 5001
            path: /im_chat(/|$)(.*)

你可能感兴趣的:(kubernetes,kubernetes,ingress-nginx)