Skip to content

OAuth2 Proxy (htpasswd)

OAuth2 Proxy คือ reverse proxy ที่ทำ authentication ให้กับ backend services โดยรองรับหลาย OAuth providers (Google, GitHub, etc.) และ htpasswd authentication สำหรับ quick setup แบบไม่ต้องมี OAuth provider

services:
oauth2-proxy:
image: quay.io/oauth2-proxy/oauth2-proxy:latest
container_name: oauth2-proxy
restart: unless-stopped
environment:
TZ: Asia/Bangkok
OAUTH2_PROXY_CLIENT_ID: dummy
OAUTH2_PROXY_CLIENT_SECRET: dummy
OAUTH2_PROXY_REDIRECT_URL: https://your-domain.com/oauth2/callback
OAUTH2_PROXY_HTPASSWD_FILE: /etc/oauth2-proxy/htpasswd
OAUTH2_PROXY_COOKIE_SECRET: 12345678901234567890123456789012
OAUTH2_PROXY_COOKIE_SECURE: "true"
OAUTH2_PROXY_UPSTREAMS: http://backend-service:8080
OAUTH2_PROXY_HTTP_ADDRESS: "0.0.0.0:4180"
OAUTH2_PROXY_SKIP_AUTH_ROUTES: "^/api(/.*)?$"
OAUTH2_PROXY_DISPLAY_HTPASSWD_FORM: "true"
OAUTH2_PROXY_EMAIL_DOMAINS: "*"
volumes:
- ./proxy/htpasswd:/etc/oauth2-proxy/htpasswd:ro
- ./proxy/logs:/var/log/oauth2-proxy
ports:
- "4180:4180"
networks:
- app-network
backend-service:
image: your-backend-image
container_name: backend-service
networks:
- app-network
networks:
app-network:
driver: bridge

ใช้ dummy สำหรับ CLIENT_ID และ CLIENT_SECRET เมื่อใช้ htpasswd auth เท่านั้น

Terminal window
# ติดตั้ง apache2-utils ก่อน
# macOS
brew install httpd
# Ubuntu/Debian
apt install apache2-utils
# Alpine
apk add apache2-utils
Terminal window
# สร้าง file ใหม่ (สร้าง user)
htpasswd -c ./proxy/htpasswd admin
# พิมพ์ password ที่ต้องการ 2 ครั้ง
# เพิ่ม user เพิ่มเติม (ไม่ใช้ -c)
htpasswd ./proxy/htpasswd another-user
# ดู users ใน file
cat ./proxy/htpasswd
admin:$apr1$L5/8HJ..$k8kTfxCj4E8K5T5X5xXyX/
bob:$apr1$Hz/8HJ..$m8kTfxCj4E8K5T5X5xXyX/
VariableDescriptionRequired
OAUTH2_PROXY_UPSTREAMSBackend service URL✅
OAUTH2_PROXY_HTTP_ADDRESSListen address✅
OAUTH2_PROXY_HTPASSWD_FILEPath to htpasswd file✅
OAUTH2_PROXY_COOKIE_SECRETSecret for cookie encryption (32 chars)✅
VariableDescriptionDefault
OAUTH2_PROXY_CLIENT_IDOAuth client ID (ใช้ dummy กับ htpasswd)-
OAUTH2_PROXY_CLIENT_SECRETOAuth client secret (ใช้ dummy กับ htpasswd)-
OAUTH2_PROXY_REDIRECT_URLOAuth callback URL-
OAUTH2_PROXY_EMAIL_DOMAINSAllowed email domains (* for all)*
OAUTH2_PROXY_DISPLAY_HTPASSWD_FORMแสดง htpasswd login formfalse
OAUTH2_PROXY_COOKIE_SECURESet Secure flag on cookiestrue if HTTPS
VariableDescriptionExample
OAUTH2_PROXY_SKIP_AUTH_ROUTESRoutes to skip auth (regex)^/api(/.*)?$
OAUTH2_PROXY_SKIP_PROVIDER_CHECKSkip OAuth provider checktrue
VariableDescriptionValues
OAUTH2_PROXY_LOG_LEVELLog verbositydebug, info, warn, error
OAUTH2_PROXY_LOG_JSONJSON format loggingtrue or false
Avatar blackCat
OAUTH2_PROXY_SKIP_AUTH_ROUTES ใช้ regex สำหรับ public API routes ที่ต้องการเข้าถึงได้เลย
docker-compose.yml
services:
oauth2-proxy:
image: quay.io/oauth2-proxy/oauth2-proxy:latest
container_name: oauth2-proxy
environment:
OAUTH2_PROXY_UPSTREAMS: http://backend-service:8080
OAUTH2_PROXY_HTPASSWD_FILE: /etc/oauth2-proxy/htpasswd
OAUTH2_PROXY_COOKIE_SECRET: 12345678901234567890123456789012
OAUTH2_PROXY_HTTP_ADDRESS: "0.0.0.0:4180"
OAUTH2_PROXY_DISPLAY_HTPASSWD_FORM: "true"
volumes:
- ./proxy/htpasswd:/etc/oauth2-proxy/htpasswd:ro
networks:
- app-network
- traefik-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.oauth2-proxy.rule=Host(`grist.example.com`)"
- "traefik.http.routers.oauth2-proxy.entrypoints=websecure"
- "traefik.http.routers.oauth2-proxy.tls=true"
- "traefik.http.services.oauth2-proxy.loadbalancer.server.port=4180"
- "traefik.http.routers.oauth2-proxy.middlewares=oauth2-proxy-auth"
- "traefik.http.middlewares.oauth2-proxy-auth.forwardauth.address=http://oauth2-proxy:4180"
- "traefik.http.middlewares.oauth2-proxy-auth.forwardauth.trustForwardHeader=true"
backend-service:
image: your-backend-image
container_name: backend-service
networks:
- app-network
labels:
- "traefik.enable=true"
- "traefik.http.routers.backend.rule=Host(`grist.example.com`)"
- "traefik.http.routers.backend.entrypoints=websecure"
- "traefik.http.routers.backend.tls=true"
- "traefik.http.routers.backend.middlewares=oauth2-proxy-auth"
networks:
app-network:
driver: bridge
traefik-network:
external: true

การตรวจสอบสถานะ

Section titled “การตรวจสอบสถานะ”
Terminal window
# ดู logs
docker logs oauth2-proxy
# ดู logs แบบ real-time
docker logs -f oauth2-proxy
# ตรวจสอบว่า container ทำงานอยู่
docker ps | grep oauth2-proxy

ปัญหาที่พบบ่อย

Section titled “ปัญหาที่พบบ่อย”
upstream timed out
  • ตรวจสอบว่า backend service ทำงานอยู่และ port ถูกต้อง
  • ดูว่า network ถูกตั้งค่าถูกต้อง
Invalid account
  • ตรวจสอบ htpasswd file ว่าถูก mount ถูกต้อง
  • ตรวจสอบ path ใน OAUTH2_PROXY_HTPASSWD_FILE ว่าตรงกับ volume mount
cookie is not valid
  • ลองลบ cookies ของเว็บ
  • ตรวจสอบว่า OAUTH2_PROXY_COOKIE_SECRET มีความยาว 32 ตัวอักษร
Terminal window
# สุ่ม 32 ตัวอักษร
openssl rand -base64 32 | tr -d '=' | head -c 32
# หรือใช้ Python
python3 -c "import secrets; print(secrets.token_hex(16))"