nginx - 通过 Nginx 连接 RabbitMQ

标签 nginx rabbitmq amqp

我正在尝试设置rabbitmq,它可以通过nginx从外部(从非本地主机)访问。

nginx-rabbitmq.conf:

server {
    listen       5672;
    server_name  x.x.x.x;
    location / {
        proxy_pass http://localhost:55672/;
    }
}

rabbitmq.conf:
[
 {rabbit,
  [
   {tcp_listeners, [{"127.0.0.1", 55672}]}
  ]
 }
]

默认情况下,guest 用户只能从 localhost 进行交互,因此我们需要创建另一个具有所需权限的用户,如下所示:
sudo rabbitmqctl add_user my_user my_password
sudo rabbitmqctl set_permissions my_user ".*" ".*" ".*"

但是,当我尝试通过 pika 连接到 rabbitmq 时,我得到 ConnectionClosed 异常
import pika
credentials = pika.credentials.PlainCredentials('my_username', 'my_password')
pika.BlockingConnection(
    pika.ConnectionParameters(host=ip_address, port=55672, credentials=credentials)
)

--[引发 ConnectionClosed 异常]--

如果我使用相同的参数,但将主机更改为 localhost 并将端口更改为 5672,那么我可以连接: pika.ConnectionParameters(host=ip_address, port=55672, credentials=credentials)
我在 GCE Web 控制台上打开了 5672 端口,并且正在通过 nginx 进行通信:nginx access.log 文件显示

[30/Apr/2014:22:59:41 +0000]“AMQP\x00\x00\x09\x01”400 172“-”“-”“-”

其中显示 400 状态代码响应(错误请求)。

所以看起来请求在通过 nginx 时失败,但是当我们直接请求 rabbitmq 时可以工作。

有没有其他人遇到过类似的问题/让 rabbitmq 通过 nginx 为外部用户工作?是否有一个 rabbitmq 日志文件,我可以在其中查看每个请求并帮助进一步排除故障?

最佳答案

由于 nginx 1.9 有 tcp 或 udp 的流模块(默认情况下未编译)。

我用 ssl 流配置了我的 nginx (1.13.3)

stream {
    upstream rabbitmq_backend {
        server rabbitmq.server:5672
    }

    server {
        listen      5671 ssl;

        ssl_protocols           TLSv1.2 TLSv1.1 TLSv1;
        ssl_ciphers             RC4:HIGH:!aNULL:!MD5;
        ssl_handshake_timeout   30s;

        ssl_certificate       /path/to.crt;
        ssl_certificate_key   /path/to.key;

        proxy_connect_timeout 1s;
        proxy_pass rabbitmq_backend;
    }
}

https://docs.nginx.com/nginx/admin-guide/security-controls/terminating-ssl-tcp/

关于nginx - 通过 Nginx 连接 RabbitMQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23399604/

相关文章:

java - 如何在具有多个队列的 RabbitMQ 中调用特定队列的特定方法?

RabbitMQ 二进制内存消耗

c++ - C/C++/Haskell AMQP 服务器库

nginx - 如何使用 nginx 运行 Go http 服务器

python - Flask/RabbitMQ : consume messages on a separate thread than the app

c++ - 可以使用 ActiveMQ CPP Api 与 RabbitMQ Broker 对话吗?

spring - Spring AMQP 中的发布者返回是什么

nginx 反向代理 - 如何为多个应用程序提供服务

javascript - nginx 服务位置与代理传递和位置与错误页面

ruby-on-rails - amazon EC2 linux 上的服务 unicorn 启动时出现意外 token `(' 附近的语法错误