# Nginx — proxy the site to the Node server.
#
#   sudo cp deploy/nginx.conf /etc/nginx/sites-available/chordly
#   sudo ln -s /etc/nginx/sites-available/chordly /etc/nginx/sites-enabled/chordly
#   sudo nginx -t && sudo systemctl reload nginx
#
# Point the DNS A record for cloudpal.live at this server, then run
# `sudo certbot --nginx -d cloudpal.live -d www.cloudpal.live` for HTTPS.
#
# Important: proxy_buffering off keeps downloads streaming instead of being
# collected by Nginx before they are sent to the browser.

server {
    listen 80;
    listen [::]:80;
    server_name cloudpal.live www.cloudpal.live;

    # The app only accepts small JSON bodies (links and titles).
    client_max_body_size 2m;

    # AdSense and the YouTube fetches are done by the server, not the browser.
    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 Upgrade           $http_upgrade;
    proxy_set_header Connection        "upgrade";

    location / {
        proxy_pass         http://127.0.0.1:3000;
        proxy_http_version 1.1;

        # Stream downloads straight through.
        proxy_buffering    off;
        proxy_request_buffering off;

        # Large videos can take a while.
        proxy_read_timeout  600s;
        proxy_send_timeout  600s;
    }

    # HTTPS: uncomment after your certificate is in place.
    #
    # listen 443 ssl http2;
    # server_name cloudpal.live www.cloudpal.live;
    # ssl_certificate     /etc/letsencrypt/live/cloudpal.live/fullchain.pem;
    # ssl_certificate_key /etc/letsencrypt/live/cloudpal.live/privkey.pem;
    # return 301 https://$host$request_uri;
}
