-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IP address not obtainable in Next 13 with app dir? #47793
Comments
On https://nextjs.org/docs/api-reference/next/server#nextrequest
|
it doesnt work. my server send to headers to request X-Real-IP and X-Forwarded-For but ip underfined... |
We're having this issue too. Is there a way to get the request IP when using app router? |
@jmif Did you find a workaround or a way of doing this? |
Still relevant problem |
what's the point of having fucking api routes if u can't get an ip? |
You can still access the ip address in api routes (and middleware) of the page router, according to the doc. The issue here is that it's not available for the app router. |
Cool! Rewriting the whole app on the old version is exactly what I want, ahahha. I'm quite pissed off frankly speaking |
Yes, api routes are the remaining bits that I've yet to move to route handlers in the app router because of this. It would be great if As for workaround, I'm currently using middleware to add geo info to searchParams: import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { app } from "./lib/app-config";
export const config = {
matcher: ["/"],
};
export function middleware(request: NextRequest) {
const { nextUrl: url, geo } = request;
const city = geo?.city ?? app.location.city;
const country = geo?.country ?? app.location.country;
url.searchParams.set("city", city);
url.searchParams.set("country", country);
return NextResponse.rewrite(url);
} Then, in my export default function Page({ searchParams }: SearchParams) {
const city = searchParams.city;
...
} |
You can get the ip related headers in your server component by using headers() from "next/headers", convert it to object literal and pass it to your fetch request which is calling your api route. I'd suggest making an api client wrapper to do this automatically.
|
Won't I be able to forge headers in a request like this? Pass not real ip for instance |
This approach only applies to server components since |
In some cases, "X-Real-IP" and "X-Forwarded-For" returns undefined, i think NextJS team should implement this on NextRequest, i don't understand what's their intention or plan on NextRequest.ip variable if we can't get the IP, kinda stupid enough |
If I'm not mistaken, there has to be some configuration on the server e.g. nginx to get an ip |
Yes, if you were using nginx, if not? |
Testing out the app router for the first time today and ran into this issue as I need to make external requests in my components during SSR. I have a backend written in another language and previously used getServerSideProps to call fetch, passing the user's IP, cookies, etc. so that the backend would see the request as coming from the end user and not localhost (nextjs). The whole caching and data fetching story with the app router is a bit complicated, but I think I figured out how to do this. Using "next/headers" as mentioned above we can get the headers (similar to appContext with the pages router) and pass them to fetch, the problem occurs when this is used in a client component. (The file that imports headers to create the fetch request, among other custom fetch additions, is used for both client and server requests) You will get an error that you cannot use headers in client components. To work around this, we need to dynamically import the headers module. Change
Then pass these headers to fetch to forward them to your backend. This is all behind nginx so x-forwarded-for is trusted. |
is it fixed ? |
No solution so far?? |
waiting for this |
It seems this is related PR |
@OlegLustenko I tried 'v13.4.20-canary.31' and request.ip is still undefined and request.geo is empty object. |
Hello, i have the same problem also. |
That is not what we are discussing here. Yes you can get ip from 3rd party. When client side sends request, server side doesn't get ip. Try create react and express and send request to express using axios. You gonna get ip even in local host. In nextjs, there is no way to get ip now unless your web server like nginx has configured or send another request to 3rd party to get ip. |
The wierd thing is that the past few days i was using this aproach on a page.tsx
and it was working just fine until today where "x-forwarded-for" is not included anymore on the headers array |
This comment has been minimized.
This comment has been minimized.
My dudes, this needs to be fixed. It's a fundamental issue. |
This comment has been minimized.
This comment has been minimized.
The |
I think this may have been fixed with #56797? |
I think we still need a way to securely get the ip of the proxy (not the visitors ip) if a reverse proxy is used in front of nextjs, correct me if I'm wrong. |
@leerob I haven't found any update regarding It seems the edge helper function also gets the ip from the headers: https://vercel.com/docs/functions/edge-middleware/middleware-api#ipaddress
Imho this issue shouldn't be closed until there is a solution for this, as it is a security issue. |
Based on feedback from #47793, I made some improvements around the geolocation docs. Specifically around `request.ip`, `request.geo`, and how to access these values. I noticed there was a bit of a divergence, as some of the `NextRequest` and `NextResponse` docs were split out for the App Router section, but not all. This PR finishes that swing by removing the previous catch-all for `next/server` in the Pages Router docs and splits them into individual docs pages. Wrote a lil' thread about this: https://twitter.com/leeerob/status/1736543599339172121 --------- Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com>
@Zerebokep this issue is around getting the IP address with the App Router. This is possible and now more clearly documented. You are correct that |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.4.0: Mon Mar 6 21:00:41 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8103 Binaries: Node: 16.15.0 npm: 8.10.0 Yarn: 1.22.15 pnpm: 6.11.0 Relevant packages: next: 13.2.4 eslint-config-next: 13.0.4 react: 18.2.0 react-dom: 18.2.0
Describe the Bug
It seems to be undoable to get the users IP address when using Next 13 with the app dir.
req.ip
orreq.geo
property.req.ip
isundefined
andreq.geo
is{}
.How is one supposed to get the ip address on the server side in Next 13?
The text was updated successfully, but these errors were encountered: