# HTTP to HTTPS Redirection
server {
    listen 80;
    server_name mail.geumdo.net;
    
    # Redirect all HTTP requests to HTTPS
    return 301 https://$host$request_uri;
}

# Main HTTPS Server Block
server {
    listen 443 ssl;
    server_name mail.geumdo.net;

    ssl_certificate /etc/letsencrypt/live/mail.geumdo.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mail.geumdo.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {
        proxy_pass https://mail.geumdo.net:4433;
        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;
        proxy_set_header X-Forwarded-Port $server_port;

        proxy_connect_timeout 20s;
        proxy_send_timeout 20s;
        proxy_read_timeout 20s;

        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;

        proxy_request_buffering off;
        proxy_buffering off;
        proxy_redirect off;
    }
}

