繁体   English   中英

nginx.conf 忽略了 nginx-ingress 配置映射片段

[英]nginx-ingress config map snippets being ignored by the nginx.conf

提示:本站提供中英文对照查看,鼠标放在中文字句上可显示英文原文。 若本文未解决您的问题,推荐您尝试使用帮您解决。

我有一个 kubernetes 集群,在那里我使用helm nginx-ingress chart部署了一个 nginx 入口控制器。

我需要向在 nginx-controller-pod 中生成的 nginx.conf 文件添加一些自定义配置,并且我看到一个问题,如果我添加一个单行选项,例如proxy-buffer-size: "512k"我可以看到这反映在 nginx.conf 文件中,一切都按预期工作。

但是,如果我尝试添加一个片段来完成同样的事情:

location-snippet: |
  proxy_buffer_size "512k";

这好像被 nginx.conf 文件忽略了,并且proxy_buffer_size设置保持它的默认值。

我需要能够添加http-snippetserver-snippetlocation-snippet覆盖,但是无论我尝试将它们添加到 ConfigMap 还是作为 Ingress.yaml 文件中的注释,它们总是被忽略。

我的入口 yaml 文件:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    ingress.kubernetes.io/ssl-redirect: "true" 
    ingress.kubernetes.io/secure-backends: "true"    
    ingress.kubernetes.io/force-ssl-redirect: "true"

    ingress.kubernetes.io/location-snippet: |
       proxy_buffer_size 512k;     --This does not update the nginx.conf
spec:
  tls:
  - hosts:
    - my.app.co.uk
    secretName: tls-secret

  rules:
  - host: my.app.co.uk
    http:
      paths:
      - path: /
        backend:
          serviceName: myappweb-service
          servicePort: 80

我的 nginx 配置图:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: nginx-ingress
    chart: nginx-ingress-0.28.3
    component: controller
    heritage: Tiller
    release: nginx-ingress
  name: nginx-ingress-controller
  namespace: default
data:
  proxy-buffer-size: "512k" -- this works and updates the nginx.conf

  location-snippet: |
    proxy_buffers 4 512k; -- this does not update the nginx.conf

  server-snippet: |       -- this does not update the nginx.conf
    location /messagehub {
      proxy_set_header Upgrade $http_upgrade;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header Connection "upgrade";
      proxy_cache_bypass $http_upgrade;
   }

如果您想修改 Kubernetes Ingress ,注释选项如下:

  • nginx.ingress.kubernetes.io/configuration-snippet用于 nginx 位置块片段
  • nginx.ingress.kubernetes.io/server-snippet用于 nginx 配置服务块中的片段

看起来您正在使用nginx.org/location-snippets:对于这种情况。

在 nginx 配置示例中还有一个 YAML 无效语法,根据这个示例,您也应该像在server-snippets一样使用复数。 在撰写本文时, 文档中有一个错字。 打开这张票跟进。

它应该是这样的:

  server-snippets: |
    location /messagehub {
      proxy_set_header Upgrade $http_upgrade;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header Connection "upgrade";
      proxy_cache_bypass $http_upgrade;
      }

而不是这个:

  server-snippet: |
    location /messagehub {
      proxy_set_header Upgrade $http_upgrade;
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header Connection "upgrade";
      proxy_cache_bypass $http_upgrade;
    }

注意最后一个花括号的缩进。

事实证明,我的问题是由于我正在应用的代码片段的内容。 每次运行kubectl apply -f myconfigmap.yaml ,都会针对您尝试应用到 nginx.conf 的更改运行验证。 当此验证失败时,它会以静默方式失败,并且终端中没有任何提醒您这一点。

实际上,您仍然会收到configmap/nginx-ingress-controller configured消息。

例如,当我将其添加到配置映射时,它会按预期更新 nginx.conf:

http-snippet: |
  sendfile on;

但是,当我添加它时,没有任何改变:

http-snippet: |
  sendfile on;
  tcp_nopush on;

原因是这没有通过验证,但找到它的唯一方法是查看 nginx 入口控制器 pod 的日志。 在这种情况下,我看到:

Error: exit status 1
2018/10/16 07:45:49 [emerg] 470#470: "tcp_nopush" directive is duplicate in 
/tmp/nginx-cfg468835321:245
nginx: [emerg] "tcp_nopush" directive is duplicate in /tmp/nginx-cfg468835321:245
nginx: configuration file /tmp/nginx-cfg468835321 test failed

所以我复制了一个已经存在的指令。

我花了一天的时间,直到ingress-nginx (来自 Kubernetes)有*-snippet ,但对于nginx-ingress (来自 NGINX), *-snippetss

看这里:

问题未解决?试试使用:帮您解决问题。
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM