Rocket.Chat icon indicating copy to clipboard operation
Rocket.Chat copied to clipboard

reverse proxy as described not working properly

Open bbrauns opened this issue 9 years ago • 6 comments

Rocket.Chat Version: 0.52.0
Running Instances: 2
DB Replicaset OpLog: Activated
Node Version: v4.8.0

Hi everybody,

The load balancing method as described in the documentation https://rocket.chat/docs/installation/manual-installation/configuring-ssl-reverse-proxy#running-behind-a-nginx-ssl-reverse-proxy is not working properly regarding my setup. I have a VM with rocket chat in a docker-compose setup.

When I deploy two backend docker app container (in the configs named as rocketchat_app_1, rocketchat_app_2) and let nginx proxy the requests to these two apps the notification feature as well as the direct phone call in 1on1 chats does not work. Immediately as I disable one backend everything works fine.

I think it has something to do with websocket session and one partner being connected to rocketchat_app_1 and the other one connected to rocketchat_app_2.

Thx in advance for any help or suggestions.

root@chat:~/rocketchat# docker ps
CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                                                                NAMES
7f1c25f97a8f        nginx:1.11                           "nginx -g 'daemon off"   2 hours ago         Up 2 hours          80/tcp, 443/tcp, 0.0.0.0:80->8080/tcp, 0.0.0.0:81->8081/tcp, 0.0.0.0:443->8443/tcp   rocketchat_nginx_1
942cb124ce38        rocket.chat:0.52                     "node main.js"           20 hours ago        Up 21 minutes       3000/tcp                                                                             rocketchat_app_1
faa731c56d9f        rocket.chat:0.52                     "node main.js"           20 hours ago        Up 21 minutes       3000/tcp                                                                             rocketchat_app_2
e36867bc71e4        mongo:3.4                            "/entrypoint.sh mongo"   2 weeks ago         Up 9 hours          127.0.0.1:27017->27017/tcp       
root@chat:~/rocketchat# cat docker-compose.yml
version: "2"
services:
  nginx:
    image: nginx:1.11
    restart: always
    ports:
      - 80:8080
      - 81:8081
      - 443:8443
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./rocketchat.conf:/etc/nginx/conf.d/rocketchat.conf:ro
      - ./certs/cert.pem:/etc/nginx/certificate.crt:ro
      - ./certs/cert.key:/etc/nginx/certificate.key:ro

  app:
    image: rocket.chat:0.52
    restart: always
    volumes:
      - /data/upload:/data/upload
    environment:
      - PORT=3000
      - ROOT_URL=http://chat.gwdg.de
      - MONGO_URL=mongodb://mongo:27017/rocketchat
      - MONGO_OPLOG_URL=mongodb://mongo:27017/local
      - MAIL_URL=smtp://mailer.gwdg.de
    depends_on:
      - mongo

  mongo:
    image: mongo:3.4
    restart: always
    ports:
      - 127.0.0.1:27017:27017
    volumes:
      - /data/db:/data/db
    command: mongod --smallfiles --oplogSize 128 --replSet rs0

  mongo-init-replica:
    image: mongo:3.2
    command: 'mongo mongo/rocketchat --eval "rs.initiate({ _id: ''rs0'', members: [ { _id: 0, host: ''localhost:27017'' } ]})"'
    depends_on:
      - mongo
root@chat:~/rocketchat# cat nginx.conf
user       nginx;
worker_processes  5;  ## Default: 1
error_log  stderr warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 8192;

events {
  worker_connections  4096;  ## Default: 1024
}

http {
  #include    conf/mime.types;
  index    index.html index.htm index.php;

  default_type application/octet-stream;
  log_format   main '$remote_addr - $remote_user [$time_local]  $status '
    '"$request" $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /dev/stdout main;

  sendfile     on;
  tcp_nopush   on;
  server_names_hash_bucket_size 128; # this seems to be required for some vhosts

  include /etc/nginx/conf.d/*.conf;
}
root@chat:~/rocketchat# cat rocketchat.conf
upstream backend {
    server rocketchat_app_1:3000 max_fails=3 fail_timeout=10s;
    server rocketchat_app_2:3000 max_fails=3 fail_timeout=10s;
}

server {
       listen         8080;
       server_name    chat.gwdg.de;
       return         301 https://$server_name$request_uri;
}


server {
    listen 8081;
    server_name chat.gwdg.de;
    location /nginx_status {
        stub_status on;

        access_log off;
        # allow 127.0.0.1;
        # deny all;
    }
}

server {
    listen 8443;
    server_name chat.gwdg.de;

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

    ssl on;
    ssl_certificate /etc/nginx/certificate.crt;
    ssl_certificate_key /etc/nginx/certificate.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    location / {
        client_max_body_size 32m;
        proxy_pass http://backend/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forward-Proto http;
        proxy_set_header X-Nginx-Proxy true;

        proxy_redirect off;
    }
}

bbrauns avatar Mar 13 '17 10:03 bbrauns

Does anybody have any experience running multiple instances of Rocketchat behind a load balancing setup and can confirm or disprove the observed effects? We are quite curious about it because long term, for any meaningful large setup, this is important. And while basic chat functionality works there is limitations as described above which users are likely to run into.

benedikt-wegmann avatar Mar 15 '17 09:03 benedikt-wegmann

I'm running similar setup. Except that my instances are on the same host (not in docker containers). During startup instances finds each other. OpLog must be enabled and working for proper notifications and good performance.

tarabanton avatar Apr 12 '17 14:04 tarabanton

I'm trying to remember the specifics, but we had multiple instances running (4 at the time) and were having inconsistent behavior w.r.t. notifications until we set INSTANCE_IP. Similar issues seem to be:

  • https://github.com/RocketChat/Rocket.Chat/issues/4019
  • https://github.com/RocketChat/Rocket.Chat/issues/4085
  • https://github.com/RocketChat/Rocket.Chat/issues/4299

Have you tried setting INSTANCE_IP?

jasoncscott avatar Jul 04 '17 13:07 jasoncscott

It seems that SSL is not fully supported for the inter-connection. Try it over HTTP or with a Stunnel, maybe it is also working for you.

N4IR0 avatar Jul 11 '17 06:07 N4IR0

inorder to run two instances of rocket chat on two VMs (web1 and web2) and with a loadbalancer infront of them (haproxy1), do I need to take care of setting up a shared storage for the session info. I have a seperate mongo DB replica setup with another three Vms: so if the session info stored in mongo; I dont need any shared storage I suppopse; please suggest me how I should move from this point

srihas619 avatar Aug 09 '17 09:08 srihas619

Solution here is definitely setting INSTANCE_IP

Needed to communicate and deliver notifications between instances

geekgonecrazy avatar Mar 29 '18 06:03 geekgonecrazy