Traefik 处理静态文件代理

12 min read

Traefik 配置文件

version: '3.6'

services:
  nginx-server:
    image: ${DOCKER_IMAGE}
    container_name: ${SERVICE_NAME}
    environment:
      - USER_UID=1000
      - USER_GID=1000
    networks:
      - proxy
    restart: unless-stopped
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=proxy"
      - "traefik.http.routers.static.entrypoints=websecure"
      - "traefik.http.routers.static.rule=Host(`static.test.xyz`)"
      - "traefik.http.services.static.loadbalancer.server.port=8000"
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /etc/timezone:/etc/timezone:ro
      - ./conf:/etc/nginx/conf.d
      - ./data:/data
    logging:
      driver: "json-file"
      options:
        max-size: "10m"

networks:
  proxy:
    external: true

Nginx 配置文件

server {
  listen 8000;
  location / {
    root    /data;
    index   index.html;
    try_files $uri $uri/ @rewrites;
  }

  location @rewrites {
         rewrite ^(.+)$ /index.html last;
  }
}

目录结构

├── conf
│   └── static.conf
├── data
│   ├── 1.txt
│   └── index.html
└── docker-compose.yml

参见 Traefik配置