database - TCP 代理到 postgres 数据库作为 nginx 中的上游服务器

标签 database postgresql nginx reverse-proxy

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

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-mail --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: invalid host in upstream "http://database_server" in /etc/nginx/conf.d/streams/database_server.conf:9

最佳答案

这是一个对我有用的 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。没有它,我可以连接,但我的连接会在几秒钟后重置。

关于database - TCP 代理到 postgres 数据库作为 nginx 中的上游服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545648/

相关文章:

ruby-on-rails - 如何在 digital ocean 上一键部署rails app?

android - 当应用程序处于离线状态时,如何保留本地数据库以将数据存储在 android 应用程序中?

ruby-on-rails - 我可以将 SPDY 与 Unicorn 一起使用吗?

python - ?在通过 Python 的 SQLite 查询中看不到

sql - 在 PostgreSQL 中创建嵌套 json blob

sql - 匹配值列表中的字符串并创建新行

postgresql - 是否可以短时间运行 VACUUM FULL 并获得一些好处?

nginx - Capistrano 3 无权查询此 Phusion Passenger 实例的状态

database - VS2010中 "Data Source Configuration Wizard"如何添加数据提供者

php - 如何显示 MySQL 数据库条目的摘录?