# Example nginx site for the Ludo game, with the game server running on this machine at 127.0.0.1:8080.
# It serves the page and the WebSocket on the same domain, so config.js can keep window.LUDO_SERVER = ''.
# Adjust server_name, the certificate paths and (if the game lives in a sub-folder) the location paths.

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 443 ssl http2;
    server_name gametest2.atmegame.com;

    ssl_certificate     /etc/letsencrypt/live/gametest2.atmegame.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/gametest2.atmegame.com/privkey.pem;

    # The game server's WebSocket. These lines are what make the "101 Switching Protocols" upgrade work; without
    # proxy_http_version 1.1 and the Upgrade / Connection headers nginx answers /ws like an ordinary web page.
    location /ws {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   # used with TRUST_PROXY=1 (per-address limit)
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_read_timeout 3600s;    # the game sends a ping every 20 s; this only has to be longer than that
        proxy_send_timeout 3600s;
    }

    # The page (only if the game server also serves it; skip this block when the static files are served by nginx itself).
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

# Only the proxy should reach the game server: start it with HOST=127.0.0.1 (see deploy/ecosystem.config.cjs), do not open
# port 8080 in the firewall.
#
# If the page is served by nginx from a folder (e.g. /ludo/v3/) and only /ws goes to the game server, drop the "location /"
# block above, add your usual "root ..." for the page, and keep config.js as it is.
