【问题标题】:nginx reverse proxying wss - client sent invalid method while reading client request linenginx反向代理wss - 客户端在读取客户端请求行时发送了无效方法
【发布时间】:2016-07-31 00:44:44
【问题描述】:

在设置 nginx 以反向代理 websocket(Mosquitto MQTT 服务)时遇到了这个荒谬的问题。 以下配置对于 ws:// 非常有效,但对于 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;
}

这是我在 nginx 日志中看到的内容:

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 客户端在读取客户端请求行时发送了无效方法,客户端:10.48.0.1,服务器:,请求:“2.��6OK���4f =4 �����jЁǐ���

我完全不知所措 - 请帮助 :( 所有非 websocket 路由(为简单起见,在原始 sn-p 之外)都在使用 SSL,并且使用 TLS 的 websocket 也可以使用 :(

如果有帮助,我会在 GCP 上的 Kubernetes 中将 Nginx 作为 Docker 容器运行。

【问题讨论】:

    标签: nginx websocket mqtt google-kubernetes-engine mosquitto


    【解决方案1】:

    我不敢相信我在这件事上浪费了一整天。在我的 MQTT.JS 客户端中,我只是将 URL 从 wss://my.domain.com/ws 更改为 wss://my.domain.com:443/ws 并且它起作用了。把这个留在这里,希望它能节省别人的时间。

    【讨论】:

      【解决方案2】:

      通过修改服务器块上的listen 行,我始终能够重现该问题。

      A - 错误: 复制乱码以及client sent invalid method while reading client request line 错误。

      server {
          listen 443;
      
      server {
          listen ssl;
      

      B - 已修复:错误消失了,我可以处理我的请求(纯 html、fastcgi_pass 等)。

      server {
          listen 443 ssl;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-21
        • 1970-01-01
        • 2021-09-01
        • 2021-11-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多