Why Traccar's WebSocket needs a proxy, and how to set one up with nginx
You have Traccar running, the REST API answers, and now you want a custom web map that moves vehicles live. You open a WebSocket to /api/socket , and it fails. Or it connects from Traccar's own UI but never from your page. This isn't a bug in your code. It comes from two design choices in Traccar, and once you see them, the fix is a short nginx config. The two problems 1. The WebSocket only…
Traccar's WebSocket requires a proxy due to two design choices in the software. Firstly, the WebSocket only accepts a session cookie for authentication, which cannot be added to a WebSocket connection in the browser. Secondly, Traccar does not send any CORS headers by default, causing cross-origin requests to be blocked by the browser.
To solve these issues, set up a reverse proxy using nginx. This proxy serves the frontend files and forwards API requests to Traccar, allowing the browser to automatically send the session cookie and eliminating the need for CORS headers. Implementing this configuration involves updating the nginx configuration file with specific proxy settings and ensuring the frontend and Traccar services are accessible through the proxy.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.