0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HAProxyで代替画像を返す

Posted at

DIR

/haproxy-docker
├── /haproxy
│   ├── haproxy.cfg                # HAProxy 設定ファイル
│   └── /errors
│       └── placeholder.http       # 代替画像
├── /minio
│   └── data                       # MinIO データ格納ディレクトリ
├── /app
│   └── index.html                 # 簡易アプリケーション
└── docker-compose.yaml            # Docker Compose ファイル

docker-compose.yaml
version: '3.9'

services:
  haproxy:
    image: haproxy:2.8
    container_name: haproxy
    ports:
      - "80:80"
    volumes:
      - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
      - ./haproxy/errors:/etc/haproxy/errors:ro
    depends_on:
      - minio
      - app
    restart: always

  minio:
    image: minio/minio:latest
    container_name: minio
    command: server /data --console-address ":9001"
    ports:
      - "9000:9000"  # MinIO API
      - "9001:9001"  # MinIO Console
    environment:
      MINIO_ROOT_USER: admin
      MINIO_ROOT_PASSWORD: password123
    volumes:
      - ./minio/data:/data
    restart: always

  app:
    image: nginx:alpine
    container_name: app
    ports:
      - "8080:80"
    volumes:
      - ./app:/usr/share/nginx/html:ro
    restart: always

haproxy/haproxy.cfg
# Global settings
global
    log stdout format raw local0
    maxconn 4096
    daemon

defaults
    log global
    option httplog
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

# Define frontend
frontend http-in
    bind *:80
    acl is_image path_reg ^/img/.+/.+\.jpg$
    
    # 画像リクエストは MinIO へ
    use_backend minio_backend if is_image
    
    # それ以外はアプリへ
    default_backend app_backend

# Backend for MinIO
backend minio_backend
    server minio_server minio:9000 check
    option httpchk GET /health
    http-check expect status 200

    # minio から 200 以外ならエラーファイルを返す
    errorfile 404 /etc/haproxy/errors/placeholder.http
    errorfile 500 /etc/haproxy/errors/placeholder.http

# Backend for Application
backend app_backend
    server app_server app:80 check

haproxy/errors/placeholder.http
HTTP/1.1 200 OK
Content-Type: image/jpeg
Cache-Control: max-age=3600

次に、placeholder.jpg を placeholder.http に埋め込みます。

# placeholder.jpg をエラー用ファイルに埋め込む
cat /path/to/placeholder.jpg >> ./haproxy/errors/placeholder.http
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?