kubernetes nginx ingress http-snippet注释不生效



我使用正则表达式仅从$http_x_origin_forwarded_for中提取IP,因为它与端口一起出现,并且我只需要头的IP。所以我是这样使用入口的

ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/backend-protocol: https
nginx.ingress.kubernetes.io/http-snippet: |
map $http_x_origin_forwarded_for $forwarded-for-ip {
~^([0-9]+.[0-9]+.[0-9]+.[0-9]+):[0-9]+ $1;
default '';
};
nginx.ingress.kubernetes.io/configuration-snippet: |
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
服务器端的输出是
Header 'x-forwarded-for' = IP:PORT

和基于它,我的spring启动应用程序返回这个错误:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Aug 13 06:55:21 UTC 2021
There was an unexpected error (type=Internal Server Error, status=500).
Failed to parse address IP: PORT

我也试过这样

ingress:
enabled: true
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$1
nginx.ingress.kubernetes.io/backend-protocol: https
nginx.ingress.kubernetes.io/configuration-snippet: |
map $http_x_forwarded_for $real_ip {
~^(d+.d+.d+.d+) $1;
default $remote_addr;
}
proxy_set_header X-Forwarded-For $real_ip;

但是我得到了这个错误

2021/08/14 11:58:54 [emerg] 19862#19862: "map" directive is not allowed here in /tmp/nginx-cfg920601764:940
nginx: [emerg] "map" directive is not allowed here in /tmp/nginx-cfg920601764:940
nginx: configuration file /tmp/nginx-cfg920601764 test failed

根据您的k8s版本,在Ingress从beta版出来后(apiVersion: networking.k8s.io/v1v1beta1->apiVersion: networking.k8s.io/v1)

通过配置configmaps可以实现你想要的。用v1;入口使用另一个名称空间,在本例中称为ingress-nginx

添加以下configmaps:

apiVersion: v1
kind: ConfigMap
metadata:
name: ingress-nginx-controller
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
data:
proxy-set-headers: "ingress-nginx/custom-headers"
http-snippet: |
map $http_x_forwarded_for $real_ip {
~^(d+.d+.d+.d+) $1;
default $remote_addr;
}
--
apiVersion: v1
kind: ConfigMap
metadata:
name: custom-headers
namespace: ingress-nginx
data:
X-Forwarded-For: ${updated_x_trace_id}

所有的ingress-nginx控制器都会受到影响

map指令在configuration-snippet中不被支持。作为一种解决方法,您可以尝试使用if条件设置real_ip变量

的值。

相关内容

  • 没有找到相关文章

最新更新