Search code examples
nginxodoovps

Odoo websocket issue


I'm constantly getting this issue in my logs. I saw a couple of references as to what might be the issue and how to fix it, but had no luck solving it.

Logs:

2023-05-08 15:41:42,422 1130561 INFO live longpolling: 127.0.0.1 - - [2023-05-08 15:41:42] "GET /websocket HTTP/1.0" 400 281 0.003553

odoo.conf:

[options]
db_user = odoo16
dbfilter = live
list_db = False
proxy_mode = True
xmlrpc_port = 8069
gevent_port = 8072
longpolling_port = False
limit_memory_hard = 741667999
limit_memory_soft = 593334399
limit_request = 8192
limit_time_cpu = 120000
limit_time_real = 180000
max_cron_threads = 2
workers = 9
logfile = /var/log/odoo16/odoo-server.log

nginx.conf:

upstream live {
    server localhost:8069 weight=1 fail_timeout=3000s;
}

upstream livechat {
    server localhost:8072 weight=1 fail_timeout=3000s;
}

# WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.live.com;

    ssl_certificate /etc/letsencrypt/live/live.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/live.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/live.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://live.com$request_uri;
}


server {
    listen 443 ssl http2;
    server_name live.com;

    # Specifies the maximum accepted body size of a client request,
    # as indicated by the request header Content-Length.
    client_max_body_size        100M;

    # add ssl specific settings
    keepalive_timeout           60;

    # increase proxy buffer to handle some OpenERP web requests
    proxy_buffers               16 64k;
    proxy_buffer_size           128k;

    location / {
        proxy_pass              http://live;

        # Force timeouts if the backend dies
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
        add_header X-Frame-Options "SAMEORIGIN";

        # Set headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_http_version 1.1;

        # Set timeouts
        proxy_connect_timeout   3600;
        proxy_send_timeout      3600;
        proxy_read_timeout      3600;
        send_timeout            3600;

        # By default, do not forward anything
        proxy_redirect          off;
    }

    location /longpolling/ {
        proxy_pass              http://livechat;
        # Force timeouts if the backend dies
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;

        # Set headers
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;

        # Set timeouts
        proxy_connect_timeout   3600;
        proxy_send_timeout      3600;
        proxy_read_timeout      3600;
        send_timeout            3600;

        # By default, do not forward anything
        proxy_redirect          off;
    }

    # Cache some static data in memory for 60mins.
    # under heavy load this should relieve stress on the Odoo web interface a bit.
    location ~* /[0-9a-zA-Z_]*/static/ {
        proxy_cache_valid       200 60m;
        proxy_buffering         on;
        expires                 864000;
        proxy_pass              http://live;
    }

    location /websocket {
        proxy_redirect off;
        proxy_pass http://livechat;
    }

    access_log /var/log/nginx/odoo-ssl.access.log;
    error_log  /var/log/nginx/odoo-ssl.error.log;

    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/live.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/live.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/live.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

}


server {
    listen 80;
    server_name www.live.com live.com;

    include snippets/letsencrypt.conf;
    return 301 https://live.com$request_uri;
}

I'm not sure if it is related but I have Odoo hosted on a VPS maybe some port is not opened.

Aditionally I'm having this logs as well they might be related to the issue.

2023-05-08 16:48:36,679 1131879 INFO ? odoo.sql_db: ConnectionPool(used=1/count=1/max=64): Closed 1 connections to 'application_name=odoo-1131879 user=odoo16 sslmode=prefer dbname=staging' 
2023-05-08 16:49:23,013 1131878 INFO ? odoo.sql_db: ConnectionPool(used=1/count=1/max=64): Closed 1 connections to 'application_name=odoo-1131878 user=odoo16 sslmode=prefer dbname=live_live' 
2023-05-08 16:49:23,028 1131878 INFO ? odoo.sql_db: ConnectionPool(used=1/count=1/max=64): Closed 1 connections to 'application_name=odoo-1131878 user=odoo16 sslmode=prefer dbname=staging' 

Solution

  • In my case the 400 HTTP code was caused by missing header setup.
    Note: These headers are present in OP`s config, just not in proper scope.

        proxy_http_version 1.1;
        proxy_set_header Host $host;
    
        location /websocket {
            proxy_pass http://livechat;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    

    If you are in debug mode, you should see these lines appearing in the browser console if the websocket connection was successful (at least in odoo16).

    8/26/2023, 4:16:28 PM - [onOpen] websocket_worker_bundle:74:45
    8/26/2023, 4:16:28 PM - [onMessage] websocket_worker_bundle:72:10