編碼的世界 / 優質文選 / 歷史

Nginx反向代理mysql 千次閱讀


2022年5月15日
-   

1、場景


mysql數據庫在純內網環境,無公網ip,無VPN。

2、方案


在一台具備公網ip,並且能與mysql服務在同一內網環境內的服務器,安裝nginx,實現對mysql訪問的路由轉發。

3、nginx安裝


nginx版本需要1.9及以上, nginx既實現http的反向代理,也支持TCP的反向代理。
1)nginx編譯時,需要加入with-stream這個參數,以加載ngx_stream_core_module模塊
示例
./configure prefix=/opt/software/nginx with-http_stub_status_module with-http_ssl_module with-stream with-stream_ssl_module with-pcre=/usr/local/src/pcre-8.35

4、nginx配置文件nginx.conf


監聽具備公網ip服務器的3307端口,實現跳轉到172.31.88.27的3306端口。
特別注意:stream要與http在同級目錄  
stream {
upstream mysql3306 {
hash $remote_addr consistent;
server 172.31.88.27:3306 weight=5 max_fails=3 fail_timeout=30s;
}

server {
listen 3307;
proxy_connect_timeout 10s;
proxy_timeout 200s;
proxy_pass mysql3306;
}
}

 

熱門文章