希望一切都好。我有以下问题,希望你能帮助我:
我正在尝试使用 vagrant 和 ansible 部署我的 rails 4 应用程序。该应用程序的一部分是一个 redis 服务器。我已经设置并运行了这个192.168.33.2:6379
,我的 rails 应用程序正在运行192.168.33.4
并在 ubuntu + nginx 上运行。
即使我已经使用 redis-rb (应用程序中的宝石)设置了 redis,并使用从 YAML 文件启动的应用程序全局变量在我的一个初始化程序中初始化了一个 redis 对象:
$redis = Redis.new(:driver => :hiredis, :host => APP_CONFIG[:redis_host], :port => APP_CONFIG[:redis_port])
APP_CONFIG[:redis_host] = 192.168.33.2, APP_CONFIG[:redis_port] = 6379
bind 0.0.0.0
在我的 redis.conf 文件中,当我在浏览器中访问 rails 应用程序时,仍然收到错误Error connected to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) 。
当vagrant redis-cli -h 192.168.33.2 ping
ssh-ed into192.168.33.4
返回时运行pong
。所以可以建立连接,我可以访问redis服务器/查看来自192.168.33.4
...的密钥
netstat -l
在终端中运行也给了我:tcp 00 *:6379 *:* LISTEN
有谁知道我该如何解决这个问题?
提前致谢!
更新
注意:我使用了redis-rails gem,并在我的应用程序中将它用于 session_store 和 cache_store。(当不使用 redis 时它可以工作)。以这种方式配置:
#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 }
有迹可循:
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'