title: Docker nginx websocket proxy== client closed connection while waiting for request
body: <p>I desperately trying to setup nginx proxy to websocket. Websocket connects but then I'm getting </p>
  
  <blockquote>
    <p>2018/02/10 19:30:34 [info] 7#7: *238 client closed connection while
    waiting for request, client: 172.18.0.1, server: 0.0.0.0:8888</p>
  </blockquote>
  
  <p>Here's the minimal example:</p>
  
  <p><strong>nginx.conf</strong></p>
  
  <pre><code>worker_processes  1;
  
  events {
      worker_connections  1024;
  }
  error_log /dev/stdout debug;
  
  http  {
      resolver 127.0.0.11 ipv6=off;
      include       mime.types;
      access_log /dev/stdout;
      default_type  application/octet-stream;
  
      sendfile        on;
  
      keepalive_timeout  65;
  
      upstream tornado {
        server ws:8888;
        #server ws:8889; I may add another upstream here
      }
  
      server {
           add_header X-Frame-Options SAMEORIGIN;
           add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";
  
          listen 8888;
  
          server_name pychat.org;
          charset     utf-8;
          client_max_body_size 75M;
  
          location / {
               proxy_pass http://tornado/;
               #&gt;&gt;&gt; otherwise error 400
               proxy_set_header Upgrade $http_upgrade;
               proxy_set_header Connection "upgrade";
               proxy_set_header Host $host;
               proxy_send_timeout 330;
               proxy_read_timeout 330;
               #&gt;&gt;&gt;&gt;&gt;&gt;
               #proxy_redirect off;
               proxy_set_header X-Real-IP $remote_addr;
               #proxy_set_header X-Scheme $scheme;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>server.py</strong></p>
  
  <pre><code>import tornado.ioloop
  from tornado.websocket import WebSocketHandler, WebSocketClosedError
  from tornado import web
  class MainHandler(WebSocketHandler):
  
      def open(self):
          print("WebSocket opened")
  
      def on_message(self, message):
          print("ws mess" +  message)
          self.write_message(u"You said: " + message)
  
      def on_close(self):
          print("WebSocket closed")
  
      def check_origin(self, origin):
          return True
  
  def make_app():
      return tornado.web.Application([
          (r'.*', MainHandler),
      ])
  
  if __name__ == "__main__":
      app = make_app()
      app.listen(8888)
      tornado.ioloop.IOLoop.current().start()
  </code></pre>
  
  <p><strong>docker-compose.yml</strong></p>
  
  <pre><code>version: '2.3'
  services:
    nginx:
      build:
        context: ../
        dockerfile: ./dockerfilenginx
      ports:
       - 8888:8888
    ws:
      build:
        context: ../
        dockerfile: ./docker/Dockerfilews
  </code></pre>
  
  <p><strong>Dockerfilenginx</strong>:</p>
  
  <pre><code>FROM alpine:3.6
  RUN apk update &amp;&amp;\
  apk add vim nginx ca-certificates wget &amp;&amp; update-ca-certificates
  COPY ./docker/nginx-test.conf /etc/nginx/nginx.conf
  CMD ["nginx", "-g", "pid /tmp/nginx.pid; daemon off;"]
  </code></pre>
  
  <p><strong>Dockerfilews</strong></p>
  
  <pre><code>FROM alpine:3.6
  RUN apk update &amp;&amp;\
   apk add python3 &amp;&amp;\
  pip3 install tornado
  WORKDIR /usr/src
  COPY ./server.py /usr/src
  CMD python3 server.py
  </code></pre>
  
  <p>Then just try to open a WebSocket to the server, e.g. from browser console (shift+ctrl+i).</p>
  
  <pre><code>ws = new WebSocket('ws://localhost:8888')
  ws.send("wsdata")
  </code></pre>
  
  <p>If I remove nginx proxy and just expose websocket port on docker - everything will work.</p>