Skip to content

george124816/own-reverse-proxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

own-reverse-proxy

A simple reverse proxy system built in Go, designed to route HTTP requests to backend services based on path prefixes. Includes two sample backend servers (server_a and server_b) and a proxy service, all containerized for easy setup using Docker Compose.


Features

  • Reverse proxy: Routes traffic to backend services based on path.
  • Health checks: Each backend service exposes a /health endpoint, used by the proxy to determine availability.
  • Dockerized: All services provided with Dockerfiles and ready for multi-service orchestration using docker-compose.
  • Configurable: Proxy configuration via a simple JSON file.

Quick Start with Docker Compose

Prerequisites

Project Structure

own-reverse-proxy/
├── docker-compose.yml
├── reverse_proxy/
│   ├── Dockerfile
│   ├── config.json
│   └── main.go
├── server_a/
│   ├── Dockerfile
│   └── main.go
├── server_b/
│   ├── Dockerfile
│   └── main.go
└── ...

1. Clone the repository

git clone https://github.com/george124816/own-reverse-proxy.git
cd own-reverse-proxy

2. Configuration

The reverse proxy reads its configuration from reverse_proxy/config.json:

{
    "/joao": {
        "target": "joao",
        "port": 4300
    },
    "/george": {
        "target": "george",
        "port": 4200
    }
}

You can edit this file to add or change routes.


Example: Using the Proxy with an Nginx Service

You can use the reverse proxy to route traffic to any service, for example an Nginx container. Here’s a simple example:

1. Create a health file

Create a file named health in your project directory with the following content:

ok

This file will be served by Nginx as a static health check endpoint.

2. Example docker-compose.yml

services:
  crazy_service:
    image: nginx:latest
    volumes:
      - ./health:/usr/share/nginx/html/health:ro
  own_reverse_proxy:
    image: george124816/own-reverse-proxy:f754ab910f90a7bcb6bf0aac7fe090c2bbc73d43
    volumes:
      - ./config.json:/bin/config.json
  • crazy service: Runs Nginx and serves a static /health file with the content ok.
  • own reverse proxy: Runs the reverse proxy using a published image and your config file.

3. Update config.json

Make sure your config file points to the Nginx service and port (default for Nginx is 80):

{
    "/crazy": {
        "target": "crazy_service",
        "port": 80
    }
}

4. Start the services

docker compose up --build

5. Test the setup

  • Health check (proxied):

    curl http://localhost/crazy/health
    # Output: ok
  • Any other proxied endpoint (for example, serving Nginx root):

    curl http://localhost/crazy/
    # Output: nginx welcome page HTML

How It Works

  • The reverse proxy listens on port 80 and forwards requests to backend services according to the path.
  • Health checks are performed every second; if a backend is unhealthy, requests to its route will receive a 503 error.

Stopping the stack

docker compose down

Extending

To add more backend services:

  1. Create a folder (e.g., server_c) with a Dockerfile and Go service, or use a prebuilt image.
  2. Add it to docker-compose.yml.
  3. Update reverse_proxy/config.json to add a new route.

Troubleshooting

  • If you see Health Check failed or Service unavailable, check that the backend containers are running and healthy.
  • For advanced logs, check each container's output.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.


License

MIT (or your chosen license).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published