How can I get the user's ip address
Hi guys how can I get the user's ip address and the url from which page the user came to the page?
14 Replies
You forward their real isp ip adress from the webserver like NGINX (you add the approriate headers in your reverse proxy) then you read them through the headers with nitro middleware(f.e), or if you dont have access to NGINX, you could altrnatively easily just do a API call once they land on the page, quite cheapskate solution the latter but it works
like: https://www.ipify.org/
ipify - A Simple Public IP Address API
ipify API is a simple public IP address API, easy enough to integrate into any application in seconds.
https://easyengine.io/tutorials/nginx/forwarding-visitors-real-ip/
to know where the user came from, u can use nitro (nuxt server) middleware to detect what path the user came from
f.e: if the user is coming from another page on site or coming from a external domain
if you dont have direct access to ur webserver that is hosting ur nuxt app or any web app, your best bet is too just do a useFetch call once u load ur page to grab their ip (after cookie consent)
here's an example api call i do in Nuxt to get some geo location based off user ip
note: you should try/catch both api calls though in my example
the middleware one is basic too
you could also expose the headers here
event.node.req.headers
holds the headers from ur incoming requests forwarded by your webserver
including the ones like real user (ISP) IP if u setup NGINX to forward it
in serverless situations this is problematic, but if u run vps's with f.e ubuntu where you manage ur environment urself, u can easily just add the two extra lines in your NGINX configurationThank you!!!
https://discord.com/channels/473401852243869706/1235192658859921419/1237079353704189963 I put in more examples in the server middleware for ya!
Can I do the same thing with an Apache server?
Globo.Tech
How to get X-Forwarded-For IP addresses in Apache Web Server - Glob...
This tutorial will show you how to get X-Forwarded-For IP addresses in Apache Web Server. Read now & Identify the originating client IP address
Yes
Hi , I copied this code into server/middleware/log.ts but I see nothing in the console...
Try stopping & restarting the server with npm/pnpm dev
It should appear
yes thx, now
I can see it in the terminal , but i want to send back the url to the backend so I can see which page the users came from the most, because of marketing
U can add (to, from) in the midddleware function
Nuxt
middleware/ · Nuxt Directory Structure
Nuxt provides middleware to run code before navigating to a particular route.