1.测试环境:kubesphere 3.3.1
2.网关模式:Nodeport网关(nginx-ngress)
3.测试容器:nginx,对应测试域名(https://xxxxxx.xxxxx.cn)
4.全部默认时
网关日志
log_format upstreaminfo '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id';
nginx容器日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
5.添加日志字段
网关日志
log_format upstreaminfo '$remote_addr $http_x_forwarded_for - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_length $request_time [$proxy_upstream_name] [$proxy_alternative_upstream_name] $upstream_addr $upstream_response_length $upstream_response_time $upstream_status $req_id';
nginx容器日志
没做修改,所以获取结果一样
6.阶段结论,网关的nginx能通过$http_x_forwarded_for获取到用户IP,但是后端容器通过$http_x_forwarded_for获取不到。
7.给网关的nginx修改配置
kubectl exec -it kubesphere-router-kubesphere-system-76855d5b4b-tvkrv /bin/bash -n kubesphere-controls-system
修改 /etc/nginx/nginx.conf,关于ngxx.zjwlyy.cn段的配置,
保存并 nginx -s reload。
proxy_set_header X-Forwarded-For $http_x_forwarded_for
注释:
$proxy_add_x_forwarded_for:包括客户端请求头的X-Forwarded-For和$remote_addr,也就是包含真实用户IP和前面所有代理的列表,第一位为用户真实IP,逗号分隔
$http_x_forwarded_for:就是X-Forwarded-For的值,能够直接拿到用户真实IP
8.访问日志
网关日志没有变化
nginx容器日志