# 1. Редирект с HTTP на HTTPS (для всех доменов сразу)
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name domain.ru www.domain.ru git.domain.ru cloud.domain.ru m.domain.ru chat.domain.ru;

    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

# 2. Основной сайт и WWW
server {
    listen 443 ssl;
    server_name domain.ru www.domain.ru;

    # Используем один сертификат, если он мультидоменный (Certbot обычно делает один на домен + www)
    # Если сертификаты разные, лучше объединить их через Certbot (флаг -d домен1 -d домен2)
    ssl_certificate /etc/letsencrypt/live/domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.ru/privkey.pem;


    location / {
        proxy_pass http://homepage:4321;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

# 3. Gitea
server {
    listen 443 ssl;
    server_name git.domain.ru;

    ssl_certificate /etc/letsencrypt/live/git.domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/git.domain.ru/privkey.pem;

    # Меняем домашнюю страницу на страницу админа
    location = / {
        return 301 /username;
    }

    location / {
        proxy_pass http://gitea:3000;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

# 4. Cloud
server {
    listen 443 ssl;
    server_name cloud.domain.ru;

    ssl_certificate /etc/letsencrypt/live/cloud.domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/cloud.domain.ru/privkey.pem;

    client_max_body_size 0;

    location / {
        proxy_pass http://filebrowser:8080;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

# 4. Matrix
server {
    listen 443 ssl;
    server_name m.domain.ru;

    ssl_certificate /etc/letsencrypt/live/m.domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/m.domain.ru/privkey.pem;

    location / {
        proxy_pass http://matrix-synapse:8008;
        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_set_header X-Forwarded-Proto $scheme;

        client_max_body_size 50M;
    }
}
server {
    listen 443 ssl;
    server_name chat.domain.ru;

    ssl_certificate /etc/letsencrypt/live/chat.domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/chat.domain.ru/privkey.pem;

    location / {
        proxy_pass http://matrix-element:8083;
        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_set_header X-Forwarded-Proto $scheme;

        client_max_body_size 50M;
    }
}

# Memos
server {
    listen 443 ssl;
    server_name todo.domain.ru;

    ssl_certificate /etc/letsencrypt/live/todo.domain.ru/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/todo.domain.ru/privkey.pem;

    location / {
        proxy_pass http://memos:5230;
        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_set_header X-Forwarded-Proto $scheme;

        client_max_body_size 50M;
    }
}
