生产视图的路径中断

时间:2018-04-05 01:45:52

标签: ruby-on-rails ruby nginx ruby-on-rails-5 passenger

除了root_path之外的所有路由都在prod。中,即使我的assert_select测试所有GREEN。

  

问题的核心是错误配置的Ngninx-Passenger。最初我使用了很多默认的nginx.conf,但我失败了   为了追踪确切的错误,它似乎不是passenger_document_root /var/www/html/mesite/public;,因为它在我的配置atm并且一切正常,也许在Nginx中有一些其他配置路径,我没有看到,因为本质上我重建了所有我的配置\配置路径,包括Nginx默认配置,来自 hurikhan77 示例配置。

试验:

site_layout_test.rb

class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'me_site_static_html/me'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", resume_path
    assert_select "a[href=?]", craft_path
    assert_select "a[href=?]", dance_path
    assert_select "a[href=?]", music_path
    assert_select "a[href=?]", contact_path
  end
end

me_site_static_html_controller_test.rb

class MeSiteStaticHtmlControllerTest < ActionDispatch::IntegrationTest

  test "should get root" do
    get root_url 
    assert_response :success
  end

  def setup
    @base_site_name = "Пешеев Павел"
  end

  test "should get me" do
    get root_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Я"
  end

  test "should get resume" do
    get resume_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Резюме"
  end

  test "should get craft" do
    get craft_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Навыки"
  end

  test "should get dance" do
    get dance_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Танцует!"
  end

  test "should get music" do
    get music_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Меломан"
  end

  test "should get contact" do
    get contact_path
    assert_response :success
    assert_select "title", "#{@base_site_name} | Связаться"
  end

end

me_site_static_html_helper.rb(此示例视图提供有关变量的一些上下文)

module MeSiteStaticHtmlHelper
    # Returns the full title on a per-page basis.
  def full_title(page_title = '')
    base_title = "Пешеев Павел"
    if page_title.empty?
      base_title
    else
      base_title + " | " + page_title
    end
   end
end

view&#34; contact.html.erb&#34;

<% provide(:title, "Связаться") %>
...

routes.rb(UPD:代码中有一些变化,根据一些答案,但它们似乎没有区别,结果是相同的。想更多地了解这些差异)

Rails.application.routes.draw do

  root 'me_site_static_html#me'

  get '/me',     to: 'me_site_static_html#me'

  get '/resume'   => 'me_site_static_html#resume'

  get 'craft',    to:'me_site_static_html#craft'

  get '/dance',   to:'me_site_static_html#dance'

  get 'music'    =>  'me_site_static_html#music'

  get '/contact',   to: 'me_site_static_html#contact'

end

只是因为我感到困惑的是为什么会发生这种情况(再次,测试在prod。和dev中运行绿色)我唯一的控制器

class MeSiteStaticHtmlController < ApplicationController
  def me
  end

  def resume
  end

  def craft
  end

  def dance
  end

  def music
  end

  def contact
  end
end

这里是部署结果https://pesheevpavel.ru/ 并且,正如您所看到的,两个指向另一个静态页面的链接以及对相应页面的GET请求都不起作用,这有点令人困惑。所有链接看起来都很正确。 header html.erb看起来像这样:

<header class="navbar navbar-fixed-top">
    <%= link_to "Пешеев Павел", root_path, id: "logo" %>
    <nav>
      <ul class="nav navbar-nav navbar-right">
        <li><%= link_to "Я",        root_path %></div></li>
        <li><%= link_to "Резюме ",  resume_path %></li>
        <li><%= link_to "Крафт ",   craft_path %></li>
        <li><%= link_to "Танцы ",   dance_path %></li>
        <li><%= link_to "Музыка ",  music_path %></li>
      </ul>
    </nav>
</header>

并在root_path

生成此html
<header class="navbar navbar-fixed-top">
<a id="logo" href="/">Пешеев Павел</a>
<nav>
  <ul class="nav navbar-nav navbar-right">
    <li><a href="/">Я</a></li>
    <li><a href="/resume">Резюме </a></li>
    <li><a href="/craft">Крафт </a></li>
    <li><a href="/dance">Танцы </a></li>
    <li><a href="/music">Музыка </a></li>
  </ul>
</nav>

相关nginx配置:

passenger_root /usr/share/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/tech/.rvm/gems/ruby-2.4.1/wrappers/ruby;
passenger_instance_registry_dir /var/run/passenger-instreg;
root   /var/www/html/mesite/public;
passenger_document_root /var/www/html/mesite/public;
    passenger_enabled on;  
    rails_env production;


    index index.php index.html index.htm;

   location ~ ^/assets/ {
   expires 1y;
   add_header Cache-Control public; 
   add_header ETag "";
   break;
   }

错误页面(404)

The page you were looking for doesn't exist.

You may have mistyped the address or the page may have moved.

rails -v Rails 5.1.6

日志:

 rake routes
 Prefix Verb URI Pattern        Controller#Action
     me GET  /me(.:format)      me_site_static_html#me
 resume GET  /resume(.:format)  me_site_static_html#resume
  craft GET  /craft(.:format)   me_site_static_html#craft
  dance GET  /dance(.:format)   me_site_static_html#dance
  music GET  /music(.:format)   me_site_static_html#music
contact GET  /contact(.:format) me_site_static_html#contact
   root GET  /                  me_site_static_html#me

production.rb config.log_level = :debug production.log(没有错误(?))

I, [2018-04-05T18:05:37.777489 #5609]  INFO -- : [8c046ad2-d911-487e-bd6c-00d6db966a75] Completed 200 OK in 4ms (Views: 3.1ms)
I, [2018-04-05T18:05:38.909255 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759] Started GET "/" for 79.173.103.210 at 2018-04-05 18:05:38 +0300
I, [2018-04-05T18:05:38.910120 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759] Processing by MeSiteStaticHtmlController#me as HTML
I, [2018-04-05T18:05:38.911059 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendering me_site_static_html/me.html.erb within layouts/application
I, [2018-04-05T18:05:38.911620 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendered me_site_static_html/me.html.erb within layouts/application (0.4ms)
I, [2018-04-05T18:05:38.912188 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendered layouts/_rails_default.html.erb (0.4ms)
I, [2018-04-05T18:05:38.912362 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendered layouts/_shim.html.erb (0.0ms)
I, [2018-04-05T18:05:38.912861 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendered layouts/_header.html.erb (0.3ms)
I, [2018-04-05T18:05:38.913137 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759]   Rendered layouts/_footer.html.erb (0.1ms)
I, [2018-04-05T18:05:38.913369 #5609]  INFO -- : [907e6cd0-2547-4bf4-b07c-9acb56668759] Completed 200 OK in 3ms (Views: 2.5ms)

Nginx访问登录相关的GET

79.173.103.210 - - [05 / Apr / 2018:18:15:05 +0300]&#34; GET / resume HTTP / 1.1&#34; 404 1722&#34; https://pesheevpavel.ru/&#34; &#34; Mozilla / 5.0(Windows NT 10.0; Win64; x64; rv:59.0)Gecko / 20100101 Firefox / 59.0&#34; &#34; - &#34;

乘客在线:

[ N 2018-04-05 17:57:26.0985 5440/T1 age/Cor/CoreMain.cpp:984 ]: Passenger core online, PID 5440

aand有人试图访问各种管理页面,sql和诸如此类的东西(但我的请求仍然没有错误,比方说/恢复)

[error] 15486#0: *243 "/usr/share/nginx/html/mysqlmanager/index.html" is not found (2: No such file or directory), client: 185.40.4.17, server: localhost, request: "GET /mysqlmanager/ HTTP/1.1", host: "46.229.212.97", referrer: "http://46.229.212.97/"

html错误来源:(它绝对是来自公共文件夹的404页面,其中一个是fiew的东西,这是我应该理解的)

<body class="rails-default-error-page">
  <!-- This file lives in public/404.html -->
  <div class="dialog">
    <div>
      <h1>The page you were looking for doesn't exist.</h1>
      <p>You may have mistyped the address or the page may have moved.</p>
    </div>
    <p>If you are the application owner check the logs for more information.</p>
  </div>
</body>

从客户端发现Mozilla日志中的一些奇怪现象:显然我的webroot页面由乘客+ nginx提供:

Server  
nginx/1.12.2 + Phusion Passenger 5.2.3
Set-Cookie  
_mesite_session=MXRQaXlQNWpxbk…b9c; path=/; secure; HttpOnly
Status  
200 OK

我的404仅由nginx提供服务:

Connection  
keep-alive
Content-Length  
1722
Content-Type    
text/html
Date    
Thu, 05 Apr 2018 15:53:41 GMT
ETag    
"5ac13601-6ba"
Server  
nginx/1.12.2

2 个答案:

答案 0 :(得分:2)

正如您所写的那样,当您尝试访问生产服务器上的GET /resume但未看到/resume时,您看不到GET /,我认为您的nginx配置为碎。

您的问题可能与使用passenger_document_root有关 - 您不应该将其用于部署(仅在将应用部署到子目录时使用)。

这是我正在使用的一个工作示例,请适应您的设置(这是一个Rails / Passenger-only nginx实例):

主要配置:

# cat nginx.conf                                                                                                                                                                                        
user nginx nginx;                                                                                                                                                                                                     
worker_processes 4;                                                                                                                                                                                                   

events {                                                                                                                                                                                                              
        worker_connections 1024;                                                                                                                                                                                      
        use epoll;                                                                                                                                                                                                    
}                                                                                                                                                                                                                     

http {
        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        client_header_timeout 10m;
        client_body_timeout 10m;
        send_timeout 10m;

        connection_pool_size 256;
        client_header_buffer_size 1k;
        large_client_header_buffers 4 8k;
        request_pool_size 4k;

        gzip on;
        gzip_min_length 1100;
        gzip_buffers 4 8k;
        gzip_types text/plain;

        output_buffers 1 32k;
        postpone_output 1460;

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;

        keepalive_timeout 75 20;

        ignore_invalid_headers on;

        index index.html;

        client_max_body_size 100m;

        passenger_root /usr/lib64/ruby/vendor_ruby/phusion_passenger/locations.ini;
        passenger_ruby /usr/bin/ruby;
        passenger_max_pool_size 50;
        passenger_pool_idle_time 1200;
        passenger_max_instances_per_app 20;
        passenger_max_requests 1000;

        include sites.d/*.conf;
}

常规设置:

# cat settings.d/expire-statics.conf
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
        add_header Cache-Control public;
}

网站配置:

# cat sites.d/aaaa.bbbbb.de.conf
server {
        listen 80;

        server_name aaaa.bbbbb.de;
        include settings.d/expire-statics.conf;

        root /home/kakra/rails-apps/aaaa/production/current/public;
        rails_env production;
        passenger_enabled on;
        passenger_min_instances 5;
}

答案 1 :(得分:1)

而不是:

Rails.application.routes.draw do
  root 'me_site_static_html#me'

  get '/me',        to: 'me_site_static_html#me'

  get '/resume',    to: 'me_site_static_html#resume'

  get '/craft',     to: 'me_site_static_html#craft'

  get '/dance',     to: 'me_site_static_html#dance'

  get '/music',     to: 'me_site_static_html#music'

  get '/contact',   to: 'me_site_static_html#contact'

end

试试:

Rails.application.routes.draw do
  root 'me_site_static_html#me'

  get 'me' => 'me_site_static_html#me'

  get 'resume' => 'me_site_static_html#resume'

  get 'craft' => 'me_site_static_html#craft'

  get 'dance' => 'me_site_static_html#dance'

  get 'music' => 'me_site_static_html#music'

  get 'contact' => 'me_site_static_html#contact'

end