因为MySQL端口为非标准端口启动,办公网络无法直接连接,只能通过Nginx端口来访问数据库。那么如何使用Nginx端口来访问呢?
1.Nginx编译
上一次分享的Nginx编译安装教程:CentOS7编译安装Nginx-1.20.1
安装的过程已经跟大家讲解了如何编译安装,但是不支持stream模式,为此,我们需要增加编译选项:
$ cd nginx-1.20.1/
$ ./configure --prefix=/opt/testerzhang/nginx --with-http_stub_status_module --with-http_ssl_module --with-stream --with-stream_ssl_module
关键参数
-with-stream --with-stream_ssl_module
- 编译安装
$ make
$ make install
这时候我们就可以在安装目录查看到编译好的文件了。
- 删掉安装目录的html文件夹下的文件
$ cd /opt/testerzhang/nginx/html
$ rm *.html
2.打包编译好的Nginx目录
这里打包最原始的配置,当然如果你要提前修改Nginx配置文件,再打包也是可以的。
$ cd ~/
$ tar zcf nginx.tar.gz nginx
然后你就可以拷贝到你想要部署的机器。
3.新机器部署
- 在你想要部署的目录,解压压缩包
$ tar zxf nginx.tar.gz
$ cd nginx
- 修改配置文件
$ vim conf/nginx.conf
下面是例子:
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /opt/testerzhang/nginx/logs/tcp-access.log proxy ;
open_log_file_cache off;
upstream mysql3706 {
hash $remote_addr consistent;
server 127.0.0.1:3706 weight=5 max_fails=3 fail_timeout=30s;
}
server {
listen 1521;
allow 10.10.10.10;
deny all;
proxy_connect_timeout 10s;
proxy_timeout 200s;
proxy_pass mysql3706;
}
}
# 这里贴了http配置节,是为了更好的表明层级关系。
http {
...
}
上面的配置:
- 对外暴露1521端口,反代内网的3706端口
- 只允许10.10.10.10的IP访问1521端口,其他端口不允许访问。
- 如果有使用防火墙和控制台规则,请增加放行1521端口。
4.启停命令
正常在编译后的安装目录,不需要指定配置文件,但是由于是部署新机器,可能不是同一个账号,你需要加上绝对路径的配置文件
- 启动
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf
- 停止
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s stop
- 重新加载Nginx配置
$ ./nginx -c /opt/testerzhang/nginx/conf/nginx.conf -s reload
本文没有授权给任何组织、企业和个人转载,未经作者允许禁止转载!
欢迎关注我的公众号testerzhang,原创技术文章第一时间推送。