Nginx tcp连接反向代理配置

时间:2022-07-28
本文章向大家介绍Nginx tcp连接反向代理配置,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

安装Nginx

Nginx需要安装stream模块 
stream模块可以代理mysql、FTP等tcp连接

安装依赖

yum install -y wget gcc gcc-c++ make pcre pcre-deve zilib zlib-devel openssl-devel

下载Nginx

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar zxvf nginx-1.18.0.tar.gz

编译安装Ngixn

cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-stream --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install

配置反代理

配置配置文件

--配置反代理Mysql
vim /usr/local/nginx/conf.d/default.conf   
events {
  worker_connections  1024;
}
stream {   
upstream mysql{
   server 172.17.0.3:3306;  
}
server {
   listen 8080;
   proxy_pass mysql;
}
}

启动Nginx

--指定配置文件启动
cd /usr/local/nginx
/usr/local/nginx/sbin/nginx -c conf.d/default.conf 

验证反代理是否成功

mysql -uroot -p123456 -h172.16.0.14 -P8080
参数解释:
-u    指定Mysql用户名
-p    指定Mysql密码
-h    指定Nginx代理服务器IP
-P    指定端口
root@linux:/usr/local/nginx# mysql -uroot -p123456 -h172.16.0.14 -P8080
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MySQL connection id is 11
Server version: 5.6.49 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MySQL [(none)]>