系统城装机大师 - 固镇县祥瑞电脑科技销售部宣传站!

当前位置:首页 > server > anz > 详细页面

nginx实现数据库端口转发

时间:2023-03-09来源:系统城装机大师作者:佚名

  • 1、mysql为例
  • 2、完整配置如下

出于数据安全性考虑,正常情况下,网站或者项目的数据库一般都是禁止外网访问,或者只允许部分主机访问。那么,如何才能不修改这类权限的前提下,让其他被禁止访问的主机也能访问这个数据库呢。这时,Nginx的作用就体现出来了。

1、mysql为例

oracle、sqlserver等数据库配置和下面配置一样,只是数据库的端口不一样而已
需要注意的是这个配置要写在http外边

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#使用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、完整配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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实现数据库端口转发的文章就介绍到这了

分享到:

相关信息

系统教程栏目

栏目热门教程

人气教程排行

站长推荐

热门系统下载