title: Geth websocket over nginx reverse proxy
body: <p>I try to connect to my private geth blockchain over a websocket using nginx as reverse proxy. This is my setup:</p>
  
  <p><strong>Node setup:</strong></p>
  
  <pre><code>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"
  </code></pre>
  
  <p><strong>Nginx config:</strong></p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p><strong>web3.js:</strong></p>
  
  <pre><code>const Web3 = require('web3');
  
  const web3 = new Web3('ws://mydomain.de/mynode');
  
  web3.eth.getAccounts()
      .then(console.log)
      .catch(console.log);
  </code></pre>
  
  <p>This config isn't working with websocket. Before I used it with <code>RPC</code> and it was really reliable.</p>
  
  <p>If I add <code>-p 8546:8456</code> to my node and connect directly to it (<code>const web3 = new Web3('ws://mydomain.de:8456')</code>), than everything is working fine. So I guess there is something wrong in the nginx config.</p>