nginx 代理postgresql

首先,Nginx为我们的数据库增加了额外的安全层。Nginx提供了一整套的选项,这使得管理访问和保护数据库变得很容易。例如,我们可以配置为只有一小部分IP地址可以访问数据库。

PostgreSQL不使用HTTP或HTTPS,而是使用一个新块儿stream。

  1. stream
    该指令定义了stream服务器。与http块平级,定义在main块中。
bash 复制代码
 stream {
     server {
         ......
     }
 }
  1. server
    该指令定义一个虚拟主机,与http块中的server类似。我们可以在stream块中定义多个server块。
bash 复制代码
stream {
     server {
         ......
     }
     server {
         ......
     }
 }
  1. listen
    该指令定义虚拟主机server要监听的socket的地址和端口。
  2. allow配置允许访问的IP
    作用域:stream, server
bash 复制代码
# 允许192.168.110.1访问
 allow 192.168.110.1;
 
 # 允许192.168.110.1到192.168.255.254
 allow 192.168.110.0/16;
 
 # 允许192.168.110.1到192.168.110.254
 allow 192.168.110.0/24;
 
 # 允许所有的IP访问
 allow all;
  1. deny 配置不可访问ip
    作用域:stream, server
bash 复制代码
# 禁止192.168.110.1访问
 deny 192.168.110.1;
 
 # 禁止192.168.110.1到192.168.255.254
 deny 192.168.110.0/16;
 
 # 禁止192.168.110.1到192.168.110.254
 deny 192.168.110.0/24;
 
 # 禁止所有的IP访问
 deny all;
  1. 配置实例
bash 复制代码
stream {
    upstream pgsql_socket {
         server 192.168.214.133:32222;
     }
    server {
      listen 9856;
      allow 192.168.214.134;
      deny all;
      proxy_connect_timeout 60s;
      proxy_socket_keepalive on;
      proxy_pass pgsql_socket;#192.168.214.133:32222;
    }
}

https://docs.nginx.com/nginx/admin-guide/load-balancer/tcp-udp-load-balancer/

https://wasi0013.com/2021/11/15/setup-nginx-reverse-proxy-to-access-postgresql-database-remotely/

https://iamgideon.medium.com/configure-a-reverse-proxy-for-postgresql-with-nginx-63c18cefe09

相关推荐
core5125 小时前
prometheus+grafana接入nginx实战
nginx·grafana·prometheus·监控·接入·vts·vtx
__风__9 小时前
PostgreSQL ExecInitIndexScan 函数解析
数据库·postgresql
中文很快乐18 小时前
postgreSQL的sql语句
数据库·sql·postgresql
····懂···20 小时前
如何成为 PostgreSQL 中级专家
数据库·postgresql
dingdingfish21 小时前
PostgreSQL 16 Administration Cookbook 读书笔记:第6章 Security
postgresql·database·security·administration·cookbook
小至尖尖1 天前
FastCDC 项目启动玩玩 😁😁😁😁
postgresql·sql优化
xian_wwq1 天前
【学习笔记】Nginx常用安全配置
笔记·学习·nginx
不知疲倦的仄仄2 天前
2025Nginx最新版讲解/面试
nginx·代理模式·proxy模式
不要图透2 天前
初用nginx
nginx
启明真纳2 天前
[特殊字符]使用 Nginx 将 HTTP 重定向到 HTTPS
nginx·http·https