From c6f9c97bab2fc8c4873b4a46bab270e250498012 Mon Sep 17 00:00:00 2001 From: Robin Dahlberg Date: Tue, 31 Dec 2024 12:46:54 +0100 Subject: [PATCH] added remoteAddress. --- packages/next/src/server/lib/start-server.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/next/src/server/lib/start-server.ts b/packages/next/src/server/lib/start-server.ts index 224c873cdd4f4..20492063b733a 100644 --- a/packages/next/src/server/lib/start-server.ts +++ b/packages/next/src/server/lib/start-server.ts @@ -149,10 +149,19 @@ export async function startServer( async function requestListener(req: IncomingMessage, res: ServerResponse) { if (accessLog) { try { - fs.appendFileSync( - accessLog, - `${new Date().toISOString()} ${req.method} ${req.url} ${req.headers['user-agent']}\n` - ) + let remoteAddress + + if (req.headers['x-forwarded-for']) { + remoteAddress = req.headers['x-forwarded-for'].split(',')[0] + } else { + remoteAddress = req.socket.remoteAddress + } + res.on('finish', () => { + fs.appendFileSync( + accessLog, + `${remoteAddress} ${new Date().toISOString()} ${req.method} ${res.statusCode} ${req.url} ${req.headers['user-agent']}\n` + ) + }) } catch (err) { Log.error(`Failed to write to access log: ${accessLog}`) console.error(err)