我的目标是通过 GCE 入口通过 nginx 服务的 Kubernetes Engine 上部署的 Angular 应用程序的 IP 地址过滤访问。
但是在我的 nginx 上,remote_addr 是不正确的。
$LB_IP 是这里定义的 ip:kubernetes.io/ingress.global-static-ip-name: app-angular
我在 nginx 上使用 set_real_ip_from 从 X-Forwarded-For 设置 ip
set_real_ip_from $LB_IP;
real_ip_header X-Forwarded-For;
正如谷歌文档所预期的那样,原始 ip 出现在 X-Forwarded-For 标头中:https ://cloud.google.com/compute/docs/load-balancing/http/#components
我可以看到 X-Forwarded-For 包含 $CLIENT_IP 但 remote_addr 不正确,顺便说一下我的 IP 过滤器不起作用。任何想法 ?
我的 nginx 日志:
10.40.40.40 - - [07/Feb/2018:11:29:48 +0000] "GET /styles.bundle.css HTTP/1.1" 200 35908 " http://MY_URL/home " "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36" "$CLIENT_IP, $LB_IP"
但我想要 :
$CLIENT_IP - - [07/Feb/2018:11:29:48 +0000] "GET /styles.bundle.css HTTP/1.1" 200 35908 " http://MY_URL/home " "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36" "$CLIENT_IP, $LB_IP"
我的入口配置:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: app-angular
spec:
rules:
- host: MY_URL
http:
paths:
- backend:
serviceName: app-backend
servicePort: 80
我的后端配置:
apiVersion: v1
kind: Service
metadata:
name: app-backend
spec:
type: NodePort
selector:
app: app-angular
ports:
- port: 80
targetPort: 80
我的部署配置:
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: app-angular
spec:
replicas: 2
revisionHistoryLimit: 2
template:
metadata:
labels:
app: app-angular
tier: frontend
spec:
containers:
- name: app-angular
image: gcr.io/MY_PROJECT/app-angular:MY_TAG
imagePullPolicy: Always
ports:
- containerPort: 80
我的 nginx 配置:
server {
listen 80;
root /usr/share/nginx/html;
location / {
set_real_ip_from $LB_IP;
real_ip_header X-Forwarded-For;
allow 130.211.0.0/22; #Google IP
allow $CLIENT_IP; # FILTERED IP
allow 10.0.0.0/8; #internal network
deny all;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
任何想法 ?