V
V
Vayladion Gognazdiak2014-10-07 09:11:48
Nginx
Vayladion Gognazdiak, 2014-10-07 09:11:48

How to solve the problem with routing in rails when proxying nginx?

Given:
HOST1 address: free.magazine.ru/1c/ , which is proxied via nginx (config below) to HOST2
1c.partner.ru, where the Rails application is deployed ( ruby ​​2.1.0, rails 4.1.4, unicorn + nginx ).
HOST1 NGINX CONF

location ^~ /1c {
    proxy_pass http://1c.partner.ru;
    include includes/upstream_proxyhost.conf;
    proxy_redirect http://1c.partner.ru/ /;
}

HOST2 NGINX CONF
upstream 1c-new {
  server unix:/tmp/unicorn.1c-new.sock fail_timeout=0;
}

server {
  listen 80;
  server_name 1c.partner.ru;

  root /home/deployer/1c/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @1c-new;

  location @1c-new {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://1c-new;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

When accessing free.magazine.ru/1c , the root application opens without assets.
All links on the pages do not contain /1c/ and therefore are not working. Those. instead of the normal address free.magazine.ru/1c/products/707/online the link leads to free.magazine.ru/products/707/online
Session stops working.
Although the application without proxying (at 1c.patner.ru) works fine.
ROUTES.RB
1c::Application.routes.draw do
  
  #scope "/1c/" do  
    resource :session  

    resources :users

    resources :oauth
    
    resources :categories do
      member do
        get :online, :home
      end
    end

    resources :products do
      member do
        get :online, :home, :go
      end
    end

    match 'logout', to: 'sessions#destroy', via: :get

    match 'search', to: 'welcome#search', via: :get
    match 'regulation', to: 'welcome#regulation', via: :get
    match 'online', to: 'welcome#online', via: :get   
    match 'home', to: 'welcome#home', via: :get
    
  #
    root to: 'welcome#online'
  #end
end
#1c::Application.routes.default_url_options[:host] = 'free.magazine.ru'

Tried using scope "/1c/" and default_url_options ( see routes.rb ) along with config.assets.prefix - same result as above.
How can you make the application work normally with such proxying?
Thanks in advance for your reply.
UPD1:
Adding config.relative_url_root = "/1c" to production.rb solved the assets problem, but links are still generated without the 1c prefix.
UPD2 SOLVED:
NGINX HOST1
server {
  listen 80;
  server_name free.magazine.ru;

  location ^~ /1c {
    proxy_pass http://1c.partner.ru;
  }
}

NGINX HOST2
server {
  listen 80;
  server_name 1c.roscontent.ru;

  root /home/deployer/1c/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @h5c-new;

  location @1c-new {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://1c-new;
  }

}

Magic rail
routes.rb
1c::Application.routes.draw do
 
  scope "/1c/" do  
    resource :session  

    resources :users

    resources :oauth
    
    resources :categories do
      member do
        get :online, :home
      end
    end

    resources :products do
      member do
        get :online, :home, :go
      end
    end

    match 'logout', to: 'sessions#destroy', via: :get

    match 'search', to: 'welcome#search', via: :get
    match 'regulation', to: 'welcome#regulation', via: :get
    match 'online', to: 'welcome#online', via: :get   
    match 'home', to: 'welcome#home', via: :get
    
  #
    root to: 'welcome#online'
  end
end

application.rb
module 1c
  class Application < Rails::Application
   config.relative_url_root = '/1c'
   end
end

production.rb
config.assets.prefix = File.join((config.relative_url_root || '/'), 'assets')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anyd3v, 2014-10-07
@anyd3v

Are you talking about it?
The default location for the manifest is the root of the location specified in config.assets.prefix ('/assets' by default).
(this is about assets)
UPD found this solution regarding links
https://community.webfaction.com/questions/9563/wr...
UPD2 and this
ENV["RAILS_RELATIVE_URL_ROOT"] is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
guides.rubyonrails.org/configuring.html#deploy-to-...

_
_ _, 2014-10-07
@AMar4enko

location / {
    location /assets {
       gzip_static on;
       expires max;
       add_header Cache-Control public;
    }
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://1c-new; 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question