Skip to content

Commit

Permalink
added remoteAddress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Dahlberg committed Dec 31, 2024
1 parent 3f6aabf commit c6f9c97
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/next/src/server/lib/start-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c6f9c97

Please sign in to comment.