# 110 Ollama GCP Proxy — ADR-110 三層容災
# 讓 K3s 叢集內可透過內網 110 存取 GCP 外網 Ollama
# 建立時間: 2026-05-04
# 部署: ansible-playbook -i inventory/hosts.yml playbooks/nginx-sync.yml --tags 110

# ============================================================
# Ollama GCP-A Primary (port 11435 → 34.143.170.20:11434)
# ============================================================
server {
    listen 11435;
    listen [::]:11435;
    server_name _;
    
    access_log /var/log/nginx/ollama-gcp-a-access.log;
    error_log /var/log/nginx/ollama-gcp-a-error.log warn;
    
    location / {
        proxy_pass http://34.143.170.20: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;
        
        # Ollama 推理可能較慢，給較長超時
        proxy_connect_timeout 10s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        
        # 支援 streaming response
        proxy_buffering off;
        proxy_cache off;
    }
    
    # 健康檢查端點
    location /nginx-health {
        access_log off;
        return 200 "Ollama GCP-A Proxy OK\n";
        add_header Content-Type text/plain;
    }
}

# ============================================================
# Ollama GCP-B Secondary (port 11436 → 34.21.145.224:11434)
# ============================================================
server {
    listen 11436;
    listen [::]:11436;
    server_name _;
    
    access_log /var/log/nginx/ollama-gcp-b-access.log;
    error_log /var/log/nginx/ollama-gcp-b-error.log warn;
    
    location / {
        proxy_pass http://34.21.145.224: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_connect_timeout 10s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;
        
        proxy_buffering off;
        proxy_cache off;
    }
    
    location /nginx-health {
        access_log off;
        return 200 "Ollama GCP-B Proxy OK\n";
        add_header Content-Type text/plain;
    }
}

# ============================================================
# Ollama Local Fallback (port 11437 → 192.168.0.111:11434)
# ADR-110 第三層：GCP-A 與 GCP-B 均不可用時的本機備援
# ============================================================
server {
    listen 11437;
    listen [::]:11437;
    server_name _;

    access_log /var/log/nginx/ollama-local-access.log;
    error_log /var/log/nginx/ollama-local-error.log warn;

    location / {
        proxy_pass http://192.168.0.111: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_connect_timeout 5s;
        proxy_send_timeout 300s;
        proxy_read_timeout 300s;

        proxy_buffering off;
        proxy_cache off;
    }

    location /nginx-health {
        access_log off;
        return 200 "Ollama Local Fallback Proxy OK\n";
        add_header Content-Type text/plain;
    }
}
