title: How to redirect ssh requests in Nginx?
body: <p>I'm using the gitea versioning system in a docker environment. The gitea used is a rootless type image.</p>
  <p>The http port mapping is “8084:3000” and the ssh port mapping is “2224:2222”.</p>
  <p>I generated the keys on my Linux host and added the generated public key to my Gitea account.</p>
  <p><strong>1.Test environment</strong></p>
  <p>Later I created the ssh config file <code>nano  /home/campos/.ssh/config </code>:</p>
  <pre><code>Host localhost
    HostName localhost
    User git
    Port 2224
    IdentityFile ~/.ssh/id_rsa
  </code></pre>
  <p>After finishing the settings i created the myRepo repository and cloned it.</p>
  <p>To perform the clone, I changed the url from <code>ssh://git@localhost:2224/campos/myRepo.git</code> to <code>git@localhost:/campos/myRepo.git</code></p>
  <p>To clone the repository I typed: <code>git clone git@localhost:/campos/myRepo.git</code></p>
  <p>This worked perfectly!</p>
  <p><strong>2.Production environment</strong></p>
  <p>However, when defining a reverse proxy and a domain name, it was not possible to clone the repository.</p>
  <p>Before performing the clone, I changed the ssh configuration file:</p>
  <pre><code>Host gitea.domain.com
    HostName gitea.domain.com
    User git
    Port 2224
    IdentityFile ~/.ssh/id_rsa
  </code></pre>
  <p>Then I tried to clone the repository again:</p>
  <p><code>git clone git@gitea.domain.com:/campos/myRepo.git</code></p>
  <p>A connection refused message was shown:</p>
  <pre><code>Cloning into 'myRepo'...
  ssh: connect to host gitea.domain.com port 2224: Connection refused
  fatal: Could not read from remote repository.
  
  Please make sure you have the correct access rights
  and the repository exists.
  </code></pre>
  <p>I understand the message is because by default the proxy doesn't handle ssh requests.</p>
  <p>Searching a bit, some links say to use &quot;stream&quot; in Nginx.</p>
  <p>But I still don't understand how to do this configuration. I need to continue accessing my proxy server on port 22 and redirect port 2224 of the proxy to port 2224 of the docker host.</p>
  <p>The <code>gitea.conf</code> configuration file i use is as follows:</p>
  <pre><code>
  server {
      listen              443 ssl http2;
      server_name         gitea.domain.com;
  
      # SSL
      ssl_certificate     /etc/nginx/ssl/mycert_bundle.crt;
      ssl_certificate_key /etc/nginx/ssl/mycert.key;
  
  
      # logging
      access_log          /var/log/nginx/gitea.access.log;
      error_log           /var/log/nginx/gitea.error.log warn;
  
      # reverse proxy
      location / {
          proxy_pass http://192.168.10.2:8084;
          include    myconfig/proxy.conf;
      }
  
  }
  
  # HTTP redirect
  server {
      listen      80;
      server_name gitea.domain.com;
      return      301 https://gitea.domain.com$request_uri;
  }
  
  </code></pre>
  <p><strong>3. Redirection in Nginx</strong></p>
  <p>I spent several hours trying to understand how to configure Nginx's &quot;stream&quot; feature. Below is what I did.</p>
  <p>At the end of the <code>nginx.conf</code> file I added:</p>
  <pre><code>stream {
  include /etc/nginx/conf.d/stream;
  }
  </code></pre>
  <p>In the <code>stream</code> file in <code>conf.d</code>, I added the content below:</p>
  <pre><code>upstream ssh-gitea {
      server 10.0.200.39:2224;
  }
  
  server {
      listen 2224;
      proxy_pass ssh-gitea;
  }
  </code></pre>
  <p>I tested the Nginx configuration and restart your service:</p>
  <p><code>nginx -t &amp;&amp; systemctl restart nginx.service</code></p>
  <p>I viewed whether ports 80,443, 22 and 2224 were open on the proxy server.</p>
  <p><code>ss -tulpn</code></p>
  <p>This configuration made it possible to perform the ssh clone of a repository with a domain name.</p>
  <p><strong>4. Clone with ssh correctly</strong></p>
  <p>After all the settings I made, I understood that it is possible to use the original url <code>ssh://git@gitea.domain.com:2224/campos/myRepo.git</code> in the clone.</p>
  <p>When typing the command <code>git clone ssh://git@gitea.domain.com:2224/campos/myRepo.git</code>, it is not necessary to define the <code>config</code> file in ssh.</p>
  <p>This link helped me:</p>
  <p><a href="https://discourse.gitea.io/t/password-is-required-to-clone-repository-using-ssh/5006/2" rel="nofollow noreferrer">https://discourse.gitea.io/t/password-is-required-to-clone-repository-using-ssh/5006/2</a></p>