環境準備html
server | IP |
---|---|
client | 172.20.27.10 |
haproxy | 172.20.27.20,192.168.27.10 |
nginx | 192.168.27.21 |
1.首先對nginx的主配置中的日誌作修改linux
[root@nginx ~]# vim /apps/nginx/conf/nginx.conf log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' #使用http透傳 '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' #使用tcp透傳 '"http_user_agent":"$http_user_agent",' '"status":"$status"}';
2.在配置文件中調用此文件nginx
[root@nginx ~]# vim /apps/nginx/conf/servers/mylinuxops.conf server { server_name www.mylinuxops.com; listen 80; access_log /apps/nginx/logs/mylinuxops.log access_json; location / { root /data/www; index index.html; } }
未使用http透傳前web
[root@nginx ~]# tail /apps/nginx/logs/mylinuxops.log {"@timestamp":"2019-06-04T16:30:47+08:00", "host":"192.168.27.21", "clientip":"172.20.27.10", "size":19, "responsetime":0.000, "upstreamtime":"-", "upstreamhost":"-", "http_host":"www.mylinuxops.com", "uri":"/index.html", "domain":"www.mylinuxops.com", "xff":"-", "referer":"-", "tcp_xff":"", "http_user_agent":"curl/7.29.0", "status":"200"} #xff顯示爲"-"
修改HAProxy配置文件,使用http模式下的ip透傳json
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg listen web bind 172.20.27.20:80 mode http #模式改成http option forwardfor #開啓forwardfor選項 server web1 www.mylinuxops.com:80 check inter 3000 fall3 rise 5
測試
使用客戶端訪問vim
[root@client ~]# curl www.mylinuxops.com www.mylinuxops.com
查看nginx上的日誌後端
[root@nginx ~]# tail -f /apps/nginx/logs/mylinuxops.log {"@timestamp":"2019-06-04T17:29:22+08:00", "host":"192.168.27.21", "clientip":"192.168.27.10", "size":19, "responsetime":0.000, "upstreamtime":"-", "upstreamhost":"-", "http_host":"www.mylinuxops.com", "uri":"/index.html", "domain":"www.mylinuxops.com", "xff":"172.20.27.10", "referer":"-", "tcp_xff":"", "http_user_agent":"curl/7.29.0", "status":"200"} #"xff":"172.20.27.10" 客戶端的地址被透傳過來了
1.修改HAProxy配置文件bash
[root@haproxy ~]# vim /etc/haproxy/haproxy.cfg listen web bind 172.20.27.20:80 mode tcp #將mode改成tcp option forwardfor #開啓forwardfor選項 server web1 www.mylinuxops.com:80 send-proxy check inter 3000 fall 3 rise 5 #在定義後端服務時加上 send-proxy參數。
2.修改nginx的配置文件app
[root@nginx ~]# vim /apps/nginx/conf/servers/mylinuxops.conf server { server_name www.mylinuxops.com; listen 80 proxy_protocol; #在listen選項後添加proxy_protocol選項 access_log /apps/nginx/logs/mylinuxops.log access_json; location / { root /data/www; index index.html; } }
測試
使用客戶端訪問dom
[root@client ~]# curl www.mylinuxops.com www.mylinuxops.com
在nginx上查看日誌
[root@nginx ~]# tail -f /apps/nginx/logs/mylinuxops.log {"@timestamp":"2019-06-04T17:43:57+08:00", "host":"192.168.27.21", "clientip":"192.168.27.10", "size":19, "responsetime":0.000, "upstreamtime":"-", "upstreamhost":"-", "http_host":"www.mylinuxops.com", "uri":"/index.html", "domain":"www.mylinuxops.com", "xff":"-", "referer":"-", "tcp_xff":"172.20.27.10", "http_user_agent":"curl/7.29.0", "status":"200"} #"tcp_xff":"172.20.27.10" 客戶端的地址在tcp的模式下被傳送過來