Azure Kubernetes Service中Nginx 405 Not Allowed问题:如何改为GET请求
解决AKS中Application Gateway Ingress POST请求405错误的方法
你的问题核心是后端静态页面服务(如Nginx)仅支持GET/HEAD等方法,前端发送的POST请求被拒绝返回405。可以通过Azure Application Gateway Ingress Controller(AGIC)的请求重写功能,将POST请求转换为GET请求后转发给后端服务。
步骤1:创建请求重写规则的ConfigMap
创建一个ConfigMap,定义将POST方法改写为GET的规则:
apiVersion: v1 kind: ConfigMap metadata: name: rewrite-post-to-get namespace: xxx # 与Ingress同命名空间 data: rewriteRules.json: | [ { "ruleSequence": 100, "conditions": [ { "variable": "request_method", "operator": "Equal", "negate": false, "values": ["POST"] } ], "actionSet": { "requestHeaderConfigurations": [], "responseHeaderConfigurations": [], "urlConfiguration": { "modifiedPath": "", "modifiedQueryString": "", "actionType": "None" }, "requestMethodConfiguration": { "method": "GET" } } } ]
步骤2:修改Ingress资源关联重写规则
在你的Ingress资源的metadata.annotations中添加注解,关联上面创建的ConfigMap:
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: appgw.ingress.kubernetes.io/appgw-ssl-certificate: cert kubernetes.io/ingress.class: azure/application-gateway # 添加以下注解关联重写规则ConfigMap appgw.ingress.kubernetes.io/rewrite-rule-set: rewrite-post-to-get creationTimestamp: "2022-08-01T11:43:56Z" generation: 12 # 版本号递增 labels: app.kubernetes.io/instance: xxx app.kubernetes.io/managed-by: Helm app.kubernetes.io/name: xxx helm.sh/chart: XXXX-0.0.1-SNAPSHOT name: XXXX-ingress namespace: xxx resourceVersion: "xxx" uid: xxxx spec: rules: - host: manuals.dat.de http: paths: - backend: service: name: xxx-svc port: number: 80 path: / pathType: Prefix status: loadBalancer: ingress: - ip: xx.xx.xx.xx
说明
- 这个重写规则会匹配所有发送到该Ingress的POST请求,将请求方法替换为GET后再转发给后端服务,这样后端静态页面服务就能正常响应,不会返回405错误。
- 确保ConfigMap和Ingress处于同一个命名空间,否则AGIC无法找到对应的规则集。
- 修改Ingress后,AGIC会自动同步配置到Application Gateway,无需手动操作网关。
内容的提问来源于stack exchange,提问作者Vivek Jain
相关产品推荐
相关产品推荐

