F
F
frolin2016-01-23 02:13:33
Nginx
frolin, 2016-01-23 02:13:33

Rails4 how to set relative_url_root other than '/'?

Greetings Toasters. Mind on fire. Help put out the fire
Solving the problem of simplifying the deployment of Rails applications in a demo environment, I ran into a couple of problems.
Plot:
I wanted the deployment for the demo environment to take place, with minimal participation of hands.
Namely, put all demo applications, for demonstration, into folders /var/www/apps/demo/$sitename/current/public
and so that nginx resolves $sitename based on the demo.server.ru/sitename address.
This has been successfully implemented here .
But a new problem looms, Rails says:

ActionController::RoutingError (No route matches [GET] "/sitename"):

The search led me to this decision. But it didn't solve the problem
// config/enviroments/production.rb
config.relative_url_root = "/sitename"
config.action_controller.relative_url_root = "/sitename"

The only way I managed to get, at least somehow, to earn routes, is directly, rudely, add to config.ru
map '/sitename' do
    run Myapp::Application
end

but this made it possible to only run root_path, all relative links do not work with this solution.
When writing, it already occurred to me to use subdomains, and then, probably, it will be possible to avoid this problem.
But still I want to understand how to set the initial path to the rails, different from the root.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
frolin, 2016-02-03
@Fralunia

In general, the story ended with success.
It turned out that the problem in routing was with static pages because of the High_Voltage gem.
You can get around this problem like this.

# config/initializers/high_voltage.rb
HighVoltage.configure do |config|
  config.routes = false
end

We specify scope in routes.rb and wrap high_voltage on our controller.
(more details https://github.com/thoughtbot/high_voltage)
# config/routes.rb
Rails.application.routes.draw do
  scope '/appname' do
    #переопределяем дефолтный путь high_voltage на свой Page контроллер 
    get "/pages/*id" => 'pages#show', as: :page, format: false
    
    resources :events
    root 'home_page#index'
  end
end

Final nginx config:
server {
  listen 80;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  server_name demo.server.ru;

  root /var/www/apps/demo/app_name/current/public;

  location /app_name{
    try_files $uri @app_name_production;
  }

  location @app_name_production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_pass http://unicorn_app_name_production;

    access_log /var/log/nginx/app_name_production.access.log;
    error_log /var/log/nginx/app_name_production.error.log;
  }

  location ^~ /assets/ {

    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question