观看路线中断生产

routes to views break on production

除了 root_path 之外的所有路线都在生产中中断,即使我的 assert_select 测试全部为绿色。

The core of the problem was misconfigured Ngninx-Passenger. Initially I used a lot of default nginx.conf, and I failed to track down exact error, it does not seems like it was passenger_document_root /var/www/html/mesite/public;, since it is in my configs atm and all is working, maybe there was some other configs paths in the Nginx which I did not saw, since in essence I rebuild all my configs\config paths, including Nginx default conf., going from hurikhan77 example config.

测试:

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

查看"contact.html.erb"

<% 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

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

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 Rails5.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 access登录相关GET

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

在线乘客:

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

a 和有人试图访问各种管理页面,sql 等等(但我的请求仍然没有错误,比如 /resume)

[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 错误来源:(它绝对是 public 文件夹中的 404 页,其中的一个是我的理解,它应该是)

<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 页面是由 passenger +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

而不是:

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

正如您所写,当您尝试在生产服务器上访问 /resume 时看不到 GET /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;
}