Configuring Varnish Cache

To be sure Varnish will work properly with NodeBB check that your configuration /etc/varnish/default.vcl is optimized for websockets.

  1. backend nodebb {
  2. .host = "127.0.0.1"; # your nodebb host
  3. .port = "4567"; # your nodebb port
  4. }
  5. sub vcl_recv {
  6. # Pipe websocket connections directly to Node.js
  7. if (req.http.Upgrade ~ "(?i)websocket") {
  8. set req.backend = nodebb;
  9. return (pipe);
  10. }
  11. # NodeBB
  12. if (req.http.host == "forum.yourwebsite.com") { # change this to match your host
  13. if (req.url ~ "^/socket.io/") {
  14. set req.backend = nodebb;
  15. return (pipe); # return pass seems not working for websockets
  16. }
  17. return (pass); # don't cache
  18. }
  19. }
  20. sub vcl_pipe {
  21. # Need to copy the upgrade header
  22. if (req.http.upgrade) {
  23. set bereq.http.upgrade = req.http.upgrade;
  24. }
  25. }