Nginx + Rails,默认网址不起作用

时间:2016-10-09 04:15:46

标签: ruby-on-rails url nginx routes default

我认为我的默认网址设置为

  

example.com/asd/ --->我的root_path

并且所有内容都正确加载。但是,每次我点击

  

'<%= link_to'某事',something_path%>'

我被导演

  

example.com/something --->没有/ asd。

这是我的nginx配置或rails配置的问题吗?我该怎么做才能解决它?

  

- 额外 - 如果我进入example.com/asd/something,一切都很好。

/etc/nginx/nginx.conf

  

用户www-data;

     

worker_processes auto;

     

pid /run/nginx.pid;

     

活动{           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的/位点可用/默认

  

服务器{

    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'  
     

1 个答案:

答案 0 :(得分:0)

我认为问题在于Rails没有Nginx。 也许你错过了一个没有附加“/ asd”的配置(自从我使用RoR已经很长时间了,所以我不记得了)

快速修复可能是为 _URL 更改 _PATH (例如 about_path 会更改为 about_url 并将其添加到ApplicationsController

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

_PATH 返回相对路径,同时 _URL 返回绝对路径。

例如,对于“www.page.com/prettyPics”, _PATH 将为“/ prettyPics”,整个网址将为 _URL 。 这可能有点http://guides.rubyonrails.org/routing.html(第2.3部分)