title: nginx + ssh_exchange_identification: Connection closed by remote host
body: <p>an nginx server is connected upstream as a reverse proxy server to separate SSH and SSL on one port (12345). The nginx recognizes the data transfer protocol and separates the data traffic behind the proxy accordingly on two ports -&gt; SSH is forwarded to PORT 9999 and SSL requests to PORT 8080 (Python web server).</p>
  <p>The nginx is configured as follows:</p>
  <p>/etc/nginx/nginx_conf</p>
  <pre><code>[...]
  
  stream {
      upstream ssh {
          server localhost:9999;
      }
  
      upstream https {
          server localhost:8080;
      }
  
      map $ssl_preread_protocol $upstream {
          default ssh;
          &quot;&quot; https;
          &quot;TLSv1.2&quot; https;
          &quot;TLSv1.3&quot; https;
          &quot;TLSv1.1&quot; https;
          &quot;TLSv1.0&quot; https;
      }
  
      # SSH and SSL on the same port
      server {
          listen 12345;
  
          proxy_pass $upstream;
          ssl_preread on;
      }
  }
  
  [...]
  </code></pre>
  <p>When the web server is called via http://rpi_ip:12345, port 8080 is forwarded cleanly and access to the web server is carried out as requested.</p>
  <p>The problem lies in trying to establish a connection via SSH.</p>
  <p>I can connect locally to the Raspberry Pi via ssh user@ip-rpi -p 9999 but not via the nginx port 12345.</p>
  <p>Adjustments in /etc/ssh/sshd_config</p>
  <pre><code>[...]
  
  Port 9999
  AddressFamily any
  ListenAddress 0.0.0.0
  #ListenAddress ::
  
  [...]
  </code></pre>
  <p>A call to ssh user@ip-rpi -p 12345 -vvv brings:</p>
  <pre><code>OpenSSH_7.9p1 Raspbian-10+deb10u2, OpenSSL 1.1.1d  10 Sep 2019
  debug1: Reading configuration data /etc/ssh/ssh_config
  debug1: /etc/ssh/ssh_config line 19: Applying options for *
  debug2: resolve_canonicalize: hostname 192.168.2.198 is address
  debug2: ssh_connect_direct
  debug1: Connecting to 192.168.2.198 [192.168.2.198] port 12345.
  debug1: Connection established.
  debug1: identity file /root/.ssh/id_rsa type -1
  debug1: identity file /root/.ssh/id_rsa-cert type -1
  debug1: identity file /root/.ssh/id_dsa type -1
  debug1: identity file /root/.ssh/id_dsa-cert type -1
  debug1: identity file /root/.ssh/id_ecdsa type -1
  debug1: identity file /root/.ssh/id_ecdsa-cert type -1
  debug1: identity file /root/.ssh/id_ed25519 type -1
  debug1: identity file /root/.ssh/id_ed25519-cert type -1
  debug1: identity file /root/.ssh/id_xmss type -1
  debug1: identity file /root/.ssh/id_xmss-cert type -1
  debug1: Local version string SSH-2.0-OpenSSH_7.9p1 Raspbian-10+deb10u2
  debug1: ssh_exchange_identification: &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot;
  
  debug1: ssh_exchange_identification:         &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
  
  debug1: ssh_exchange_identification: &lt;html&gt;
  
  debug1: ssh_exchange_identification:     &lt;head&gt;
  
  debug1: ssh_exchange_identification:         &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=utf-8&quot;&gt;
  
  debug1: ssh_exchange_identification:         &lt;title&gt;Error response&lt;/title&gt;
  
  debug1: ssh_exchange_identification:     &lt;/head&gt;
  
  debug1: ssh_exchange_identification:     &lt;body&gt;
  
  debug1: ssh_exchange_identification:         &lt;h1&gt;Error response&lt;/h1&gt;
  
  debug1: ssh_exchange_identification:         &lt;p&gt;Error code: 400&lt;/p&gt;
  
  debug1: ssh_exchange_identification:         &lt;p&gt;Message: Bad HTTP/0.9 request type ('SSH-2.0-OpenSSH_7.9p1').&lt;/p&gt;
  
  debug1: ssh_exchange_identification:         &lt;p&gt;Error code explanation: HTTPStatus.BAD_REQUEST - Bad request syntax or unsupported method.&lt;/p&gt;
  
  debug1: ssh_exchange_identification:     &lt;/body&gt;
  
  debug1: ssh_exchange_identification: &lt;/html&gt;
  
  ssh_exchange_identification: Connection closed by remote host
  </code></pre>
  <p>At this point I hope for your help/ideas to solve my problem.</p>