ActiveMQ 和 NGINX

ActiveMQ and NGINX

我可以全神贯注...我们要求使用隐藏在 NGINX 代理后面的 ActiveMQ,但我不知道如何设置它。

对于 ActiveMQ,我为所有协议设置了不同的端口

<transportConnectors>
        <!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
        <transportConnector name="openwire" uri="tcp://0.0.0.0:62716?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="amqp" uri="amqp://0.0.0.0:5782?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="stomp" uri="stomp://0.0.0.0:62713?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1993?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        <transportConnector name="ws" uri="ws://0.0.0.0:62714?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
    </transportConnectors>

nginx 配置如下:

server {
  listen *:61616;
  server_name           192.168.210.15;

  index  index.html index.htm index.php;

  access_log            /var/log/nginx/k1.access.log combined;
  error_log             /var/log/nginx/k1.error.log;

  location / {
    proxy_pass            http://localhost:62716;
    proxy_read_timeout    90;
    proxy_connect_timeout 90;
    proxy_redirect        off;
    proxy_method          stream;
    proxy_set_header      Host $host;
    proxy_set_header      X-Real-IP $remote_addr;
    proxy_set_header      X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header      Proxy "";
  }
}

(所有其他五个重新定义的端口相同)

我虽然这会公开默认端口 ActiveMQ 端口并且 Nginx 会将其映射到新定义,但这不起作用。

对于通信,我们使用版本 3.1.4 中的 NodeJs 库 amqp10。

并且服务器上的所有端口都已启用...如果使用没有 nginx 代理的标准端口,它可以工作。

有人知道我错过了什么吗?感谢您的任何想法。

Nginx 是一个能够代理 WebSocket 和 HTTP 的 HTTP 服务器。

但是您正在尝试为 AMQP 客户端代理 OpenWire。这不适用于 Nginx 或 Node.js.

所以 - 如果你真的需要使用 Nginx,你需要通过 WebSocket 将客户端协议更改为 STOMP 或 MQTT。然后在 Nginx 中设置 WebSocket 代理。

带 TLS 的 Nginx 示例。更多详情请见 https://www.nginx.com/blog/websocket-nginx/

upstream websocket {
    server amqserver.example.com:62714;
}
server {
    listen 8883 ssl;
    ssl on;
    ssl_certificate /etc/nginx/ssl/certificate.cer;
    ssl_certificate_key /etc/nginx/ssl/key.key;

    location / {
        proxy_pass http://websocket;
        proxy_http_version 1.1;
        proxy_set_header Upgrade websocket;
        proxy_set_header Connection upgrade;
        proxy_read_timeout 120s;
    }
}

但是,既然你要重写所有的客户端代码,我会重新考虑Nginx的想法。还有其他软件和硬件可以前端基于 TCP 的服务器并执行 TLS 终止等。

即使您尝试为 AMQP 客户端代理 OpenWire,您也可以将 ActiveMq 隐藏在 nginx 代理之后。

如果您在 http 块中添加配置,它肯定会失败。

不过明白了,nginx不仅支持http,还支持tcp block

如果您通过 tcp 代理 activemq,那么在 http 级别发生的事情将无关紧要,您仍然可以代理。

当然,您将失去 http 附带的灵活性。

打开您的 nginx.conf(位于 /etc/nginx/nginx.conf)。 这会有 http 块,而 http 块又会有一些 include 语句。

在此 http 块之外,添加另一个 include 语句。

$ pwd
/etc/nginx
$ cat nginx.conf | tail -1
include /etc/nginx/tcpconf.d/*;

include 语句指示 nginx 在目录“/etc/nginx/tcpconf.d/”中查找其他配置。 在此目录中添加所需的配置。我们称它为 amq_stream.conf.

$ pwd
/etc/nginx/tcpconf.d
$ cat amq_stream.conf
stream {
    upstream amq_server {
        # activemq server
        server <amq-server-ip>:<port like 61616.;
    }

    server {
        listen 61616;
        proxy_pass amq_server;
    }
}

重启你的 nginx 服务。

$ sudo service nginx restart

你完成了