1

So I tried to set up a rails app using Bryan Bate's private_pub gem (a wrapper for the faye gem) to create chat channels. It works great on my local machine in dev mode.

I'm also booting up the private_pub server on port 8080 at the same time my rails app starts by including a initializer file with the lines:

Thread.new do
  system("rackup private_pub.ru -s thin -E production -p 8080")
end

however, after deploying to aws ec2 ubuntu instance with the nginx webserver and puma app sever, the chrome console keeps showing this every 2 seconds, and the real time chat features don't work.

GET http://localhost:8080/faye.js net::ERR_CONNECTION_REFUSED

If I open port 8080 in my aws security group, I can see the big chunk of javascript code in faye.js using curl from localhost:8080/faye.js when I ssh into the instance. I can also access it from my browser if I go to http://my.apps.public.ip:8080/faye.js. I can't access it if I remove 8080 from the security group, so I don't think this is an firewall problem.

Also, if I change the address from localhost to 0.0.0.0 or the public ip for my ec2 instance, the chrome console error is gone, but the real time chat is still not working.

I suspect I might have to do more configuration to nginx because all I have done so far to configure the nginx server is in /etc/nginx/sites-available/default, I have:

upstream app {
  server unix:/home/deploy/myappname/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 80;
  server_name localhost;

  root /home/deploy/myappname/public;

  try_files $uri/index.html $uri @app;

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_http_version 1.1;
    proxy_set_header Connection '';
    proxy_pass http://app;
  }

  location ~ ^/(assets|fonts|system)/|favicon.ico|robots.txt {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

But maybe this has nothing to do with nginx either, I'm pretty lost. Has anyone experienced this or could suggest an alternative solution? I could post any additional config files here if needed.

ChaiTea
  • 65
  • 1
  • 8
  • I don't have a full answer, but I can tell part of the issue is with your use of `server_name localhost;` in nginx. Chrome is pointing out that this is telling the client (you in this case) to connect to is using the name "localhost", and you are therefore getting the `GET http://localhost:8080/faye.js net::ERR_CONNECTION_REFUSED` because your browser is trying to connect to localhost:8080 (your own computer) which, since you're not running any server, is refusing the connection. See here for help on server_name: http://nginx.org/en/docs/http/server_names.html – Ian M Apr 21 '16 at 08:58
  • Thank you Ian, I think you are right. After you pointed this out I realized that the reason sometimes i see the error in chrome and other times I don't is because I sometimes also have the Faye server running on my local machine. I tested it by starting my main app without starting the Faye server locally and I see the same error. So now I just have to find the proper Nginx config for a Faye server. Thanks a lot! – ChaiTea Apr 21 '16 at 17:08
  • 1
    It looks like you could either set it as the IP of your AWS server (if it is static) or, if you felt crazy `_` (underscore) is a catch-all. Meaning any connection (to port 80) would be accepted. This is mostly used so one server can host multiple websites, and they are routed correctly. I also just noticed on startup you tell it to use port 8080, and your config only has 80 set, so there is that, too... – Ian M Apr 21 '16 at 19:37

1 Answers1

1

Solved

first I took Ian's advice and set server_name to my public ip

then

based on guide from http://www.johng.co.uk/2014/02/18/running-faye-on-port-80/

I added the location block

 location ^~ /faye {
    proxy_pass         http://127.0.0.1:9292/faye;
    proxy_redirect     off;
    proxy_set_header   Upgrade    $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_http_version 1.1;
    proxy_buffering    off;
    proxy_cache_bypass $http_pragma $http_authorization;
    proxy_no_cache     $http_pragma $http_authorization;
  }

finally, for my private_pub.yml I set the faye entry point for production:

production:
  server: "http://my.public.ip:80/faye/faye"
  secret_token: "mysecrettoken"
  signature_expiration: 3600 # one hour

and now the chatting in my app responds much faster than when I was using the remote standalone chat server I put on on heroku because both the chat server and my main app is running in the same instance.

ChaiTea
  • 65
  • 1
  • 8