All along, Nginx does not support the TCP protocol, so some TCP-based services in the background can only be done through other high-availability load software, such as Haproxy.
This is a more obvious flaw of Nginx. However, this recognition will be rewritten after the 1.90 release:
2015-04-28 |
nginx-1.9.0 Mainline version has a been released, with the stream module for generic TCP proxying and load balancing. nginx-1.9.0 has been released, and this version adds the stream module for general TCP proxies and load balancing. |
The Ngx_stream_core_module module is available since version 1.9.0. This module isn't built by default, it should are enabled with the--with-stream configuration parameter.
Ngx_stream_core_module This module will be enabled after version 1.90. However, this module is not installed by default and needs to be activated at compile time by specifying the--with-stream parameter.
Other improvements include:
- Change: Remove outdated AIO and Rtsig event handling methods
- Feature: "zone" command can be used in upstream block
- Feature: Streaming module, support for TCP proxies and load balancing
- Feature:ngx_http_memcached_module byte range Support
- The Feature:windows version supports the use of shared memory with a randomized address space layout.
- Feature: the "error_log" directive is available at the mail and server levels
- Bugfix:the "Proxy_protocol" parameter of the "listen" directive did not work if not specified in the first "listen" Direc tive for a listen socket.
So, if we need to use this function, we need to add the--with-stream parameter to recompile nginx. For Nginx that is already running online, you may want to use a smooth upgrade to avoid interruption of service on the line, and refer to the tutorials previously shared by Zhanggo:
Detailed operation record of smooth upgrade or new module in Nginx online service status
Finally, post a simple configuration demo of the stream module, which is officially shared:
Http://nginx.org/en/docs/stream/ngx_stream_core_module.html
Worker_processes Auto;
Error_log/var/log/nginx/error.log info;
Events {
Worker_connections 1024;
}
Stream {
Upstream Backend {
Hash $remote _addr consistent;
Server backend1.example.com:12345 weight=5;
Server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
Server Unix:/tmp/backend3;
}
server {
Listen 12345;
Proxy_connect_timeout 1s;
Proxy_timeout 3s;
Proxy_pass backend;
}
server {
Listen [:: 1]:12345;
Proxy_pass Unix:/tmp/stream.socket;
}
}
Similar to the HTTP module, simple and straightforward. It is easy for a friend who is familiar with Nginx to complete a TCP load Balancer cluster configuration under Nginx.
Because of the busy work, it is really powerless. Fortunately recently the company gave me a small fresh meat to do operation and maintenance assistant, and so empty down, I will test this Nginx TCP proxy and load balancing function. Come back to the blog to share one or two, please look forward to!
The configuration of the Nginx Proxy TCP protocol is tested below.
realserver:10.134.241.68
nginx:10.134.72.166
Client: 10.129.157.168
TCP Listening Port: 2014
First, the configuration Nginx
Crossing documents on the Web http://nginx.org/en/docs/stream/ngx_stream_core_module.html
nginx1.90 the proxy for the TCP protocol is not turned on by default, you need to configure the--with-stream parameter at compile time:
[Img]http://images.cnitblog.com/blog2015/450613/201505/071746123452724.png[/img]
Nginx.config File Reference website:
Stream {
Upstream Cloudsocket {
Hash $remote _addr consistent;
Server 10.134.241.68:2014 weight=5 max_fails=3 fail_timeout=30s;
}
server {
Listen 2014;
Proxy_connect_timeout 1s;
Proxy_timeout 3s;
Proxy_pass Cloudsocket;
}
}
Start Nginx and find Nginx has started listening on port 2014.
Second, the test client connected Realserver
Connect the Realserver 2014 port via telnet to the client:
To view network connections on Realserver:
Can connect normally
Third, test client connection Nginx
Port 2014 of the server to which Nginx is connected via Telnet
Viewing the network connection on the Nginx machine
View Network connections on Realserver
It can be noted that Nginx was given a relay to make a TCP connection.
NGINX:TCP agent and load-balanced stream module