title: How can I use Nginx to do Secure Web Socket Connections?
body: <p>I can directly connect a web socket to my PHP daemon server via JavaScript: var websocket = new WebSocket("ws://IP:PORT"); This will get the handshake correctly, but when I try the nginx proxy <a href="http://IP" rel="nofollow noreferrer">http://IP</a> it fails to receive the header value for Sec-WebSocket-Key and the handshake fails. </p>
  
  <p>-- Recent Update : JavaScript does not connect at all due to: SyntaxError: An invalid or illegal string was specified! </p>
  
  <pre>
  nginx config file:
  
      upstream chatwebsocket {
          server 127.0.0.1:9090;
      }
      server {
         # ...
         listen 80 default_server;
         location / {
            proxy_pass http://chatwebsocket;
            ...
            proxy_set_header Sec-WebSocket-Key      $http_sec_websocket_key;
         } 
  </pre>
  
  <p>The variable: $http_sec_websocket_key is not set...why? What variable should I be using for this? My PHP Chat server does not get the web socket header "Value" called: Sec-Websocket-Key. I can set it manually to some dummy value with: proxy_set_header Sec-WebSocket-Key 13; and my PHP log file will show it got 13 for the key.</p>
  
  <p>Also, when I go to <a href="http://IP" rel="nofollow noreferrer">http://IP</a> I can inspect the response headers in my browser console window. HTTP/1.1 400 Bad Key Request which is my error I created in PHP when: Note - that $headers is converted to lower case and trimmed... and works fine...</p>
  
  <pre><code>$headers[strtolower(trim($matches[1]))] = trim($matches[2]);
  </code></pre>
  
  <pre>
      if (! isset($headers['sec-websocket-key'])) { 
         return "HTTP/1.1 400 Bad Key Request"; //...
  </pre>
  
  <p>Why?, I want to do this is once HTTP works I want to use HTTPS to securely use WebSockets in encrypted form, also to keep my web page traffic secure. </p>
  
  <p>I've looked at these pages: <a href="https://github.com/die-net/nginx-config-example/blob/master/include/proxy-headers" rel="nofollow noreferrer">https://github.com/die-net/nginx-config-example/blob/master/include/proxy-headers</a> and <a href="http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header" rel="nofollow noreferrer">http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header</a> . 
  Not sure why $http_sec_websocket_key is not getting a value set...or what variable to use in its place?</p>
  
  <hr>
  
  <p>Seems to be a JavaScript issue:
  var websocket = new WebSocket('<a href="http://IP" rel="nofollow noreferrer">http://IP</a>');
  SyntaxError: An invalid or illegal string was specified</p>
  
  <p>How can I connect JavaScript Page to a Nginx web page proxy, is it possible? Or how do I setup a server to do wss://IP </p>
  
  <p>OR any small simple code in PHP to do wss sockets?</p>
  
  <hr>
  
  <p>Ha, I got it now, here is the Nginx config:</p>
  
  <pre><code>map $http_upgrade $connection_upgrade {
      default upgrade;
      '' close;
  }
  
  upstream chatwebsocket {
      server 127.0.0.1:9090;
  }
  
  server {
      listen 8020;
      location / {
          proxy_pass http://chatwebsocket;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
      }
  }
  </code></pre>
  
  <p>I was missing the proxy_set_header **</p>
  
  <blockquote>
    <p>Upgrade</p>
  </blockquote>
  
  <p>** $http_upgrade;</p>