Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx reverse proxying wss - client sent invalid method while reading client request line

Have this ridiculous issue setting up nginx to reverse proxy a websocket (a Mosquitto MQTT service). The following config works perfectly for ws:// but fails for wss://

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log info;

    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    server {
        listen 80;
        listen 443 ssl;

        # nginx is smart enough to ignore these when serving HTTP instead of HTTPS
        ssl_certificate /etc/nginx/cert;
        ssl_certificate_key /etc/nginx/key;

        location /ws {
            # access_log off;

            rewrite ^/ws$ / break;
            rewrite ^/ws(.*)$ $1 break;

            proxy_pass http://mqtt:9001;
            proxy_redirect default;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;

            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_read_timeout 86400;
        }

    }

    client_max_body_size 1M;
    client_body_buffer_size 1M;
}

Here is what I see in nginx logs:

10.142.0.4 - - [09/Apr/2016:19:15:16 +0000] "\x16\x03\x01\x012\x01\x00\x01.\x03\x03-\xFD\xD4C\x828\xDFai!\xB1\x87\x96l\x8E\xF6a\x00\x059\xC4!\xF1y:\x89\xFF$d^\x87\xE5\x00\x00z\xC0'\x00g\x00\x9C\xC0\x11\xC0\x07\xC0\x0C\xC0\x02\x00\x05\xC00\xC0,\xC0(\xC0$\xC0\x14\xC0" 400 173 "-" "-" 2016/04/09 19:15:17 [info] 7#7: *6 client sent invalid method while reading client request line, client: 10.48.0.1, server: , request: "2.��6OK���4f=4 �����jЁǐ���

I am at a complete loss - please help :( All non-websocket routes (left out of the original snippet for simplicity) are working with SSL, and the websocket with TLS works as well :(

If it helps, I'm running Nginx as a Docker container inside Kubernetes on GCP.

like image 613
Angad Avatar asked Apr 09 '16 19:04

Angad


2 Answers

I cannot believe I wasted an entire day on this. In my MQTT.JS client, I simply changed the URL from wss://my.domain.com/ws to wss://my.domain.com:443/ws and it worked. Leaving this here hoping it saves somebody the time.

like image 188
Angad Avatar answered Oct 24 '22 00:10

Angad


I was consistently able to replicate the issue by modifying the listen line on the server block.

A - ERROR: Reproduce gibberish along with client sent invalid method while reading client request line error.

server {
    listen 443;
server {
    listen ssl;

B - FIXED: Error goes away and I'm able to serve my request (plain html, fastcgi_pass, etc).

server {
    listen 443 ssl;
like image 40
amateur barista Avatar answered Oct 24 '22 00:10

amateur barista