4

I need to proxy SSH through NGINX through the same domain. I have one proxy setup on port 80, working fine. But I need port 22 to be proxied to the same server.

Original Configuration:

    upstream web {
        least_conn;
        server 10.0.0.4;
}

server {
        access_log /var/log/nginx/web.com combined;
        index index.html index.htm index.php;

        server_name www.web.com web.com;

        location /{
        proxy_pass http://web;
        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;
}

}

I have a configuration for port 22 like this:

stream {

        upstream ssh {
                server 10.0.0.17:22;
        }

        server {
                listen 22;
                server_name gitlab.web.com;
                proxy_pass ssh;
        }

}

When I connect to the domain it tries to connect the nginx server not 10.0.0.17.

1 Answers1

1

For SSH proxy through Nginx, use a different port other than port 22 for the SSH server

# use a different port for SSH client if Ngnix uses
# port 22
Port 8022

Or if you want to stay port 22 to SSH server, you may need to configure your Nginx config to use another port

stream {
    upstream ssh {
            server 10.0.0.17:22;
    }

    server {
            listen 8022;
            server_name gitlab.web.com;
            proxy_pass ssh;
    }

}

If you configured that way, your GitLab CE/EE users might need to do these instead:

# unless you blocked port 22, users who do Git-over-SSH need to configure
# stuff on their clients instead of doing this.
ssh git@gitlab.web.com -p 8022
  • And how do you access the VM (EC2 in AWS in my case) if you cannot connect trough SSH? – AlexAcc Aug 11 '22 at 09:20
  • I mean, once you start Nginx, the port 22 is blocked and impossible to connect by for SSH, so there is no access to the VM. – AlexAcc Aug 11 '22 at 13:42
  • Changing the port sshd listens on will ado the trick. Have Nginx listen on port 22, and have sshd listen on port 222 for example. – Minecraftchest1 Nov 08 '23 at 12:30