How to Get the Source Address of the Client?
CLB supports two types of message forwarding and request proxy. Message forwarding supports protocols such as TCP, UDP, and request proxy supports protocols such as HTTP, HTTPS, TCP.
In the message forwarding mode, the source address of the request received by the backend service node is the actual source address.
In the request proxy mode, in the HTTP/HTTPS protocol, X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-SrcPort have been added to the message header by default, which can correspondingly obtain the source address of the client, the application layer protocol between the client and the load balancer, and the client port. The TCP protocol cannot return a source address.
Obtain the client source address:
# Nginx example
log_format upstream '$time_iso8601 $http_x_forwarded_for $host $upstream_response_time $request $status $upstream_addr';
# Apache example
SetEnvIf REMOTE_ADDR "(.+)" CLIENTIP=$1
SetEnvIf X-Forwarded-For "^([0-9.]+)" CLIENTIP=$1
LogFormat "%{CLIENTIP}e %D %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" trueip_combined
CustomLog logs/access_log trueip_combined
# Modify the following parameters in the tomcat's server.xml file:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
prefix="tomcat_access."
suffix=".log"
pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i %T" resolveHosts="false"/>
</Host>