Wednesday, September 17, 2025

NGINX reverse proxy (one domain support)

 Adding an NGINX reverse proxy so your users just hit one domain/port (e.g. http://localhost) and NGINX will serve:

  • Frontend at /

  • Backend API at /api/

  • Ollama at /ollama/ (optional, in case you want to expose it too)


๐Ÿ”น Step 1: Create nginx.conf (reverse proxy)

Make a new folder nginx/ in your project root and add nginx.conf:

server {
  listen 80;

  server_name _;

  # Serve frontend Angular app
  location / {
    proxy_pass http://frontend:80;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

  # Proxy backend FastAPI
  location /api/ {
    proxy_pass http://backend:8000/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

  # Proxy Ollama (optional)
  location /ollama/ {
    proxy_pass http://ollama:11434/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }
}

๐Ÿ”น Step 2: Add Reverse Proxy Service to docker-compose.yml

Extend your compose file:

version: "3.9"

services:
  backend:
    build: .
    container_name: company-recommender-backend
    expose:
      - "8000"
    environment:
      - OLLAMA_URL=http://ollama:11434
    depends_on:
      - ollama

  ollama:
    image: ollama/ollama:latest
    container_name: ollama
    expose:
      - "11434"
    volumes:
      - ollama_data:/root/.ollama

  frontend:
    build: ./frontend
    container_name: company-recommender-frontend
    expose:
      - "80"
    environment:
      - API_URL=/api   # frontend calls backend via proxy
    depends_on:
      - backend

  proxy:
    image: nginx:alpine
    container_name: reverse-proxy
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - frontend
      - backend
      - ollama

volumes:
  ollama_data:

๐Ÿ”น Step 3: Adjust Angular Runtime Config

Since the reverse proxy maps backend → /api/, update frontend/src/assets/env.js:

(function(window) {
  window["env"] = window["env"] || {};
  window["env"]["API_URL"] = "${API_URL}";
})(this);

In docker-compose.yml, the frontend service already sets:

environment:
  - API_URL=/api

So the Angular app will now call /api/..., which gets routed to the backend by NGINX.


๐Ÿ”น Step 4: Rebuild & Run

From your project root:

docker-compose up --build

✅ Final Setup

  • Visit → http://localhost → Angular frontend served.

  • Frontend API calls → /api/... → routed to backend.

  • Optional Ollama calls → /ollama/....

Everything is cleanly under one domain and one port (80) ๐ŸŽฏ.


๐Ÿ‘‰ Do you want me to also add HTTPS (SSL/TLS) with Let’s Encrypt support to this proxy so you can put it on a real server/domain securely?

No comments:

Post a Comment

Document for Docker + nginx + HTTPS setup for Company Recommender

Docker + nginx + HTTPS setup for Company Recommender This document contains a ready-to-use Docker deployment for your FastAPI backend and An...