开启 gzip 特性
gzip in gke with nginx-ingress
针对 Ingress Controller 服务,开启 gzip 特性
use-gzip: true; | https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#use-gzip
gzip-types: “*”; | https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#gzip-types
修改 ConfigMap 资源,增加如上配置:
kubectl edit configmap -n ingress-nginx ingress-nginx-controller
针对特定 Ingress 资源,开启 gzip 特性
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: cafe-ingress annotations: kubernetes.io/ingress.class: "nginx" nginx.org/server-snippets: "gzip on;" ...
配置 GeoIP 模块
How to block a country in Nginx ingress? : kubernetes
# 07/03/2022 我们暂时放弃通过 GeoIP 来实现安全访问控制,而采用其他方案。所以,我们这里仅简单记录配置思路,而具体配置可能存在错误;
NGINX Ingress Controller 支持 MaxMind GeoIP2 数据库,使用流程如下:
- 申请 MaxMind 的许可 | IP Geolocation and Online Fraud Prevention | MaxMind
- 调整 NGINX Ingress Controller 的配置,以使用许可 | NGINX Ingress Controller/ConfigMap/use-geoip2
- 通过 Annotation 进行相关配置:
... metadata: annotations: nginx.ingress.kubernetes.io/server-snippet: | # server block map $geoip2_city_country_code $blocked_country { default 0; AU 1; FR 1; } nginx.ingress.kubernetes.io/configuration-snippet: | # location block if ($blocked_country) { return 403; } ...
暴露 gRPC 服务
问题描述
通过 Ingress 无法访问到其后的 gRPC 服务;
原因分析
未在 Ingress 资源中设置 Annotation,指定后端协议类型;
gRPC 服务只能通过 TLS 端口进行访问;
解决方案
要满足两个条件:
1)在对应 Ingress 资源中,设置 Annotation: nginx.ingress.kubernetes.io/backend-protocol:”GRPC”;
2)在确认客户端发送请求时,使用的是 TLS 端口,并且将流量加密;
场景 | 通过路径来区分服务
Kubernetes Cluster / Nginx Ingress Controller;
前端项目,静态页面,容器内 Nginx 提供访问;
通过路径 /web 来分流,以转发到对应的 Service 中;
页面静态资源使用相对路径,以保证静态资源请求能够进入后端容器;
同时,我们也不考虑修改代码。
方案一、通过 URL 重写
在 Container 中,站点需要放在根目录中。
在 Ingress 中,移除 /web 路径
… path: /web(/|$)(.*)
缺点:
1)需要访问 example.com/web/ 地址,如果使用 example.com/web 访问,则页面引用的静态资源无法正常访问。
方案二、通过 web 路径
在 Ingress 中,需要使用如下配置
… nginx.ingress.kubernetes.io/ssl-redirect: “true”
… nginx.ingress.kubernetes.io/preserve-trailing-slash: “true”
在 Container 中,站点需要存放在 /web/ 中,否则将返回 404 页面。
访问 example.com/web 将自动重定向到 example.com/web/ 地址 ⇒ 该行为与生成的 nginx.conf 及其 location 特性有关。
https://example.com/web 请求到达后端容器,后端容器 Nginx 因监听 HTTP 而将其重定向到
http://example.com/web/ 地址,而后 Ingress Nginx 再次重定向到
https://example.com/web/ 地址。
http://example.com/web/ 会被 Ingress Nginx 重定向为
https://example.com/web 地址,然后 后端容器 Nginx 又将其重定向为
http://example.com/web/ 地址,进而陷入循环。
场景 | 跨域配置
ingress-nginx/docs/user-guide/nginx-configuration/annotations.md at main · kubernetes/ingress-nginx
nginx.ingress.kubernetes.io/enable-cors: “true”
nginx.ingress.kubernetes.io/cors-allow-headers
cors-allow-headers does not allow wildcard · Issue #10501 · kubernetes/ingress-nginx
Accept wildcard in nginx.ingress.kubernetes.io/cors-allow-headers annotation by croemmich · Pull Request #11655
Nginx Ingress Controller, App Version 1.11.1, Chart Version 4.11.1
现在(11/11/2024)该字段无法使用 * 通配置,其会被解析为具体的值。
Request Entity Too Large | 413 | proxy-body-size
nginx.ingress.kubernetes.io/proxy-body-size: “20m”
nginx.ingress.kubernetes.io/ssl-redirect: “false”
关闭 http 到 https 的重定向