tactoth
tactoth

Reputation: 917

Is it possible to forward NON-http connecting request to some other port in nginx?

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?

Upvotes: 30

Views: 47608

Answers (3)

Slava V
Slava V

Reputation: 17286

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;
    }
}

Upvotes: 69

rogerdpack
rogerdpack

Reputation: 66851

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).

Upvotes: 0

Zimbabao
Zimbabao

Reputation: 8250

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.

Upvotes: 4

Related Questions