Configuring NGINX to accept the PROXY Protocol
nginx作为tcp负载均衡
http {
log_format combined '$proxy_protocol_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
...
server {
server_name localhost;
listen 80 proxy_protocol;
listen 443 ssl proxy_protocol;
ssl_certificate /etc/nginx/ssl/public.example.com.pem;
ssl_certificate_key /etc/nginx/ssl/public.example.com.key;
set_real_ip_from 192.168.1.0/24;
real_ip_header proxy_protocol;
location /app/ {
proxy_pass http://backend1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $proxy_protocol_addr;
proxy_set_header X-Forwarded-For $proxy_protocol_addr;
}
}
}
stream {
...
server {
listen 12345;
proxy_pass example.com:12345;
proxy_protocol on;
}
}
- 接受tcp proxy代理流量
该示例假设在NGINX前面有一个负载均衡器,例如Amazon ELB,它平衡所有传入的HTTPS流量
NGINX接受端口443上的HTTPS流量(listen 443 ssl;),和80端口的流量
并通过PROXY协议(proxy_protocol参数)接受从负载平衡器传递的客户端的IP地址。负载均衡器地址在set_real_ip_from指令中指定。客户端的IP地址通过proxy_protocol参数从real_ip_header指令传递
具体代码在:ngx_proxy_protocol_read 中解析proxy协议得到客户端的IP地址 - 作为tcp的负载均衡器/反向代理
具体参考Module ngx_stream_core_module
在看到ngx-rtmp代码中ngx_rtmp_init_connection 初始化的时候,有关于proxy_protocol的时候有点不明白,这个链接或者流也是从负载均衡器传过来的吗?
个人觉得:这个链接建立开始发送的是proxy_ptotocol的header,后续通过这个connection发送的数据都不加头,只是tcp数据而已了
listen 1936 proxy_protocol