Nginx配置数据库服务器反向代理

60 篇文章 13 订阅
9 篇文章 1 订阅

一、 目标

通过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

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hehuyi_In

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值