Rails + Passenger + Nginx错误请求来源(domainname.com)与请求Base_url(IP地址)

时间:2017-09-11 10:43:14

标签: ruby-on-rails passenger request-headers nginx-reverse-proxy

设置: Rails 5.1.3,带有Passenger和Nginx的Dreamhost VPS。 因为它是VPS,所以我没有root权限来执行任何需要root权限的功能。

情况:目前,我已经使用ActionCable功能重做了以前的rails应用程序。早期的配置是Apache而不是Nginx用于http服务器。一切正常,除了websockets,我读了很多文章,所有确认动作电缆+ apache将无法正常工作。

此时,我切换到NginX。使用NginX,我的第一个观察是应用程序不断尝试使用电缆/'进程并且无法升级到websocket。在阅读了各种讨论后,我设法解决了这个问题:

location /cable {
    proxy_pass http://127.0.0.1:8000/cable;
    proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

}

由于我没有root权限访问通常的dt / etc / nginx /.../***。conf文件,使用dreamhost VPS包,我可以在/ home / username / nginx /中添加一个文件(appname.com)/(any-file-name.conf),这将添加到nginx"服务器块",根据dreamhost文档。

^^所以上面的代码块单独在一个名为" redis.conf"的文件中。在该目录中排序了动作电缆问题。它成功地开始在频道上播放一切都很好。

我后来才意识到,主要问题是我无法执行任何创建,更新,销毁操作 - 任何需要POST或DELETE方法的操作,因为它会导致真实性令牌错误,并说"请求来源(http://siik.io - 这是应用和域名)与request.base_url(http://127.0.0.1)不匹配。

细节错误:

I, [2017-09-11T03:27:28.623080 #7273]  INFO -- : [50a21156-0333-413e-94b

8-07b791c209fc] Completed 200 OK in 83ms (Views: 56.0ms | ActiveRecord: 16.6ms)
I, [2017-09-11T03:27:31.950732 #7273]  INFO -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] Started POST "/conversations/1/messages" for 106.208.156.243 at 2017-09-11 03:27:31 -0700
I, [2017-09-11T03:27:31.953339 #7273]  INFO -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] Processing by MessagesController#create as JS
I, [2017-09-11T03:27:31.953511 #7273]  INFO -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d]   Parameters: {"utf8"=>"✓", "message"=>{"user_id"=>"1", "body"=>"sdfgh"}, "commit"=>"Send", "conversation_id"=>"1"}
W, [2017-09-11T03:27:31.954325 #7273]  WARN -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] HTTP Origin header (http://www.siik.io) didn't match request.base_url (http://127.0.0.1:8000)
I, [2017-09-11T03:27:31.954890 #7273]  INFO -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)
F, [2017-09-11T03:27:31.956888 #7273] FATAL -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d]   
F, [2017-09-11T03:27:31.956969 #7273] FATAL -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
F, [2017-09-11T03:27:31.957017 #7273] FATAL -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d]   
F, [2017-09-11T03:27:31.957126 #7273] FATAL -- : [dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/request_forgery_protection.rb:195:in `handle_unverified_request'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/request_forgery_protection.rb:227:in `handle_unverified_request'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] devise (4.3.0) lib/devise/controllers/helpers.rb:253:in `handle_unverified_request'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/request_forgery_protection.rb:222:in `verify_authenticity_token'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:413:in `block in make_lambda'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/abstract_controller/callbacks.rb:12:in `block (2 levels) in <module:Callbacks>'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:198:in `block in halting'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:507:in `block in invoke_before'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:507:in `each'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:507:in `invoke_before'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:130:in `run_callbacks'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] remotipart (1.3.1) lib/remotipart/middleware.rb:32:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] warden (1.2.7) lib/warden/manager.rb:35:in `catch'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] warden (1.2.7) lib/warden/manager.rb:35:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/etag.rb:25:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/conditional_get.rb:38:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/head.rb:12:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/method_override.rb:22:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/runtime.rb:22:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] railties (5.1.3) lib/rails/engine.rb:522:in `call'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] passenger (5.1.8) src/ruby_supportlib/phusion_passenger/rack/thread_handler_extension.rb:97:in `process_request'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] passenger (5.1.8) src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb:160:in `accept_and_process_next_request'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] passenger (5.1.8) src/ruby_supportlib/phusion_passenger/request_handler/thread_handler.rb:113:in `main_loop'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] passenger (5.1.8) src/ruby_supportlib/phusion_passenger/request_handler.rb:416:in `block (3 levels) in start_threads'
[dd8e08f7-f28b-497b-9ae9-e57a6a45c35d] passenger (5.1.8) src/ruby_supportlib/phusion_passenger/utils.rb:113:in `block in create_thread_and_abort_on_exception'

但是,当我通过http://siik.io访问该站点时:8000一切正常,即使是websockets也不需要redis.conf文件。

我已经历了一周的各种修复和文档,但未能找到解决方案。我认为基本要求是在location / {}块上添加更多标题以添加到nginx.conf文件中...(我已经列出了我在下面尝试过的一些示例。)

listen 80;
listen *:80;
server_name siik.io www.siik.io http://siik.io;
location / {

        proxy_redirect off;
        proxy_set_header Host $host:8000;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


        proxy_http_version 1.1;

        proxy_set_header   X-Real-IP $remote_addr:8000;
        proxy_set_header  X-Forwarded-Port $server_port;

        proxy_pass http://127.0.0.1:8000/;
}

..所以我基本上都在尝试各种组合&amp;上面的值的排列,看看是什么有效,因为很明显,我不知道这是如何工作的 - 我有一些关于反向代理和http服务器的概念性知识。

是的,我知道request.origin == request.base_url是来自rails的protect_from_forgery位,我不觉得把它关闭是一个解决方案,虽然,我确实读过包含正确标题的地方将解决这个问题。我已尝试过此标头集的一些变体,但没有一个工作,我想知道是否包含正确的标头应该到达http原始标头将从domainname.io更改为IP地址或request.base_url将从IP地址更改为域名??

哦,是的,当我在redis.conf旁边的新文件proxy.conf中添加location / {}块时,它会完全中断。通过domainname.io的网站拒绝连接,而domainname.io:8000完美无缺。即使是位置下的空块/打破它 - 我正在测试是否有任何特定的proxy_set命令导致它中断 - 但没有。只是位&#34;位置/ {}&#34;打破它 - 也许这是预期的?

为什么当我通过siik.io:8000访问它时,这是否完美?端口号是否明确?

任何帮助都非常感谢!!!!

2 个答案:

答案 0 :(得分:0)

您不需要在其他端口上运行操作电缆服务器,请尝试按照以下说明操作:https://www.phusionpassenger.com/library/config/nginx/action_cable_integration/

根据THE RULES™复制到这里:

在子URI

下的同一主机和端口上运行Action Cable服务器

这是Rails推荐的默认设置,也是最简单的设置。它的工作原理是将ActionCable.server挂载到config/routes.rb中的某个路径。这样,您的Action Cable服务器将在与您的应用程序相同的主机和端口上运行,但在子URI下运行。

例如,您的routes.rb可能包含:

# Serve websocket cable requests in-process
mount ActionCable.server => '/cable'

(虽然routes.rb评论表示挂载ActionCable.server是为了在进程中提供服务,但Passenger实际上坚持将其作为单独的进程运行。)

要在Passenger + Nginx中使用此功能,您需要在Nginx虚拟主机上添加一个代码段。假设您已经有一个适用于您的应用程序的虚拟主机,如下所示:

server {
    listen 80;
    server_name www.foo.com;
    root /path-to-your-app/public;
    passenger_enabled on;
}

您需要插入一个配置操作电缆端点的位置块,如下所示:

server {
    listen 80;
    server_name www.foo.com;
    root /path-to-your-app/public;
    passenger_enabled on;

    ### INSERT THIS!!! ###
    location /cable {
        passenger_app_group_name YOUR_APP_NAME_HERE_action_cable;
        passenger_force_max_concurrent_requests_per_process 0;
    }
}

/cable替换为routes.rb中指定的实际操作电缆路径。

YOUR_APP_NAME_HERE替换为唯一标识符,该标识符不会出现在Nginx配置文件中的任何其他位置。

passenger_force_max_concurrent_requests_per_process选项调整Passenger以获得最佳WebSocket性能。

答案 1 :(得分:0)

固定。

我不完全确定这是如何解决的,我还在努力弄清楚为什么一切正常。 基本上,问题似乎与Passenger + Nginx配置的维护方式有关。 - 我相信这是一个特定于梦想主机的问题。

根据Dave Jones在youtube上的教程,我的初始VPS配置是使用Apache作为http服务器完成的。在Dreamhost面板的域托管部分中,有一个复选框,可以使用带有ruby应用程序的Passenger,根据我正在关注的原始教程,该应用程序未被选中。 阅读@Camden的链接 - 非常有帮助 - 我决定从头开始重新配置所有内容,并在控制面板上检查了这个Passenger Box,以及HTTPS安全位,添加了来自&#39; Let&#39的证书; s加密SSL&#39;并重新启动我的服务器。 一切都开始按要求完美运作。我还在&#39; / cable&#39;的位置块下添加了443端口的标题。在CONF文件中 - 并在production.rb文件中添加了http://www.domainnam.io,并将config.force_ssl设置为true。

因此,总而言之,我理解问题在于乘客nginx集成的配置不当。 谢谢@Camden的链接。