5

I want to create a simple chat. I am not a guru of server administration. So I have a question about ngnix and faye.

I use ngnix + passenger for my production server. I have a droplet on digitalocean and want deploy my application on this. So for deployment I use official passenger tutorial https://www.phusionpassenger.com/library/install/nginx/install/oss/trusty/

For model callbacks I use faye-rails gem. Like faye-rails say if I use passenger, I need use this configuration

config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25, server: 'passenger', engine: {type: Faye::Redis, host: 'localhost'} do
  map '/announce/**' => SomeController  
end

In my development localhost:3000 chat works perfectly fast. But when I deploy it, it works very slowly(the response comes in the interval of 5 to 60 seconds). I dont know how to fix it.

In my /etc/ngnix/sites-enabled/myapp.conf I use this config:

server {
    listen 80;
    server_name server_ip;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /project_path_to_public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /ruby_wrapper_path;

}

Need I upgrade my /etc/ngnix/sites-enabled/myapp.conf and how? Or what I need to do?

facetostool
  • 723
  • 1
  • 6
  • 12
  • Do you use a connection pool for redis? You may need one since passenger 5 is multithreaded – Vasfed Jan 15 '16 at 16:25
  • @Vasfed What do you mean about connection pool for redis? I use gem 'faye-redis' in my application with default config(I don't change it nowhere). And I have started redis-server with default settings too. Can you give me a link for tutorial or something else where I will understand how it should work. And I will understand why it is working so slowly now. – facetostool Jan 15 '16 at 21:16
  • Looked up - faye-redis uses em-hiredis internally and opens two connections, one for publish, other for subscribtions. But these are handled asynchronously so pool should not be needed unlike sidekiq setups – Vasfed Jan 15 '16 at 21:45
  • @Vasfed So what does it mean? Everything should work!? – facetostool Jan 16 '16 at 06:27

1 Answers1

0

I'm currently using Faye and Redis on an application I'm developing. This is not a direct solution to the question's current setup, but an alternative method that I have implemented. Below is my nginx configuration and then I have Faye running via rackup in a screen on the server.

/etc/nginx/sites-enabled/application.conf:

server {
     listen 80;
     listen [::]:80;
     server_name beta.application.org;

     # Tell Nginx and Passenger where your app's 'public' directory is
     root /var/www/application/current/public;

     # Turn on Passeger
     passenger_enabled on;
     passenger_ruby /usr/local/rvm/gems/ruby-2.2.1/wrappers/ruby;
     rails_env production;

     location ~* ^/assets/ {
          # Per RFC2616 - 1 year maximum expiry
          # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
          expires 1y;
          add_header Cache-Control public;
          add_header Last-Modified "";
          add_header ETag "";
          break;
     }

}

map $http_upgrade $connection_upgrade {
     default upgrade;
     '' close;
}

upstream websocket {
     server 127.0.0.1:9292;
}

server {
     listen 8020;
     location / {
          proxy_pass http://127.0.0.1:9292/push;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection $connection_upgrade;
     }

}

This link should provide a little insight into how it works. https://chrislea.com/2013/02/23/proxying-websockets-with-nginx/

You can also reference the Faye github for some guidance on setting it up with Passenger.

Also, if you followed the Digital Ocean tutorials for initial server setup and ended up enabling your firewall, please ensure you allow the ports you have Faye/websockets running on. (See here under configuring a basic firewall: Additional Recommended Steps for New Ubuntu 14.04 Servers

My alternative method involves running Faye in a separate screen on the server. A few commands you will need to manage screens on an ubuntu server are:

screen -S <pick screen name> (new screen)
screen -ls (lists screens)
screen -r <screen number> (attach screen)
to quit from a screen, ctrl + a THEN "d" (detach screen)

Once you have a new screen running, run the Faye server in that screen using rackup: rackup faye.ru -s thin -E production

As a note, with this option, every time you restart your Digital Ocean server (i.e. if you create a screenshot as a backup), you will need to create a new screen and run the faye server again; however, using something like Daemon would be a better implementation to circumvent this (I merely haven't implemented it yet...). Head over to Github and look for FooBarWidget/daemon_controller.

Let me know if you have any other questions and I'll try to help out!

  • This probably is not very relevant, since OP uses passenger for nginx with websockets on the same http port – Vasfed Jan 15 '16 at 15:42
  • @Vasfed - could you elaborate please? – Remain Zany Jan 15 '16 at 15:44
  • In your config you use standalone process for faye that is listening on port 9292 and is proxied to 8020, do not know how you start it, probably `rackup`, but it is not running inside passenger – Vasfed Jan 15 '16 at 15:48
  • @Vasfed - Ah, copy, yes, you are correct. We currently have it running in a screen via rackup. My apologies, do you think I should remove my answer? – Remain Zany Jan 15 '16 at 16:03
  • I think let it be, but edit to make clear that it is not the solution, but an alternative way. Actually one may even have nginx proxy `/faye` location on the main port with a setup like yours – Vasfed Jan 15 '16 at 16:28
  • @Vasfed - Edited. Thanks! – Remain Zany Jan 15 '16 at 16:32
  • @RemainZany Can you show me how you run faye in a screen via rackup? Do you use config.ru for this? – facetostool Jan 15 '16 at 21:32
  • @facetostool - I added some additional information that should help you out with running the Faye server via rackup in a new screen. You will need a faye.ru file, yes. Checkout the railscast 260 on messaging with Faye for some additional details on how to set that up! – Remain Zany Jan 17 '16 at 01:22