【问题标题】:TCP proxy to postgres database as an upstream server in nginxTCP 代理到 postgres 数据库作为 nginx 中的上游服务器
【发布时间】:2016-06-03 09:46:53
【问题描述】:

问题:是否可以将 Nginx 设置为数据库的反向代理? 这些是我目前拥有的标志,我相信拥有 --with-stream 模块足以使用 TCP 流到数据库。这是 PLUS 功能吗?

Nginx 配置选项:

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=%{_libdir}/nginx/modules --conf-path=/etc/nginx/nginx. conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx。 pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/ nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/ var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with -http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with -http_slice_module --with-邮件 --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE =2' --with-ld-opt='-Wl,-z,relro -Wl,--as-needed' --with-ipv6

Nginx 配置

stream {

    include conf.d/streams/*.conf;
}

conf.d/streams/upstream.conf的内容

upstream database_server {
    least_conn;
    keepalive 512;
    server 192.168.99.103:32778 max_fails=5 fail_timeout=30s weight=1;

}

来自 Nginx 的错误消息

2016/02/22 03:54:13 [emerg] 242#242:/etc/nginx/conf.d/streams/database_server.conf:9 中上游“http://database_server”中的主机无效

【问题讨论】:

    标签: database postgresql nginx reverse-proxy


    【解决方案1】:

    问题是“http://database_server” 它是一个 tcp 流,所以你只需要 proxy_pass database_server

    也保持活动不是进入 tcp 上游服务器的指令

    【讨论】:

      【解决方案2】:

      这是一个对我有用的 nginx 配置(我正在运行 inside Docker,所以其中一些选项可以帮助解决这个问题):

      worker_processes auto;
      
      daemon off;
      
      error_log stderr info;
      
      events {
          worker_connections 1024;
      }
      
      stream {
          upstream postgres {
              server my_postgres:5432;
          }
      
          server {
              listen 5432 so_keepalive=on;
              proxy_pass postgres;
          }
      }
      

      对我来说,关键是listen 5432 so_keepalive=on; 行,它开启了 TCP keepalive。没有它,我可以连接,但我的连接会在几秒钟后重置。

      【讨论】:

      • 我想my_postgres:5432 代表名称为my_postgres 的docker 容器,对吗?我也在为我的 docker 东西尝试这个。
      • 这里,my_postgres 是解析为侦听端口 5432 的服务器的名称。如果您有 docker-compose 配置,您可以将 Postgres 容器命名为 my_postgres,然后 Docker 会设置为您解析名称。但你可能不在 Docker 之外,只使用 DNS。
      • 对我来说,尝试使用此配置从客户端连接到 postgres 时连接超时。
      • @BielSimon 如果您在连接时超时,那么您的 Nginx 将无法访问您的 Postgres。可能其中一个或两个都配置错误(可能 Postgres 没有在正确的接口上侦听,可能 Nginx 使用了错误的端口等)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      • 2012-10-08
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 2015-06-06
      • 1970-01-01
      相关资源
      最近更新 更多