Replies: 1 comment
-
In this particular situation, I don't think there are "correct" or "incorrect" approaches. This is just an example of passing headers from SSR to the external client without implementing a complicated API layer in your app.
I've never worked with CloudFlare, so I cannot tell for sure. But I would definitely start with checking request input/headers on the Nuxt side first. Usually, if you have this information in the first request coming from the client to Nuxt app via both CF and LB, it should contain some information about the IP. If so, then you can add this header to be passed to your Laravel API, even if it goes via CF/LB again. Afaik, in your case, it would be either X-Forwarded-For or CF-Connecting-IP |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
well, it's me again, asking stupid questions about the routeRules-proxy combined with nuxt-auth-sanctum 🙈
Abstract
I am using sanctum in my nuxt project using routeRules-proxy to access my Laravel backend.
That nuxt project is behind Cloudflare Proxy and I am trying to get the user's IP on the backend, which currently does not work.
The current setup
User Request -> Cloudflare Proxy -> Load Balancer -> internal Network
Inside the internal network I got two virtual machines that are hosting:
While Nuxt is accessible via Cloudflare, the Laravel-API is only reachable from inside the private network.
So if a user hits example.com, the request chain is:
Cloudflare Proxy -> Load Balancer -> webserver1
Now to make things a little more complicated, I need to use nitro proxy:
and the sanctum base URL is set to that backend:
This works.
The problem
But there is one little problem: The client IP gets lost on the Laravel api.
So if I log
request()->ip()
, I get10.0.1.1
, which is the internal IP of the load balancer.If I refresh the page, the following log appears on the laravel's webserver:
If I now navigate through the page without refreshing, I am getting these logs:
As you can see, my IP is now stored in the header:
So what I learn from that: The server-side fetching that happens on first page load does not forward the ip of the user. Technically, the webserver1 performs a request, which goes through cloudflare proxy again and so the IP of the nat gateway is stored in the header:
I cannot work with this IP when it comes to rate-limiting on the Laravel-side, so the question is, whether I can force the proxy to send the requesting user's IP on initial page load.
First approach to this issue
As @manchenkoff pointed out that there is a possibility to Forward context headers, I might try this.
But before going down that way, I need your help, as I am not that experienced with nitro and h3: Is this the correct approach? Is it possible to proxy those initial requests including the user's ip this way?
Beta Was this translation helpful? Give feedback.
All reactions