I'm using nginx to create a reverse proxy to a web app that may be behind a firewall. For my initial proof of concept I used the following location block to ensure it worked.
location / {
proxy_pass https://localhost:2222;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
I open up my reverse tunnel on my web app with ssh -f -N -T -R2222:localhost:443 user@nginx-ip
.
This works exactly as I like. I type in my nginx-ip
into my browser and I get the https traffic from my web app but obfuscated by the nginx-ip
, etc.
I want to allow potentially a few thousand reverse tunnels though across a few thousand ports (instead of just 2222 in the above case). Reading up on nginx, I thought to use regular expressions to dynamically use a URI containing the port number to proxy_pass to that specific port.
That is, I'd like https://nginx-ip/2222/
to proxy_pass https://localhost:2222;
and I'd like https://nginx-ip/1111/
to proxy_pass https://localhost:1111;
.
I've tried quite a few variations, but as far as I've been able to reason, I've landed on thinking this should work:
location ~* ^/(\d+) {
proxy_pass https://localhost:$1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
It doesn't. I get a 502 Bad Gateway in my browser and the error log gives me:
2016/05/31 21:02:36 [error] 6188#0: *3584 no resolver defined to resolve localhost, client: {my-client-ip}, server: localhost, request: "GET /2222/ HTTP/1.1", host: "{nginx-ip}"
When I use 127.0.0.1
instead of localhost
I get a 404. The webpage says
Not Found The requested URL /2222/ was not found on this server.
Is what I'm attempting possible with nginx configuration?
To reiterate, I would like to be able to initiate many (thousands) unique reverse tunnels through an nginx web server. My initial thought was to vary the outgoing ports of the nginx server based on what other web app I want to proxy through, and to assign the request to my nginx server to a different port by a port in the URI (extracting it via regex).