Nginx reverse proxy between multiple servers
Hello,
I installed rocket.chat in a local server (server A), and am trying to access it through a second server that is connected to the internet. In the second server, I have nginx installed and I am accessing rocket.chat from server A using 'upstream' option.
When I try to access rocket chat, I cannot manage to load the log in page. You can see it here - http://rnaseq.work.
Any idea about what is going on? Here is my nginx config in the internet-facing server.
`upstream backend { server 10.0.0.209; }
server { server_name rnaseq.work; location / { 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;
}
} `
Hello! I have same configuration and all works fine. Here is my config: Local server:
# Upstreams
upstream backend {
server 127.0.0.1:3000;
}
# HTTPS Server
server {
listen 80;
server_name chat.lan;
error_log /var/log/nginx/rocketchat.access.log;
# ssl on;
# ssl_certificate /etc/nginx/certificate.crt;
# ssl_certificate_key /etc/nginx/certificate.key;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # don’t use SSLv3 ref: POODLE
location / {
proxy_pass http://chat.lan:3000/;
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;
}
}
Server that can be avialable from internet:
server {
listen 80; ## listen for ipv4; this line is default and implied
server_name my.host.name.com www.my.host.name.com;
location / {
proxy_pass http://chat.lan:80;
access_log /var/log/nginx/my.host.name.com.access;
error_log /var/log/nginx/my.host.name.com.error error;
}
}
Also your server report about error in console:

Thanks vetash. Which browser are you using to see the syntax error in the last comment?
Here is my local server, where rocket.chat seems to load fine - http://rnaseq.work:21209/. However, I cannot make nginx in the internet server to reverse proxy that request - http://rnaseq.work.
I see the syntax error in my console, but not sure why it is coming. The meteor_js_resource file that rocket.chat file is pulling is the same for both local and internet-facing servers. If rocket.chat in the local server is loading fine ( http://rnaseq.work:21209/), why should the remote server give error in loading the meteor_js_resources javascript file? Are there special meteor configurations for reverse proxy that I am missing? Please help.
Can you post both nginx config here?
Did you modify any files? If not, it could be a Chrome(ium) extension interfering. Try disabling all extensions or opening the page incognito. I had a very similar issue just like this and it turned out to be due to an extension.
Best wishes, -k0nsl
K0nsl, no. I did not modify any file. I downloaded rocket.chat and followed standard instruction.
Also, it is failing in all browsers - chrome, firefox and IE. Chrome and firefox are both complaining about syntax error, but one with '}' and the other with ')'. I also tried an older version of rocket.chat (from Jan '16), but that did not help either.
Vetash, the config files are identical to what you posted above except for chat.lan being replaced by 10.0.0.209 (the internal IP address of the server). BTW, it is nice to meet someone from Kirensk, Russia. The place feels so remote, yet thanks to the internet, we can all share our knowledge about 'rocket.chat' !
@homologus Can you post debug log of both of your ngninx servers and rocket chat instance? PS: Yeah, earth is very big place to live and very nice to meet new nice people too :)
@homologus , I have noticed that when the nginx points to the local server it works fantastic, but if you trying to access an external server then the problems start.
Did you ever get this to work for you?
My issue is slightly different. I have
Upstreams
upstream backend { server drvm05.test.com:3001; server drvm05.test.com:3002; server drvm06.test.com:3001; server drvm06.test.com:3002;
My outcome is a little different. It takes almost 5 minutes before the browser brings up the login page. Once I'm logged in then it is very snappy. So I'm not sure if I have a nginx config issue. I was commenting to see if you were able to figure out the issue you were having... and that it may help me figure out what mine is. Thanks :-)
Nope, did not solve and moved on to something else. Sorry for late reply.
Looks like this one is no longer needed.
If you have any further questions please join our community server in the #support channel or ask your question on stackoverflow with the rocket.chat tag
I faced same issue. Workaround - replace
proxy_set_header X-Forward-Proto http;
with
proxy_set_header X-Forwarded-Proto https;
Rocket chat checks remote address and if it is not 127.0.0.1 and X-Forwarded-Proto doesn't contains https it will enter into infinite redirect loop. So to use nginx on remote server you should just pass X-Forwarded-Proto https
Proof:
packages/rocketchat_cors.js
143
144 const isLocal = localhostRegexp.test(remoteAddress) && (!req.headers['x-forwarded-for'] || _.all(req.headers['x-forwarded-for'].split(','), localhostTest));
145
146 const isSsl = req.connection.pair || req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'].indexOf('https') !== -1;
147
148 if (RocketChat && RocketChat.debugLevel === 'debug') {
149 console.log('req.url', req.url);
150 console.log('remoteAddress', remoteAddress);
151 console.log('isLocal', isLocal);
152 console.log('isSsl', isSsl);
153 console.log('req.headers', req.headers);
154 }
155
156 if (!isLocal && !isSsl) {
157 let host = req.headers['host'] || url.parse(Meteor.absoluteUrl()).hostname;
158 host = host.replace(/:\d+$/, '');
159 res.writeHead(302, {
160 'Location': `https://${host}${req.url}`
161 });
162 res.end();
163 return;
164 }
I don't understand why official manual offers to send X-Forward-Proto http instead of X-Forwarded-Proto
Please feel free to open PR to change docs