实验环境
- nginx1.12.1 : 192.168.1.19
- MySQL : 192.168.1.20:3306
1: 编译或者升级 nginx 至版本 1.9.0 以上, 编译过程中需带上 --with-stream.
- # ./configure --prefix=/opt/apps/nginx --with-stream
- # make && make install
2: 配置 stream, 定义代理 192.168.1.20 的 3306 端口映射为端口 2333.
- # vi /opt/apps/nginx/conf/nginx.conf
- ......
- ......
- events {
- use epoll;
- worker_connections 65535;
- }
- #stream 配置
- stream {
- server {
- listen 2333;
- proxy_connect_timeout 1s;
- proxy_timeout 3s;
- proxy_pass 192.168.1.20:3306;
- }
- }
- http {
- ......
- ......
- }
3: 开启 nginx, 验证代理是否生效.
可以看到我们现在可以通过 nginx 代理的端口访问到内网的 MySQL 服务了. 这也直接避免了 MySQL 直接暴露到公网, 增加些许的安全. 当然, 利用 stream 也可以实现后端服务的负载均衡.
来源: http://www.bubuko.com/infodetail-3286882.html