Nginx Ingress对集群服务(Service)中外部可访问的API对象进行管理,提供七层负载均衡能力。本文总结使用nginx-ingress过程中的一些经验。
域名重定向 Redirect
redirect主要用于域名重定向,比如访问a.com被重定向到b.com。
只能保留path
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: xxx-ingress
kubernetes.io/ingress.rule-mix: "true"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/temporal-redirect: "https://xxxxxxxxxxxx.com/$1"
name: xxxxx
spec:
rules:
- host: yyyyyyyyyyyy.com
http:
paths:
- backend:
serviceName: xxxxx
servicePort: 80
path: /(.*)
pathType: ImplementationSpecific
stackoverflow.com/questions/5…
根据url参数重定向
nginx.ingress.kubernetes.io/server-snippet: |
set $agentflag 0;
if ($args ~* "destination_env=prod" ){
set $agentflag 1;
}
if ($args ~* "destination_env=test" ){
set $agentflag 2;
}
if ( $agentflag = 1 ) {
return 307 https://api-xxxxx.baidu.com$request_uri;
}
if ( $agentflag = 2 ) {
return 307 https://api-xxxxxx-test.baidu.com$request_uri;
}
stg环境nginx收到请求,重定向后不是直接转发给prod环境,而是回消息给发起方,让他按新的url重新发送请求。
如果调用方HttpClient不支持重定向,这个方案是不行的。
网上找到nginx的一个实践,直接nginx进行转发。
例如 api.example.com/?boole=1234…
plat=mix_[a|b|c|d]时,转发到api-bgp.example.com
location / {
proxy_redirect off;
proxy_set_header Host api-bgp.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
if ( $query_string ~ "\bplat=mix_(a|b|c|d)\b" ){
proxy_pass $scheme://1.1.1.1:$server_port;
}
index index.php index.html index.htm;
}
但是在Ingress上配置
nginx.ingress.kubernetes.io/configuration-snippet: |
if ( $query_string ~ "\bdestination_env=prod\b" ){
proxy_pass http://api-xxxxx.baidu.com;
}
if ( $query_string ~ "\bdestination_env=test\b" ){
proxy_pass http://api-xxxxx-test.baidu.com;
}
请求报“404 Not Found”,不可行。
配置单个服务的上传文件大小限制
proxy-body-size
Sets the maximum allowed size of the client request body. See NGINX client_max_body_size