Skip to content

Fix: Allow users to specify a bind address for self-hosted Next.js applications #77612

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

Open
wants to merge 3 commits into
base: canary
Choose a base branch
from

Conversation

dmyerscough
Copy link

What?

This change adds the ability for users to specify a bind address for self-hosted Next.js apps. By default, if a hostname is set, Next.js will bind to the hostname's loopback address. This becomes problematic within a Kubernetes environment.

Why?

When self-hosting a Next.js application on Kubernetes, it will automatically listen on the pod's private IP address. This causes an issue with Kubernetes port-forwarding, it's not possible to forward requests to the Next.js container. For example, attempting to port-forward to a container will result in the following error:

kubectl port-forward onboarding-frontend-5bb695b7b6-25ngt 3000:3000
Forwarding from 0.0.0.0:3000 -> 3000
Handling connection for 3000
E0329 17:25:23.974546   55527 portforward.go:424] "Unhandled Error" err="an error occurred forwarding 3000 -> 3000: error forwarding port 3000 to pod 2efe69d045562d5f
a66ad884406f6561440ec4ff1529da4c44dcdb100783efba, uid : failed to execute portforward in network namespace \"/var/run/netns/cni-2b73967b-b555-35f4-b5bd-f2e8d7145e9d\"
: failed to connect to localhost:3000 inside namespace \"2efe69d045562d5fa66ad884406f6561440ec4ff1529da4c44dcdb100783efba\", IPv4: dial tcp4 127.0.0.1:3000: connect:
connection refused IPv6 dial tcp6 [::1]:3000: connect: connection refused "

A workaround for this is to either set the HOSTNAME to an empty string in the Dockerfile or in the Kubernetes deployment.

env:
  - name: HOSTNAME
    value: ""

How?

Allowing the user to specify the bind address provides a cleaner approach to this problem. The pod retains its hostname, and the user can configure Next.js to listen on all interfaces within a Kubernetes environment. This is useful when a user wants to troubleshoot an issue without having to setup an ingress controller.

@ijjk
Copy link
Member

ijjk commented Mar 30, 2025

Allow CI Workflow Run

  • approve CI run for commit: 04f40b7

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

@dmyerscough dmyerscough changed the title Fix: Allow users to override the hostname and specify a custom address to bind to Fix: Allow users to specify a bind address for self-hosted Next.js applications Mar 30, 2025
@dmyerscough dmyerscough force-pushed the feature/allow-binding-interface branch from 9785696 to 5c4a982 Compare March 30, 2025 02:18
@dmyerscough
Copy link
Author

@ijjk is there anything required to get the CI to pass? I noticed it doesn't pass validation due to the old approve CI run for commit message.

@@ -1713,7 +1713,7 @@ process.env.NODE_ENV = 'production'
process.chdir(__dirname)

const currentPort = parseInt(process.env.PORT, 10) || 3000
const hostname = process.env.HOSTNAME || '0.0.0.0'
const hostname = process.env.BIND_ADDRESS || process.env.HOSTNAME || '0.0.0.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants