Latest
Docker 1 min read

Put Nginx in Front of a Node.js Container

Put Nginx in Front of a Node.js Container
Docker · September 4, 2026

Docker lab/version note: use supported Docker and Node.js tools and patched images. The JavaScript examples have local tests; Docker, Nginx and hosted CI steps were not tested live.

What this guide covers

Nginx can serve a web page and forward its API requests to a Node.js container. The browser uses one address, while the application container stays on a private Docker network.

Before you start

Use the files from the Node.js development guide. Docker and the Compose plugin must be installed. This is a local HTTP example; add HTTPS before exposing it to the internet.

Step 1: Create the application Dockerfile

Save this as Dockerfile in the same project root:

FROM node:24-bookworm-slim
ENV NODE_ENV=production PORT=3210
WORKDIR /app
COPY --chown=node:node src ./src
USER node
EXPOSE 3210
CMD ["node", "src/server.mjs"]

Unlike the development setup, this image contains a fixed copy of the source.

Step 2: Create a web page

Create public/index.html:

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Web application example</title>
<h1>Message from the API</h1>
<p id="message">Loading…</p>
<script type="module">
const output = document.querySelector('#message');
try {
  const response = await fetch('/api/message');
  if (!response.ok) throw new Error(`API returned ${response.status}`);
  const data = await response.json();
  output.textContent = data.message;
} catch {
  output.textContent = 'The API is not available.';
}
</script>
</html>

The page requests /api/message on its own origin. It never tries to contact a container name from the browser.

Step 3: Configure Nginx

Create edge/default.conf:

server {
    listen 80;
    server_name _;
    root /usr/share/nginx/html;
    location /api/ {
        proxy_pass http://web-app:3210;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_connect_timeout 3s;
        proxy_read_timeout 15s;
    }
    location / {
        try_files $uri $uri/ =404;
    }
}

The /api/ path goes to the application container. The lack of a trailing slash on proxy_pass preserves the request path. Other paths are served from the static directory.

Create edge/Dockerfile:

FROM nginx:1.28-alpine
COPY edge/default.conf /etc/nginx/conf.d/default.conf
COPY public /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

Step 4: Start both services

Save this as compose.release.yaml:

services:
  web-app:
    build:
      context: .
      dockerfile: Dockerfile
    image: example-web:local
    healthcheck:
      test: ['CMD', 'node', '-e', "fetch('http://127.0.0.1:3210/healthz').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
      interval: 5s
      timeout: 3s
      retries: 10
    networks: [app-network]
  proxy:
    build:
      context: .
      dockerfile: edge/Dockerfile
    image: example-web-edge:local
    ports: ['127.0.0.1:8086:80']
    depends_on:
      web-app:
        condition: service_healthy
    networks: [app-network]
networks:
  app-network:

Then run:

docker compose -f compose.release.yaml config --quiet
docker compose -f compose.release.yaml up -d --build
docker compose -f compose.release.yaml exec -T proxy nginx -t
curl --fail http://127.0.0.1:8086/api/message
curl --fail http://127.0.0.1:8086/
docker compose -f compose.release.yaml logs --tail 60 proxy

Open http://127.0.0.1:8086/. Only the proxy publishes a host port. Docker’s internal DNS lets Nginx find web-app.

Step 5: Check an application failure

Stop the backend with docker compose -f compose.release.yaml stop web-app, then reload the page. It should show an API error, not a false success. Start the service again and confirm recovery. Finish with docker compose -f compose.release.yaml down.

Check the result

Check both the page and /api/message through port 8086. Test an unknown API path too; it should not return the HTML page as a successful API response.

References

PB
Written by

Parul Bedi

Notes and guides by Parul Bedi.

Up next

Show SonarQube Results on a Pull Request

Sonar September 6, 2026 1 min read
Open post

Conversation

0 comments

No comments yet. Start the conversation below.

Leave a comment

Replying to a comment.
Be kind. Markdown is not supported in comments.