通过 nginx 反向代理的 Geth websocket [英] Geth websocket over nginx reverse proxy

查看:433
本文介绍了通过 nginx 反向代理的 Geth websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 nginx 作为反向代理通过 websocket 连接到我的私有 geth 区块链.这是我的设置:

I try to connect to my private geth blockchain over a websocket using nginx as reverse proxy. This is my setup:

节点设置:

docker run                                                                  
    -d
    --net                       mynet
    --ip                        192.168.1.21
    -v                          myvol:/root
    ethereum/client-go:stable
        --datadir               "/root/geth1"                              
        --networkid             1029
        --syncmode              "full"

        --ws                                                              
        --wsaddr                "0.0.0.0"                                  
        --wsport                8546                                       
        --wsapi                 "eth,net,web3,rpc"                         
        --wsorigins="*"                                                            

        --bootnodes             $BOOTNODE                                  
        --port                  30303                                      
        --maxpeers              8                                          
        --nat                   "any"

Nginx 配置:

server {
    #listen     80;
    listen      443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server ipv6only=on;

    server_name             mydomain.de;

    # basic auth stuff here
    # ssl stuff here

    location /mynode {

        if ($request_method = OPTIONS) {
            return 204;
        }

        auth_basic          off;

        add_header          Access-Control-Allow-Origin  "$http_origin";
        add_header          Access-Control-Allow-Headers "authorization, content-type";
        add_header          Access-Control-Allow-Methods "DELETE, GET, OPTIONS, POST, PUT, UPDATE";

        # to avoid double origin value what leads to an CORS error in the browser
        proxy_hide_header   Access-Control-Allow-Origin;

        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   $scheme;

        proxy_http_version  1.1;
        proxy_set_header    Upgrade             $http_upgrade;
        proxy_set_header    Connection          "upgrade";

        proxy_pass          http://192.168.1.21:8546;
    }
}

web3.js:

const Web3 = require('web3');

const web3 = new Web3('ws://mydomain.de/mynode');

web3.eth.getAccounts()
    .then(console.log)
    .catch(console.log);

此配置不适用于 websocket.在我将它与 RPC 一起使用之前,它真的很可靠.

This config isn't working with websocket. Before I used it with RPC and it was really reliable.

如果我将 -p 8546:8456 添加到我的节点并直接连接到它 (const web3 = new Web3('ws://mydomain.de:8456')),然后一切正常.所以我猜nginx配置有问题.

If I add -p 8546:8456 to my node and connect directly to it (const web3 = new Web3('ws://mydomain.de:8456')), than everything is working fine. So I guess there is something wrong in the nginx config.

推荐答案

如评论中所说,要使用带有 SSL 的 websocket,您需要前缀 wss://.

As said in comment, to use a websocket with SSL, you need to prefix wss://.

这篇关于通过 nginx 反向代理的 Geth websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆