Can anyone help me. I want to know if there is any way to get the client IP in a middleware that I created.
I've tried:
env["REMOTE_ADDR"]
env["HTTP_X_FORWARDED_FOR"]
and none of these returns nothing.
I have already entered my middleware after them all:
in my application.rb
config.middleware.insert_after ActionDispatch :: Head, "MyMiddleware".
Please can anyone help me?
Thanks.
request.env
request.env
=> {"GATEWAY_INTERFACE"=>"CGI/1.1",
"PATH_INFO"=>"/events/1",
"QUERY_STRING"=>"",
"REMOTE_ADDR"=>"127.0.0.1",
"REMOTE_HOST"=>"127.0.0.1",
"REQUEST_METHOD"=>"GET",
"REQUEST_URI"=>"http://localhost:3000/events/1",
"SCRIPT_NAME"=>"",
"SERVER_NAME"=>"localhost",
"SERVER_PORT"=>"3000",
"SERVER_PROTOCOL"=>"HTTP/1.1",
"SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/2.2.0/2014-09-17)",
"HTTP_HOST"=>"localhost:3000",
"HTTP_USER_AGENT"=>"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:32.0) Gecko/20100101 Firefox/32.0",
"HTTP_ACCEPT"=>"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.5",
"HTTP_ACCEPT_ENCODING"=>"gzip, deflate",
"HTTP_DNT"=>"1",
"HTTP_COOKIE"=>"nameofapp-example_session=Qnh5ZMQVN5V2821f",
"HTTP_CONNECTION"=>"keep-alive",
"HTTP_CACHE_CONTROL"=>"max-age=0",
...
Each of these are keys you can use. In your case it would be one of these two
request.env["REMOTE_ADDR"]
request.env["REMOTE_HOST"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.