I'm writing a plug which put the access log into a file, like mediocre web server. As the data source of client's IP address, conn
has remote_ip
and peer
. Which should I use, and what's the difference between them?
Is there any documents which describe about the each entities in conn
?
Also, my plug is like the following snippets, is it natural from Elixir/Phoenix point of view?
Logger.info(
Enum.join([
"type:" <> "request",
"remoteip:" <> Enum.join(Tuple.to_list(conn.remote_ip), ","),
"method:" <> conn.method,
"path:" <> conn.request_path,
"status:" <> to_string(conn.status),
"size_res:" <> to_string(byte_size(to_string(conn.resp_body))),
], ",")
)
From the Plug.Conn docs:
peer - the actual TCP peer that connected, example: {{127, 0, 0, 1}, 12345}. Often this is not the actual IP and port of the client, but rather of a load-balancer or request-router.
remote_ip - the IP of the client, example: {151, 236, 219, 228}. This field is meant to be overwritten by plugs that understand e.g. the X-Forwarded-For header or HAProxy’s PROXY protocol. It defaults to peer’s IP.
If you are using a load balancer (such as http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/x-forwarded-headers.html) then the peer
ip address will refer to the load balancer and not the client. Load balancers provide the actual IP address of the client in a header (X-Forwarded-For
for AWS) that will be stored in the Plug.Conn struct as remote_ip
.
See also https://www.rfc-editor.org/rfc/rfc7239
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With