Nginx configuration implements tcp-based reverse proxy and load balancing

Source: Internet
Author: User
: This article describes how to configure Nginx to implement reverse proxy and server load balancer based on the tcp protocol. For more information about PHP tutorials, see. 1. installation environment

System environment: centos6.3 _ x64

Software version: nginx-1.4.2.tar.gz nginx_tcp_proxy_module-master.zip

2. software installation cd/root/
Wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.12/pcre-8.12.tar.gz
Tar zxvf pcre-8.12.tar.gz
Cd pcre-8.12/
./Configure
Make & make install
Cd/usr/local/src
Wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
Tar-zxvf openssl-1.0.1c.tar.gz
Cd/root/
Wget http://nginx.org/download/nginx-1.4.2.tar.gz
Groupadd www
Useradd-g www
Tar zxvf nginx-1.4.2.tar.gz
Cd nginx-1.4.2/
# Download nginx_tcp_proxy_module from https://github.com/yaoweibin/nginx_tcp_proxy_module
Unzip nginx_tcp_proxy_module-master.zip
Patch-p1 </root/nginx-1.4.2/nginx_tcp_proxy_module-master/tcp. patch
. /Configure -- user = www -- group = www -- prefix =/usr/local/nginx -- with-http_stub_status_module -- with-http_ssl_module -- with-openssl =/usr/local/src/openssl-1.0.1c -- add-module =/root/nginx-1.4.2/nginx_tcp_proxy_module-master
Make & make install

3. nginx configuration

# Cat/usr/local/nginx/conf/nginx. conf
User www;
Worker_processes 8; # equivalent to the number of CPU cores
Error_log logs/error. log;
Pid logs/nginx. pid;
Events {
Use epoll;
Worker_connections 51200;
}
Http {
Include mime. types;
Default_type application/octet-stream;
Log_format main '$ remote_addr-$ remote_user [$ time_local] "$ request "'
'$ Status $ body_bytes_sent "$ http_referer "'
'"$ Http_user_agent" "$ http_x_forwarded_for "';
# Access_log logs/access. log main;
Sendfile on;
# Tcp_nopush on;
Keepalive_timeout 65;
Client_header_buffer_size 4 k;
Large_client_header_buffers 4 16 k;
Client_max_body_size 50 m;
Client_body_buffer_size 256 k;
Proxy_connect_timeout 600;
Proxy_read_timeout 600;
Proxy_send_timeouts 600;
Proxy_buffer_size 64 k;
Proxy_buffers 4 64 k;
Proxy_busy_buffers_size 128 k;
Proxy_temp_file_write_size 128 k;
Gzip on;
}
Tcp {
#### Dhserver ####
Upstream dh {
Server 192.168.5.83: 26888;
Server 192.168.5.84: 26888;
Check interval = 3000 rise = 2 fall = 5 timeout = 1000;
}
Server {
Listen 26888;
Proxy_pass dh;
}
#### Mainserver ####
Upstream main {
Server 192.168.5.85: 8088;
Server 192.168.5.86: 8088;
Check interval = 3000 rise = 2 fall = 5 timeout = 1000;
}
Server {
Listen 8088;
Proxy_pass main;
}
#### Files ####
Upstream files1 {
Server 192.168.5.93: 30001;
Server 192.168.5.94: 30001;
Check interval = 3000 rise = 2 fall = 5 timeout = 1000;
}
Server {
Listen 30001;
Proxy_pass files1;
}
Upstream files2 {
Server 192.168.5.93: 30002;
Server 192.168.5.94: 30002;
Check interval = 3000 rise = 2 fall = 5 timeout = 1000;
}
Server {
Listen 30002;
Proxy_pass files2;
}

}

PS: A lot of NGINX proxy tests have been done before, and port-based forwarding services have all ended in failure. only this method can be used.

Another server with NGINX installed may need to be uninstalled and reinstalled because it is played in the installation package during the patching phase. at least so far, I have not found a patch for the installed nginx service. README only introduces patching under the installation package.

The above describes how to configure Nginx to implement reverse proxy and load balancing based on the tcp protocol, including some content, and hope to help friends who are interested in the PHP Tutorial.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.