nextjs api route request.url origin returns 0.0.0.0:3000 with domain assigned

Hello

I’m deploying nextjs to fly following this guide https://fly.io/docs/js/frameworks/nextjs/.

App deploys and renders well using custom domain but;

I have an api endpoint (GET) where I access the request.url from params, this should return the request url with domain e.g. https://mysite.com/path but I get https://0.0.0.0:3000/path. This doesn’t happen on vercel deployments

fly.toml

[build]

[http_service]
  internal_port = 3000
  force_https = true
  auto_stop_machines = true
  auto_start_machines = true
  min_machines_running = 0
  processes = ['app']

[[vm]]
  memory = '512mb'
  cpu_kind = 'shared'
  cpus = 1

Dockerfile


# syntax = docker/dockerfile:1

# Adjust NODE_VERSION as desired

ARG NODE_VERSION=20.11.1

FROM node:${NODE_VERSION}-slim as base

LABEL fly_launch_runtime="Next.js"

# Next.js app lives here

WORKDIR /app

# Set production environment

ENV NODE_ENV="production"

# Install pnpm

ARG PNPM_VERSION=9.4.0

RUN npm install -g pnpm@$PNPM_VERSION

# Throw-away build stage to reduce size of final image

FROM base as build

# Install packages needed to build node modules

RUN apt-get update -qq && \

apt-get install --no-install-recommends -y build-essential node-gyp pkg-config python-is-python3 ca-certificates

# Install node modules

COPY --link package.json pnpm-lock.yaml ./

RUN pnpm install --frozen-lockfile --prod=false

# Copy application code

COPY --link . .

# Build application

#docker secrets redacted

pnpm run build

# Remove development dependencies

RUN pnpm prune --prod

# Final stage for app image

FROM base

# Copy built application

COPY --from=build /app/.next/standalone /app

COPY --from=build /app/.next/static /app/.next/static

COPY --from=build /app/public /app/public

# Start the server by default, this can be overwritten at runtime

EXPOSE 3000

ENV HOSTNAME "0.0.0.0"

CMD [ "node", "server.js" ]

thanks in advance!

This is because Vercel is doing its own special sauce vs your standalone dockerized app. Same reason why you won’t have access to the fancy geo object. I believe you can find all the info in the request.headers object though.

Also, you should set ENV HOSTNAME :: so that your other fly apps can communicate w/ it.

1 Like

unfortunately headers doesn’t suffice my use-case :confused: I use supabase as BaaS their auth flow requires access to requestUrl.searchParams to get auth code, token and hash…

I remember this was working on other docker instances (e.g. railway/coolify) not sure why not on fly

Hmm are you sure it was working in other platforms? Because next standalone generates the server.js file which calls startServer({ hostname, ... }), where hostname is the HOSTNAME env, in this case it’s 0.0.0.0… so request.url will have 0.0.0.0 as the hostname.

1 Like

yeah I figured others are not standalone builds but built with nixpack

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.