Rocket Chat instances behind Apache2.4 proxy
Your Rocket.Chat version: 0.37.1 Apache version: 2.4.6
Hi everyone.
After a couple tests, we got our management approval to work on production setup of Rocket Chat and we've been struggling out to deliver something close to a high available and performatic environment since we already have a few hundred users in our old XMPP service. We were also asked about providing access from the Internet to this new chat, so this is how the mess begins.
Currently, we have an internet-exposed Apache which reverse proxies to all our internal apps which need to be accessed from the Internet, so as a security rule, I can't simply NAT another IP to Nginx as Rocket Chat's docs suggest to.
Since we also want performance, I was able to setup two Rocket Chat instances on the same backend server, but there was no docs about balancing them under Apache (just using Nginx). I tried many many approachs under Apache, using BalancerMember settings, but it didn't work. So I followed the Nginx instructions under "Running Multiple Instances Per Host To Improve Performance" and setup another Apache virtual host config to reverse proxy to Nginx.
This is the architecture I have: [APACHE]----reverse proxies im.mydomain.com---->([NGINX]-----load balances----[RC1]/[RC2])
This is Nginx (placed in the same backend server as RocketChat) config I'm using:
upstream backend {
server localhost:3001 weight=1;
server localhost:3002 weight=1;
}
server {
listen 443 default_server;
server_name im.mydomain.com;
root /opt/Rocket.Chat/programs/web.browser/app/;
ssl on;
ssl_certificate /etc/nginx/certificate.crt;
ssl_certificate_key /etc/nginx/certificate.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # dont use SSLv3 ref: POODLE
client_max_body_size 32m;
proxy_buffering off;
proxy_http_version 1.1;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_set_header Host $host:$server_port;
proxy_set_header Referer $http_referer;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Nginx-Proxy true;
proxy_redirect off;
location / {
proxy_pass http://backend;
}
}
This is the Apache virtual host I'm trying to work on:
<VirtualHost *:443>
ServerName im.mydomain.com
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/mydomain.crt
SSLCertificateKeyFile /etc/pki/tls/private/mydomain.key
SSLCertificateChainFile /etc/pki/tls/certs/bundle.crt
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder On
SSLCipherSuite ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ProxyRequests off
ProxyPreserveHost on
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerName off
SSLProxyCheckPeerCN off
ErrorLog logs/im_error_log
TransferLog logs/im_access_log
LogLevel warn
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* wss://backend.mydomain.com:443%{REQUEST_URI} [P]
<Location "/">
ProxyPass https://backend.mydomain.com/
ProxyPassReverse https://backend.mydomain.com/
Require all granted
</Location>
</VirtualHost>
This configuration almost works, except for the fact it's intermitent. Sometimes it will just take a couple seconds to load Rocket Chat (and 101s 'GET websocket'), sometimes it will loop a "Connecting..." on top and taking ages to 101 a 'GET websocket'. STILL, it doesn't fully work in both situations, because I keep receiving 404s on every 'POST xhr_send' request.
This is one of the POST xhr_send requests that 404:
Request URL - POST https://im.mydomain.com/sockjs/746/n0_loc3c/xhr_send
Request Headers - Host: im.mydomain.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate, br
Referer: https://im.mydomain.com/home
Content-Length: 81
Content-Type: text/plain;charset=UTF-8
Connection: keep-alive
Response Headers - Access-Control-Allow-Origin: *
Cache-Control: no-store, no-cache, must-revalidate, max-age=0
Connection: Keep-Alive
Content-Type: text/plain; charset=UTF-8
Date: Fri, 26 Aug 2016 18:13:27 GMT
Keep-Alive: timeout=5, max=97
Server: nginx/1.6.3
Transfer-Encoding: chunked
Vary: Origin
However and as supposed to, connecting directly to Nginx works like a charm, so I guess this is some sticky session issue anywhere between Apache and Nginx, but I need help to progress here.
Thank you and sorry for any typos/grammar.
UPDATE: Ok, so I guess I just made it work using the above setup. The problems happens only to me using latest Firefox. Opening on Chrome/IE runs fine.
OK, this configuration works, looked like it was really a problem on my desktop.
@MartinSchoeler feel free to update wiki if this helps. Thank you!
Hi @bhbarbosa
have you faced any problems with apache so far? or is it just working fine? and what about video calls through jitsi and screen sharing is it also working fine?
I want to ask about the configuration you posted above, you have been using im.mydomain.com but then in the location section you used the url backend.mydomain.com, can you please explain?
Hi @MosabWadea
Haven't had any issues with this setup so far, though we are not delivering any RTC features such as video and audio calls neither screen sharing.
For my Apache reverse proxy server (front end), im.mydomain.com is a SSL virtual host which proxies its requests to backend.mydomain.com, which is the application server where both NGINX and my two Rocket Chat instances reside.