nginx -stream(tcp連接)反向代理配置 實現代理mysql以及文件上傳


原文鏈接:https://blog.csdn.net/Activity_Time/article/details/95767390

1. stream模塊安裝

nginx默認安裝的時候無法加載流stream模塊,需要在啟動參數里加上–with-stream。

解決方法:
重新對源文件進行編譯、安裝,通過添加–with-stream參數指定安裝stream模塊。
nginx安裝教程

[root@localhost nginx-1.12.2]# ./configure --with-stream
[root@localhost nginx-1.12.2]# make & make install

configure命令設定安裝參數

# 常用模塊配置
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-stream  \
--with-http_ssl_module

2.配置文件 nginx.conf

stream模塊位於與http模塊相同的層次
示例:

stream {
    upstream backend {
        server 202.118.163.18:3306;
    }
 
    upstream landm {
        server 202.118.163.18:8092;
    }
 
    upstream nginxCon {
        server 202.118.166.247:22;
    }
 
    upstream stu.neau.actim.top {
        server 202.118.163.18:80;
        server 202.118.167.86:9004;
        server 202.118.167.86:9003;
        server 202.118.167.86:9002;
        server 202.118.167.86:9001;
     }
 
    server {
        listen 8088;
        proxy_connect_timeout 8s;
        proxy_timeout 24h;   #¿¿¿¿
        proxy_pass backend;
    }
 
    server {
        listen 8082;
        proxy_connect_timeout 8s;
        proxy_timeout 24h;   #¿¿¿¿
        proxy_pass landm;
    }
 
    server {
        listen 8686;
        proxy_connect_timeout 8s;
        proxy_timeout 24h;   #¿¿¿¿
        proxy_pass nginxCon;
    }
 
    server {
        listen 8093;
        proxy_connect_timeout 8s;
        proxy_timeout 24h;   #¿¿¿¿
        proxy_pass stu.neau.actim.top;
    }
}

3. 代理mysql、文件下載等tcp連接

stream在使用上與http相近,只需配置upstream然后再配置server即可,參考上面的配置,
代理mysql只需用反向代理服務器的一個端口去代理數據庫服務器(集群)的端口即可,文件下載亦然


參考文章:
Nginx1.14.2新增tcp/udp代理stream(詳細)

nginx代理tcp協議連接mysql

ssl模塊安裝(https)
Nginx反向代理+負載均衡簡單實現(https方式)
https://blog.csdn.net/Activity_Time/article/details/95770900


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM