Is it possible to forward NON-http connecting request to some other port in nginx?
Solution 1
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;
}
}
Solution 2
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.
Related videos on Youtube
tactoth
Java, RTMP, Live streamming, iOS, Android, Boost, curl, C++0x
Updated on July 09, 2022Comments
-
tactoth almost 2 years
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?
-
tactoth over 13 yearsdo i need to write some script, or modify some configuration files, or recompile nginx?
-
Zimbabao over 13 yearsYou need to modify code of the nginx module itself. Bit difficult but doable.
-
Torben E almost 3 yearsYou can also add this code into any /etc/nginx/modules-enabled/*.conf (or similar) file, since this would be included. When using Docker, make sure to copy the /etc/nginx/nginx.conf file to the container even if you don't intend to make changes there, this solved the problem for me