Ssh – How to set ssh tunnel properly so you can access site on the server as localhost

ssh

I've restricted some part of the sites just to localhost access for security reasons.
In order to access them i would make a ssh tunnel like this
ssh -L 8080:localhost:80 username@server.com
and set the local browser at my pc to proxy 127.0.0.1:8080

Well I guess i'm missing something because nothing loads in the local browser
and in the terminal at the server side it says
channel 3: open failed: connect failed: Connection refused
every time i tried to load something at the local pc

firewall was off at the time of trying this

Best Answer

As has been discussed, ssh -L 8080:localhost:80 username@server.com doesn't start a magic proxy server on the client side, it simply forwards client port 8080 to server port 80. You need to get the client web browser to connect to client port 8080, which as others and myself have said involves pointing your client web browser at http://localhost:8080/.

Your new problem is that the server is running a number of name-based virtual hosts, and you don't get the right host served to you when you don't request it from the server in the URL, which is reasonable enough.

The simplest workround is to tell your client to access the site by name, but to get the OS to lie to the browser about what IP address that host resolves to. Let us suppose that you want to access hosts vsite1.example.com and vsite2.example.org, which are both being served on port 80 on server, via the SSH tunnel we have already set up.

Edit your client-side /etc/hosts file to tell your OS that those hostnames resolve to 127.0.0.1, with entries such as

127.0.0.1     localhost localhost.localdomain vsite1.example.com vsite2.example.org

I believe there are corresponding hacks for Windows, but I don't know what they are, as I never use it.

Now you can point the client browser at http://vsite1.example.com:8080/, the client OS will tell the browser that's on localhost, the URL will point the browser to localhost port 8080, ssh will conduct the packets sub rosa to server port 80, and client browser will ask server's web server for the right vhost.