nginx安装–with-stream模块
- nginx -V确保nginx安装了–with-stream如果没有,重新用安装nginx
- 安装
yum -y install epel-release
- 安装所有modules
yum -y install nginx-all-modules.noarch
- vi nginx.conf顶部加一行
load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
或者
include /usr/share/nginx/modules/*.conf;
nginx.conf配置
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
#load_module /usr/lib64/nginx/modules/ngx_stream_module.so;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
stream {
# 代理MYSQL 1
upstream mysql_01{
hash $remote_addr consistent;
server 172.168.0.11:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 25674; # 数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s; # 设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass mysql_01;
}
# 代理MYSQL 2
upstream mysql_02{
hash $remote_addr consistent;
server 192.168.0.3:3306 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 25675; # 数据库服务器监听端口
proxy_connect_timeout 10s;
proxy_timeout 300s; # 设置客户端和代理服务之间的超时时间,如果5分钟内没操作将自动断开。
proxy_pass mysql_02;
}
# 代理Redis
upstream redis {
server 172.168.0.12:6379 max_fails=3 fail_timeout=30s; #redis替换为真实地址
}
server {
listen 8085; # 对外暴露端口号
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass redis;
}
# 代理Oracle
upstream oracle {
server 172.168.0.13:1521 weight=1 max_fails=2 fail_timeout=30s; #原oracle地址
}
server {
listen 3335;# 反向代理后监听的端口,
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass oracle;
}
}
http {}
测试TCP代理,链接mysql:
mysql -h127.0.0.1 -P3306 -u username -p'password'