ruby-on-railsredisvagrantansibleredis-rails

Vagrant+Ansible+Redis - Get 127.0.0.1:6379 (Errno::ECONNREFUSED) when using different servers


Hope everything is well. I have the following problem and was hoping you could help me out:

I'm trying to deploy my rails 4 app using vagrant and ansible. Part of the app is a redis server. I have this set up and running at a 192.168.33.2:6379, my rails app is running at 192.168.33.4 and running on ubuntu + nginx.

Even-though I have redis set up with redis-rb (gem in app) and initialize a redis object in one of my initializers with app global vars initiated from a YAML file:

$redis = Redis.new(:driver => :hiredis, :host => APP_CONFIG[:redis_host], :port => APP_CONFIG[:redis_port])

with APP_CONFIG[:redis_host] = 192.168.33.2, APP_CONFIG[:redis_port] = 6379

and bind 0.0.0.0 in my redis.conf file, I still receive the error Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) when I go to the rails app in my browser.

Running redis-cli -h 192.168.33.2 ping when vagrant ssh-ed into 192.168.33.4 returns pong. So connections can be made and I can reach the redis server / see the keys from 192.168.33.4...

Also running netstat -l in the terminal gives me: tcp 00 *:6379 *:* LISTEN

Does anyone know how I can fix this problem?

Thanks in advance!

Update

Note: I make use of the redis-rails gem and also use it in my app for the session_store & cache_store. (when not using redis it works). Configured in this way:

#config/initializers/session_store.rb
MyApp::Application.config.session_store :redis_store, { 
            :host => APP_CONFIG[:redis_host],
             :port => APP_CONFIG[:redis_port],
             :db => APP_CONFIG[:redis_db],
             # :password => "mysecret",
             :namespace => "_myapp_session"}, :expires_in => 90.minutes

#config/application.rb
config.cache_store = :redis_store,"redis://192.168.33.2:6379/12/api_cache", { expires_in: 3.minutes }

With trace:

  redis (3.2.1) lib/redis/client.rb:331:in `rescue in establish_connection'
redis (3.2.1) lib/redis/client.rb:317:in `establish_connection'
redis (3.2.1) lib/redis/client.rb:94:in `block in connect'
redis (3.2.1) lib/redis/client.rb:279:in `with_reconnect'
redis (3.2.1) lib/redis/client.rb:93:in `connect'
redis (3.2.1) lib/redis/client.rb:350:in `ensure_connected'
redis (3.2.1) lib/redis/client.rb:207:in `block in process'
redis (3.2.1) lib/redis/client.rb:292:in `logging'
redis (3.2.1) lib/redis/client.rb:206:in `process'
redis (3.2.1) lib/redis/client.rb:112:in `call'
redis (3.2.1) lib/redis.rb:681:in `block in set'
redis (3.2.1) lib/redis.rb:37:in `block in synchronize'
/home/vagrant/.rubies/ruby_2.1.0/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
redis (3.2.1) lib/redis.rb:37:in `synchronize'
redis (3.2.1) lib/redis.rb:677:in `set'
redis-store (1.1.6) lib/redis/store/interface.rb:9:in `set'
redis-store (1.1.6) lib/redis/store/ttl.rb:8:in `set'
redis-store (1.1.6) lib/redis/store/marshalling.rb:5:in `block in set'
redis-store (1.1.6) lib/redis/store/marshalling.rb:29:in `_marshal'
redis-store (1.1.6) lib/redis/store/marshalling.rb:5:in `set'
redis-rack (1.5.0) lib/rack/session/redis.rb:31:in `block in get_session'
redis-rack (1.5.0) lib/rack/session/redis.rb:55:in `with_lock'
redis-rack (1.5.0) lib/rack/session/redis.rb:28:in `get_session'
rack (1.6.4) lib/rack/session/abstract/id.rb:266:in `load_session'
actionpack (4.2.0) lib/action_dispatch/middleware/session/abstract_store.rb:43:in `block in load_session'
actionpack (4.2.0) lib/action_dispatch/middleware/session/abstract_store.rb:51:in `stale_session_check!'
actionpack (4.2.0) lib/action_dispatch/middleware/session/abstract_store.rb:43:in `load_session'
rack (1.6.4) lib/rack/session/abstract/id.rb:151:in `load!'
rack (1.6.4) lib/rack/session/abstract/id.rb:147:in `load_for_write!'
rack (1.6.4) lib/rack/session/abstract/id.rb:73:in `[]='
actionpack (4.2.0) lib/action_controller/metal/request_forgery_protection.rb:314:in `real_csrf_token'
actionpack (4.2.0) lib/action_controller/metal/request_forgery_protection.rb:268:in `masked_authenticity_token'
actionpack (4.2.0) lib/action_controller/metal/request_forgery_protection.rb:260:in `form_authenticity_token'
actionpack (4.2.0) lib/abstract_controller/helpers.rb:67:in `form_authenticity_token'
actionview (4.2.0) lib/action_view/helpers/url_helper.rb:608:in `token_tag'
actionview (4.2.0) lib/action_view/helpers/form_tag_helper.rb:847:in `extra_tags_for_form'
actionview (4.2.0) lib/action_view/helpers/form_tag_helper.rb:861:in `form_tag_html'
actionview (4.2.0) lib/action_view/helpers/form_tag_helper.rb:866:in `form_tag_with_body'
actionview (4.2.0) lib/action_view/helpers/form_helper.rb:448:in `form_for'
components/frontend/app/views/frontend/pages/home.html.erb:22:in `_components_frontend_app_views_frontend_pages_home_html_erb___3446196759514705024_70018609103500'
actionview (4.2.0) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.0) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.0) lib/action_view/template.rb:143:in `render'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.0) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.0) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.0) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.0) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.0) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.0) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.0) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.0) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/vagrant/.rubies/ruby_2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.0) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:40:in `render'
wicked_pdf (0.11.0) lib/wicked_pdf/pdf_helper.rb:23:in `render_with_wicked_pdf'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/railtie.rb:194:in `public_send'
railties (4.2.0) lib/rails/railtie.rb:194:in `method_missing'
actionpack (4.2.0) lib/action_dispatch/routing/mapper.rb:51:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
better_errors (2.1.1) lib/better_errors/middleware.rb:59:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
/usr/lib/ruby/vendor_ruby/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:149:in `accept_and_process_next_request'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
/usr/lib/ruby/vendor_ruby/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
/usr/lib/ruby/vendor_ruby/phusion_passenger/utils.rb:111:in `block in create_thread_and_abort_on_exception'

Solution

  • So I was able to fix this, using the one-liner approach:

    MyApp::Application.config.session_store :redis_store, servers: "redis://#{APP_CONFIG[:redis_host]}:#{APP_CONFIG[:redis_port]}/#{APP_CONFIG[:redis_db]}/myapp_session", key: '_myapp_session', :expires_in => 90.minutes
    

    Please note the servers: which I accidentally left out before. Also note the key: '_myapp_session' it is important to add this (and match it with the cache namespace in the redis uri?) otherwise you will get a "Can't verify CSRF token authenticity" error when trying to connect with your API.