Search code examples
ruby-on-railsurlnginxroutesdefault

Nginx + Rails, Default url not working


I supposedly have my default url set as

example.com/asd/ ---> my root_path

and everything loads correctly. However, everytime I click a

'<%= link_to 'something', something_path %>'

I'm directed to

example.com/something ---> without the /asd.

Is this a problem with my nginx configurations or rails configuration? What should I do to solve it?

--Extra-- If I enter to example.com/asd/something, everything loads fine too.

/etc/nginx/nginx.conf

user www-data;

worker_processes auto;

pid /run/nginx.pid;

events { worker_connections 768; }

http {

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    client_max_body_size 250M;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;
    gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json >application/javascripttext/xml application/xml application/xml+rss >text/javascript;
    include /etc/nginx/passenger.conf;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*; 

}

/etc/nginx/passenger.conf

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;

passenger_ruby /home/rubyuser/.rvm/gems/ruby-2.3.0/wrappers/ruby;

/etc/nginx/sites-available/default

server {

    listen 80 ;
    listen 443 ssl;
    server_name example.com;
    root /home/user/app/current/public;
    passenger_enabled on;
    passenger_app_env production;  

}

production.rb

Rails.application.configure do

    config.cache_classes = true
    config.eager_load = true
    config.consider_all_requests_local= false
    config.action_controller.perform_caching = true
    config.action_dispatch.rack_cache = true
    config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
    config.assets.js_compressor= :uglifier
    config.assets.css_compressor = :sass
    config.assets.compile = true
    config.assets.precompile =  ['*.js', '*.css', '*.css.erb']
    config.assets.digest = true
    config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
    config.log_level = :debug
    config.relative_url_root = "/asd"
    config.action_controller.asset_host = 'example.com'
    config.i18n.fallbacks = true
    config.active_support.deprecation = :notify
    config.log_formatter = ::Logger::Formatter.new
    config.active_record.dump_schema_after_migration = false
    config.force_ssl = false
    Rails.application.routes.default_url_options[:host] = 'example.com/asd'  

end


Solution

  • I think the problem is with Rails not with Nginx. Maybe you're missing a configuration that is not appending the "/asd" (It's been a long time since I used RoR so I don't remember exactly)

    A quick fix could be to change _PATH for _URL (for example about_path would change to about_url) and adding this to the ApplicationsController

    def default_url_options
       if Rails.env.production?
         {:host => "www.prettyPage.com/asd"}
       else  
         {}
      end
    end
    

    _PATH returns the relative path meanwhile _URL returns the absolute path.

    For example for "www.page.com/prettyPics" the _PATH would be "/prettyPics" and the whole url would be the _URL. This could help a little bit http://guides.rubyonrails.org/routing.html (Part 2.3)