How to configure Varnish for WebSocket traffic

When I decided to use NodeBB as the forum application for this website, I wasn’t aware that Varnish and WebSocket don’t play nice by default, so Varnish has to be explicitly configured to handle WebSocket traffic. But it took several frustrating days before I finally figured out that Varnish was the source of the problems and errors I was dealing with.