websocket connection error when putting traccar behind nginx

Angelo5 years ago

Hi,
I run traccar in a docker container, in the browser I always got:

app.min.js: WebSocket connection to 'ws://mycarsample123.com:8090/api/socket' failed: Error during WebSocket handshake: Unexpected response code: 400

I don't know why it goes back to 8090, actually mycarsample123.com:80 is a nginx:

 location / {
     
       # client_max_body_size 4M;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://traccar:8082/;
    }

any idea how to put traccar behind a nginx?

Angelo5 years ago
Angelo5 years ago

with following, not getting error: WebSocket connection to 'ws://mycarsample123.com:8090/api/socket' failed: Error during WebSocket handshake:
but browser still pops up 'websocket connection error', why?

location / {

   # client_max_body_size 4M;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_pass http://traccar:8082/;
}

location /api/socket {
    proxy_pass http://traccar:8082/api/socket;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
Julio Adrian T5 years ago

This worked for me, don't ask me to explain, not expert, just trial and error:
Traccar is working on "mydomain.com/gps" folder

location /gps/ {

    add_header Cache-Control no-cache;
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; $
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Frame-Options "";
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_cookie_path /api /gps/api;
    proxy_redirect http://localhost:8082/ /;
    proxy_redirect ws:/localhost:8082/api/socket /api/socket;
    proxy_pass http://localhost:8082/;
    proxy_read_timeout 86400s;
    proxy_send_timeout 86400s;
    allow all; # Any IP can perform any other requests

  }
Ebskia year ago

I also had an issue with HTTP 400 Bad request errors for the websocket requests in nginx.
I run traccar on a subdomain but it would be the same config if it was run on a root domain.

    location /api/socket {
       proxy_pass http://localhost:8082/api/socket;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "upgrade";
    }

    location / {
       proxy_set_header X-Forwarded-Host $host:$server_port;
       proxy_set_header X-Forwarded-Server $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

       proxy_pass http://localhost:8082/;
    }

The order might be important but I'm not sure. It works for me now and I can see the websocket communication in the inspect session on chrome.