[Ethereum] Invalid JSON RPC response: “” Geth RPC on custom domain with nginx

go-ethereumjson-rpc

in my geth.service file I have..

Description=Ethereum Go Client
[Service]
ExecStart=/usr/bin/geth --cache=1000 --datadir=/mnt/eth-blockchain --keystore=/mnt/eth-blockchain --rpc --rpcport=8882 --rpccorsdomain=* --
rpcapi=web3,db,net,eth
Restart=always
RestartSec=30
Type=simple
User=root
Group=root
[Install]

And in the nginx conf file
/etc/nginx/conf.d/eth.domain.com.conf
I have…

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

server {
    listen 80;
    client_body_timeout 5s;
    client_header_timeout 5s;
    server_name eth.domain.com;

    location / {
        limit_req zone=one;
        limit_conn addr 10;        
        proxy_pass http://localhost:8882;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
}

I run the below code in the browser

    web.eth.getBalance('0xa1fe5d3ca40da27705847361351c34a3e77d187a', 'latest', function(error, result){
        if(!error)
            console.log(result)
        else
            console.error(error);
    })

And the response is..

web3-hook.html:68 Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (web3.min.js:1)
    at XMLHttpRequest.n.onreadystatechange (web3.min.js:2)

If I change the host to a different RPC server I don't get the error message, so I know it's a server issue. Both geth and nginx look right to me, what am I missing?

I have also tried starting it from the console with

admin.startRPC("127.0.0.1", 8882, "*", "web3,db,net,eth")

and then I can curl it with

curl -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' localhost:8882

and I get the response

{"jsonrpc":"2.0","id":67,"result":"Geth/@binky/v1.6.0-stable-facc47cb/linux-amd64/go1.8.1"}

Best Answer

all of this is not necessary

    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With' always;
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;

I then had to remove

--rpc --rpcport=8882 --rpccorsdomain=* --rpcapi=web3,db,net,eth

then start the console and do

admin.startRPC("127.0.0.1", 8882, "*", "web3,db,net,eth")

at this point, it was all working. I added back

 --rpc --rpcport=8882 --rpccorsdomain=* --rpcapi=web3,db,net,eth

rebooted and all is still good!