通过websocket将web3连接到使用nginx的节点时出现问题

通过websocket将web3连接到使用nginx的节点时出现问题,nginx,websocket,blockchain,solidity,web3,Nginx,Websocket,Blockchain,Solidity,Web3,我们正在尝试通过WebSocket连接到geth节点,以便能够订阅合同事件。这个节点是用docker创建的,这个docker使用nginx作为代理 我们可以轻松地使用http(而不是WS-rpc)进行连接,但无法使用http订阅契约事件 我们已经成功地在docker映像的本地实例中与具有相同nginx代理的nodejs websocket服务器建立了连接。但是我们无法连接web3 v1.0.0-beta55 我们得到的两个不同错误是403(禁止)与此nginx配置(这是一个适用于非web3 we

我们正在尝试通过WebSocket连接到geth节点,以便能够订阅合同事件。这个节点是用docker创建的,这个docker使用nginx作为代理

我们可以轻松地使用http(而不是WS-rpc)进行连接,但无法使用http订阅契约事件

我们已经成功地在docker映像的本地实例中与具有相同nginx代理的nodejs websocket服务器建立了连接。但是我们无法连接web3 v1.0.0-beta55

我们得到的两个不同错误是403(禁止)与此nginx配置(这是一个适用于非web3 websockets的配置):

或此其他配置出现错误400(错误请求):

location /rpcws {
    proxy_pass http://localhost:22001;
}
在客户端,我们可以

Error during WebSocket handshake: Unexpected response code: 400||403

现在,我们正在跟踪此docker映像的本地实例中可能存在的端口问题,但是我们已经多次配置节点通过已经在工作的http rpc端口接收此ws连接(显然,将所有geth和nginx配置更改为接收wsrpc而不是http rpc),并且我们得到了相同的错误代码

我们的主要猜测是nginx没有正确地传递WebSocket请求。我们询问了仲裁网络技术团队,他们从未尝试建立WebSocket连接,他们不知道如何进一步帮助我们。任何猜测都很受欢迎

谢谢

下面列出了所有代码

Solidity智能合约:

pragma solidity 0.4.18;

contract EventTest {

   string fName;
   uint age;

   event doSetInstructor();
   event instructorSetted(string name, uint age);

   function askForSetInstructor() public {
       doSetInstructor();
   }

   function setInstructor(string _fName, uint _age) public {
       fName = _fName;
       age = _age;
       instructorSetted(fName, age);
   }

   function getInstructor() public constant returns (string, uint) {
       return (fName, age);
   }

}
Web3连接:

var Web3 = require('web3');
var TruffleContract =  require('truffle-contract');
var eventTestABI = require('./abi/EventTest.json');
var io = require('socket.io-client');

var web3 = new Web3(new Web3.providers.WebsocketProvider('ws://9.43.80.817/rpcws'));

var contractAddress;


web3.eth.defaultAccount = '0x41E4e56603bF37a03Bb5Asa635787b3068052b82';


let truffleContract = TruffleContract(eventTestABI);
contractAddress = '0x82ce1df01f2a8bcadfad485eaa785424123734f7';
let contract = new web3.eth.Contract(eventTestABI.abi, contractAddress, {
    from: '0x41E4e56603bF37a03Bb5Asa635787b3068052b82',
    gas: 20000000,
    gasPrice: 0,
    data: truffleContract.deployedBytecode
});

web3.eth.subscribe('logs', {
    address: contract.options.address,
    topics: [contract.events.doSetInstructor().signature]
}, (error, result) => {
    if (!error) {
        console.log("Event triggered");
        const eventObj = web3.eth.abi.decodeLog(
            eventJsonInterface.inputs,
            result.data,
            result.topics.slice(1)
        )
        console.log("New event!", eventObj)
        console.log(eventObj);
    }else{
        console.log("Error watching event", error);
    }
});
geth设置:

--networkid $NETID --identity $IDENTITY --permissioned --ws --wsaddr 0.0.0.0 
--wsport 22001 --wsapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,
istanbul --wsorigins '*' --rpc --rpcaddr $RPCADDR --rpcapi admin,db,eth,debug,miner,
net,shh,txpool,personal,web3,quorum,istanbul --rpccorsdomain '*' --rpcport 22000
--port 21000 --istanbul.requesttimeout 10000  --ethstats $IDENTITY --verbosity 3 --vmdebug --emitcheckpoints --targetgaslimit 18446744073709551615 --syncmode full --gcmode $GCMODE --vmodule consensus/istanbul/core/core.go=5 --nodiscover
nginx conf文件:

limit_req_zone $binary_remote_addr zone=one:10m rate=999999999999999999r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;


client_body_buffer_size 128k;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    root /var/www/html;
    access_log /var/log/nginx/access_log combined;
    error_log /var/log/nginx/error.log warn;
    index index.html index.htm index.nginx-debian.html;
    #ssl_certificate /etc/ssl/nginx/alastria-test.crt;
    #ssl_certificate_key /etc/ssl/nginx/alastria-test.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    client_body_timeout 30s;
    client_header_timeout 30s;

    add_header 'Access-Control-Allow-Headers' 'Content-Type';
    add_header 'Access-Control-Allow-Origin' "http://someurl.com";  

    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location /rpcws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://localhost:22001;
    }
    location /rpc {
        # Request rate and number of connections limitation
        limit_req zone=one burst=30 nodelay;
        limit_conn addr 10;
        # Whitelist/Blacklist
        include ./conf.d/blacklist;
        content_by_lua_block {
            ngx.req.read_body()
            local data = ngx.req.get_body_data()
            if data then
                if not (string.match(data,"eth_") or string.match(data,"net_") or string.match(data,"web3_") or string.match(data, "personal_")) then
                    ngx.exit(403)
                else
                    ngx.exec("@rpc_proxy")
                end
            end
        }
    }

    location @rpc_proxy {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://localhost:22000;
    }
}

如果你找到了解决方案,请把它贴在这里。谢谢如果你找到了解决方案,请把它贴在这里。谢谢
--networkid $NETID --identity $IDENTITY --permissioned --ws --wsaddr 0.0.0.0 
--wsport 22001 --wsapi admin,db,eth,debug,miner,net,shh,txpool,personal,web3,quorum,
istanbul --wsorigins '*' --rpc --rpcaddr $RPCADDR --rpcapi admin,db,eth,debug,miner,
net,shh,txpool,personal,web3,quorum,istanbul --rpccorsdomain '*' --rpcport 22000
--port 21000 --istanbul.requesttimeout 10000  --ethstats $IDENTITY --verbosity 3 --vmdebug --emitcheckpoints --targetgaslimit 18446744073709551615 --syncmode full --gcmode $GCMODE --vmodule consensus/istanbul/core/core.go=5 --nodiscover
limit_req_zone $binary_remote_addr zone=one:10m rate=999999999999999999r/s;
limit_conn_zone $binary_remote_addr zone=addr:10m;


client_body_buffer_size 128k;

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    root /var/www/html;
    access_log /var/log/nginx/access_log combined;
    error_log /var/log/nginx/error.log warn;
    index index.html index.htm index.nginx-debian.html;
    #ssl_certificate /etc/ssl/nginx/alastria-test.crt;
    #ssl_certificate_key /etc/ssl/nginx/alastria-test.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    client_body_timeout 30s;
    client_header_timeout 30s;

    add_header 'Access-Control-Allow-Headers' 'Content-Type';
    add_header 'Access-Control-Allow-Origin' "http://someurl.com";  

    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location /rpcws {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://localhost:22001;
    }
    location /rpc {
        # Request rate and number of connections limitation
        limit_req zone=one burst=30 nodelay;
        limit_conn addr 10;
        # Whitelist/Blacklist
        include ./conf.d/blacklist;
        content_by_lua_block {
            ngx.req.read_body()
            local data = ngx.req.get_body_data()
            if data then
                if not (string.match(data,"eth_") or string.match(data,"net_") or string.match(data,"web3_") or string.match(data, "personal_")) then
                    ngx.exit(403)
                else
                    ngx.exec("@rpc_proxy")
                end
            end
        }
    }

    location @rpc_proxy {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_pass http://localhost:22000;
    }
}