1、nginx.conf 配置

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''   close;
}

upstream websocket {
    server socket-host:8080;
}

server {
    listen      80;
    #listen      443 ssl;
    server_name domain.com;

    # ssl_certificate certs.d/domain.crt;
    # ssl_certificate_key certs.d/domain.key;

    location / {
        proxy_pass http://websocket;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Real-PORT $remote_port;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header Server-Protocol $server_protocol;
        proxy_set_header Server-Name $server_name;
        proxy_set_header Server-Addr $server_addr;
        proxy_set_header Server-Port $server_port;
        # 以下重要
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

配置后即可通过 ws://domain.com 或 wss://domain.com 访问websocket服务(ws://socket-host:8080)