-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nginx remoteip #146
Comments
为了确保 Kong Gateway 能获取客户端的出口 IP 地址,您需要确保在整个请求中转途经的每一层代理都正确转发这个IP地址。 在您提供的配置中,L7 Nginx 已经设置了 X-Real-IP 和 X-Forwarded-For 头信息来转发原始客户端IP。现在关键是确保L4 Nginx 能正确地将这些信息再次转发给 Kong Gateway。 L4 Nginx的配置需要确保它不会覆盖 X-Real-IP 和 X-Forwarded-For 头信息。这通常意味着在相关的 stream 或 server 配置块中要设置相关头信息的转发,不过要注意的是,在处理TCP/SSL流量时,代理不会直接访问到HTTP头信息。 如果您的L4 Nginx是按照四层(传输层)来转发SSL/TCP连接,而没有解包HTTP层。那么L4级别的Nginx将无法修改或添加任何HTTP头部信息。在这种情况下,您需要确保L7 Nginx直接与Kong Gateway通信,或者您的L4 Nginx需要能够解包TCP流以编辑HTTP请求(这将涉及到SSL的终止和创建新的SSL连接)。 一般而言,以下两点是确保Kong Gateway获取客户端出口IP地址的关键所在:
yaml 确保L4 Nginx保留了从L7 Nginx传入的 |
理解到你使用的是L4级别的Nginx,它只处理TCP/SSL连接,而不会解包HTTP层内容,所以L4 Nginx不能直接修改或添加HTTP头部信息。在这种情况下,要使Kong Gateway获取客户端的真实IP地址,必须通过Nginx的 1. 配置L7 NginxL7 Nginx仍然负责处理HTTP请求,并将原始客户端IP传递到L4 Nginx。这部分配置不会修改: http {
server {
listen 80;
location /apiname/v1 {
if ($host !~* aibangjuxin$ ) {
return 444;
}
if ($content_type ~ (multipart/form-data|text/plain)) {
return 405;
}
rewrite ^/apiname/v1(.*)$ $1 break;
proxy_pass https://10.10.15.1:8081;
proxy_set_header Host dev-api.cn.aibang;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
} 2. 配置L4 Nginx在L4 Nginx上启用 stream {
server {
listen 8081 proxy_protocol; # 启用 proxy_protocol
proxy_pass 192.168.0.5:8443; # 假设 Kong Gateway 运行在 192.168.0.5:8443
proxy_protocol on; # 启用 proxy_protocol 支持
}
} 3. 配置Kong GatewayKong Gateway必须配置为接受并解析来自 配置Kong的Nginx模板如果Kong使用的是Nginx作为前端,那么需要配置Nginx模板以启用 server {
listen 8443 ssl proxy_protocol; # 启用 proxy_protocol
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
location / {
proxy_pass http://upstream_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr; # 使用 proxy_protocol_addr 获取真实的客户端IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
} 更新Kong的配置文件确保Kong的配置文件接受并解析 nginx_http_real_ip_header: "proxy_protocol"
nginx_http_real_ip_recursive: "on"
trusted_ips:
- 0.0.0.0/0 完整流程图以下是更新后的流程图,展示了从L7 Nginx到Kong Gateway的请求流动过程: graph TD
A(Client) -->|HTTPS| B(Nginx_L7)
B(Nginx_L7) -->|proxy_pass + X-Forwarded-For| C(Nginx_L4)
C(Nginx_L4) -->|proxy_pass + proxy_protocol| D(Kong_GW)
D(Kong_GW) --> E(Upstream_Service)
subgraph Network
B
C
D
end
故障排除如果启用
通过以上配置和故障排除步骤,可以确保Kong Gateway能够获取并使用客户端的真实IP地址。 |
在L4层Nginx中启用 配置步骤1. L7 Nginx 配置首先,L7 Nginx负责处理HTTP请求并将其转发到L4 Nginx,同时传递原始客户端IP地址: http {
server {
listen 80;
location /apiname/v1 {
if ($host !~* aibangjuxin$ ) {
return 444;
}
if ($content_type ~ (multipart/form-data|text/plain)) {
return 405;
}
rewrite ^/apiname/v1(.*)$ $1 break;
proxy_pass https://10.10.15.1:8081;
proxy_set_header Host dev-api.cn.aibang;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
} 2. L4 Nginx 配置L4 Nginx负责接收从L7 Nginx传来的请求,并使用 stream {
server {
listen 8081 proxy_protocol;
proxy_pass 192.168.0.5:8443; # 假设 Kong Gateway 运行在 192.168.0.5:8443
proxy_protocol on;
}
} 3. Kong Gateway 配置确保Kong Gateway配置正确接收和解析 Kong 的 Nginx 模板配置: 在Kong的Nginx模板配置文件中,启用 server {
listen 8443 ssl proxy_protocol; # 启用 proxy_protocol
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
location / {
proxy_pass http://upstream_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr; # 使用 proxy_protocol_addr 获取真实的客户端IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
} Kong 配置文件: 确保在Kong的配置文件中设置了正确的头信息解析: nginx_http_real_ip_header: "proxy_protocol"
nginx_http_real_ip_recursive: "on"
trusted_ips:
- 0.0.0.0/0 故障排除
整体流程图以下是更新后的流程图,展示从L7 Nginx到Kong Gateway的请求流动过程: graph TD
A(Client) -->|HTTPS| B(Nginx_L7)
B(Nginx_L7) -->|proxy_pass + X-Forwarded-For| C(Nginx_L4)
C(Nginx_L4) -->|proxy_pass + proxy_protocol| D(Kong_GW)
D(Kong_GW) --> E(Upstream_Service)
subgraph Network
B
C
D
end
示例配置假设你有一个基本的设置,以下是各个部分的示例配置: L7 Nginx: http {
server {
listen 80;
location /apiname/v1 {
if ($host !~* aibangjuxin$ ) {
return 444;
}
if ($content_type ~ (multipart/form-data|text/plain)) {
return 405;
}
rewrite ^/apiname/v1(.*)$ $1 break;
proxy_pass https://10.10.15.1:8081;
proxy_set_header Host dev-api.cn.aibang;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
} L4 Nginx: stream {
server {
listen 8081 proxy_protocol;
proxy_pass 192.168.0.5:8443;
proxy_protocol on;
}
} Kong Gateway: server {
listen 8443 ssl proxy_protocol;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
location / {
proxy_pass http://upstream_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
} Kong 配置文件: nginx_http_real_ip_header: "proxy_protocol"
nginx_http_real_ip_recursive: "on"
trusted_ips:
- 0.0.0.0/0 通过确保各层之间的 |
要让 Kong Gateway 获取到客户端的真实 IP 地址,你需要在 Kong 的配置中指定获取客户端 IP 的方式。Kong 支持从不同的 HTTP 头中获取客户端 IP,包括
X-Forwarded-For
和X-Real-IP
。以下是一些建议:
在 Kong 的配置文件中,你可以指定从哪个 HTTP 头中获取客户端 IP。例如,如果你想从
X-Real-IP
头中获取 IP,可以这样配置:由于 Nginx L7 已经在
X-Real-IP
头中设置了客户端 IP,你可以让 Nginx L4 转发这个头部到 Kong Gateway。在 Nginx L4 的配置中添加:这样 Kong Gateway 就可以从
X-Real-IP
头中获取客户端真实 IP 了。如果你更倾向于使用
X-Forwarded-For
头,可以在 Nginx L4 中这样配置:然后在 Kong 中配置从
X-Forwarded-For
头获取 IP:通过上述配置,Kong Gateway 应该可以获取到客户端的真实出口 IP 地址了。记得在做任何更改后重新加载 Nginx 和 Kong 的配置。
The text was updated successfully, but these errors were encountered: