Is it possible to forward NON-http connecting requ

2019-02-01 01:14发布

问题:

I have nginx running on my server, listening port 80 and 433. I know nginx has a number ways of port forwarding that allows me to forward request like: http://myserver:80/subdir1 to some address like: http://myserver:8888.

My question is it possible to configure nginx so that i can forward NON-http request (just those plain TCP connection) to some other port? It's very easy to test if it's a http request because the first bytes will be either "GET" or "POST". Here's the example.

The client connected to nginx . The client send:

a. HTTP get request: "GET / HTTP 1.1": some rule for HTTP

b. Any bytes that can't be recognized as HTTP header: forward it to some other port, say, 888, 999, etc.

Is it technically possible? Or would you suggest a way to do this?

回答1:

This is technically possible for sure.

You can modify open source tcp proxies like nginx module called nginx_tcp_proxy_module or HAproxy.

Or you can write a nginx module similar to above one to do this for you.



回答2:

It is possible since nginx 1.9.0:

http://nginx.org/en/docs/stream/ngx_stream_core_module.html

Something along these lines (this goes on top level of nginx.conf):

stream {
    upstream backend {
        server backend1.example.com:12345;
    }

    server {
        listen 12345;
        proxy_pass backend;
    }
}


回答3:

if nginx remote proxying with HTTP, your client could use the HTTP CONNECT command, then it connects with the remote port and forwards all data as "raw" (or at least I think so).



标签: http tcp nginx