nginx实现数据库端口转发
更新时间:2023年03月07日 10:06:11 作者:ourLang
本文主要介绍了nginx实现数据库端口转发,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
出于数据安全性考虑,正常情况下,网站或者项目的数据库一般都是禁止外网访问,或者只允许部分主机访问。那么,如何才能不修改这类权限的前提下,让其他被禁止访问的主机也能访问这个数据库呢。这时,Nginx的作用就体现出来了。
1、mysql为例
oracle、sqlserver等数据库配置和下面配置一样,只是数据库的端口不一样而已
需要注意的是这个配置要写在http外边
#使用nginx做数据库端口转发 stream { upstream sql { # 配置数据库的ip和端口 server 172.16.8.190:3306 weight=1 max_fails=2 fail_timeout=30s; } server { # 配置本机暴露端口 listen 925; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass sql; } }
2、完整配置如下
#user ?nobody;#配置用户或者用户组,默认为nobody worker_processes ?2;#允许生成的进程数,默认为1 #制定日志路径,级别。这个设置可以放入全局块,http块,server块, #级别以此为:debug|info|notice|warn|error|crit|alert|emerg #error_log ?logs/error.log; #error_log ?logs/error.log ?notice; #error_log ?logs/error.log ?info; #pid ? ? ? ?logs/nginx.pid;#指定nginx进程运行文件存放地址 events { ? ? worker_connections ?1024; ? ?#最大连接数,默认为512 ? ? accept_mutex on; ? #设置网路连接序列化,防止惊群现象发生,默认为on ? ? multi_accept on; ?#设置一个进程是否同时接受多个网络连接,默认为off ? ? #use epoll; ? ? ?#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport } stream { ? ? upstream sql { ?? ? ? ? ? server 172.16.8.190:3306 weight=1 max_fails=2 fail_timeout=30s; ?? ? ? } ? ? server { ? ? ? ?listen ? ? 925; ? ? ? ?proxy_connect_timeout 1s; ? ? ? ?proxy_timeout 3s; ? ? ? ?proxy_pass sql; ? ? } } http { ? ? include ? ? ? mime.types; ? ? default_type ?application/octet-stream; ? ? #log_format ?main ?'$remote_addr - $remote_user [$time_local] "$request" ' ? ? # ? ? ? ? ? ? ? ? ?'$status $body_bytes_sent "$http_referer" ' ? ? # ? ? ? ? ? ? ? ? ?'"$http_user_agent" "$http_x_forwarded_for"'; ? ? #access_log ?logs/access.log ?main; ? ? sendfile ? ? ? ?on; ? ? #tcp_nopush ? ? on; ? ? #keepalive_timeout ?0; ? ? keepalive_timeout ?65; ? ? #gzip ?on; ? ? #配置tomcat的IP地址和访问端口 ? ? upstream tomcat { ? ? ? ? server 172.16.8.190:8080; ?? ??? ? ? ? ?} ? ?? ? ? server { ? ? ? ? listen ? ? ? 9008; ? ? ? ? server_name ?172.16.8.190; ?? ?#header name含下划线 ?? ?underscores_in_headers on;? ?? ?#charset gbk; # 编码设置 ?? ?#开启gzip压缩 ? ? ? ? #gzip模块设置 ? ? ? ? gzip on; #开启gzip压缩输出 ? ? ? ? gzip_min_length 1k; #最小压缩文件大小 ? ? ? ? gzip_buffers 4 16k; #压缩缓冲区 ? ? ? ? gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0) ? ? ? ? gzip_comp_level 2; #压缩等级 ? ? ? ? gzip_types text/plain application/x-javascript text/css application/xml; ? ? ? ? #压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。 ? ? ? ? gzip_vary on; ? ? ? ? #charset koi8-r; ?? ? ? ?#charset utf-8,gbk; # 避免中文乱码 ? ? ? ? #root ? ?D:/htmlPage/dist;? ? ? ? ? #access_log ?logs/host.access.log ?main; ?? ?location /{ ?? ? ? ?#这个地方指定被访问的文件夹位置 ?? ??? ?root ? D:/htmlPage; ?? ??? ?index ?index.html index.htm; ?? ??? ?#limit_rate 1280k; #限制速度 ?? ? ? ?client_max_body_size ?100M; ?? ? ? ?allow all; ?? ??? ?autoindex on; ?? ? ? ?proxy_set_header X-Real-IP ?$remote_addr; ?? ? ? ?proxy_set_header Host $host; ?? ??? ?proxy_set_header X-Real-IP $remote_addr; ?? ??? ?proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ?? ? ? ?add_header 'Access-Control-Allow-Headers' 'Content-Type'; ?? ??? ?add_header 'Access-Control-Allow-Methods' 'GET'; ?? ? ? ?add_header 'Access-Control-Allow-Methods' 'POST'; ?? ? ? ?add_header 'Access-Control-Allow-Credentials' 'true'; ?? ? ? ?add_header 'Access-Control-Allow-Origin' '*'; ?? ? ? ?proxy_connect_timeout ? ? ? 600s; ?? ??? ?proxy_read_timeout ? ? ? ? ?600s; ?? ??? ?proxy_send_timeout ? ? ? ? ?600s;? ?? ??? ?access_log off; ?? ? ? ?break; ? ? ? ? } ? ? ?} ?}
到此这篇关于nginx实现数据库端口转发的文章就介绍到这了,更多相关nginx 数据库端口转发内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!
相关文章
uwsgi+nginx代理Django无法访问静态资源的解决
这篇文章主要介绍了uwsgi+nginx代理Django无法访问静态资源,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2021-05-05
最新评论