一、 目标
通过Nginx服务器ip及端口,能访问到数据库服务器(哪种数据库关系不大,改ip和端口即可)。在应用迁移时将应用连接改到Nginx做中转,或者作为vip,都比较好用。
二、 nginx安装
下载地址
http://nginx.org/en/download.html
新建组和用户
groupadd -g 1004 nginx
useradd -g nginx nginx
解压安装包
tar xvf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
安装依赖包
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
编译
根据实际需要的模块装即可,这里偷懒把大部分都装上了
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module
安装
make && make install
修改环境变量,添加nginx命令路径
vi .bash_profile
#修改内容
export PATH=$PATH:$HOME/bin:/usr/local/nginx/sbin
source .bash_profile
三、 配置代理Oracle数据库
cd /usr/local/nginx/conf
vim nginx.conf
在配置文件的 events 和 http 模块之间加上一段:其中server 192.168.2.107:1521 是被代理的数据库和端口;listen 666; 是代理的端口。
比如nginx服务器ip是192.168.2.220,后面通过连 192.168.2.220:666 就相当于连到了192.168.2.107:1521
stream {
log_format logstash_json '{ "@timestamp": "$time_iso8601", '
'"@fields": { '
'"remote_addr": "$remote_addr", '
'"remote_port": "$remote_port", '
'"upstream_addr":"$upstream_addr", '
'"bytes_sent": "$bytes_sent", '
'"status": "$status" } }';
upstream oracle {
server 192.168.2.107:1521 max_fails=3 fail_timeout=30s;
}
server {
listen 666;
proxy_connect_timeout 300s;
proxy_timeout 300s;
proxy_pass oracle;
}
}
检查配置文件语法
nginx -t
应该输出
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动nginx(正常启动没有信息返回)
nginx
ps -ef | grep nginx
关闭命令为
nginx -s stop
查看代理的端口是否启动
ss -ntl | grep 666
正常启动后就可以通过代理ip和端口连接了~
查看是否已有连接(好像active session才能看到)
ss -nt | grep 1601
停止Nginx(影响现有连接)
nginx -s stop
reload配置文件(不影响现有连接)
nginx -s reload