首页 文章

Rails 3在子目录中运行,资产未找到

提问于
浏览
1

我正在使用nginx服务两个Rails站点 . Rails1不使用资产管道,但Rails2使用 . Rails2还使用前缀将其与Rails1区分开来 . 例如:

http://myhost     -> Rails1
http://myhost/abc -> Rails2

两个站点都在运行,但是对Rails2站点上的资产的任何引用都是 not found .

这是我的伪nginx.conf的样子:

http {
    upstream rails1 {
        server 127.0.0.1:3000;
    }
    upstream rails2 {
        server 127.0.0.1:3030;
    }
    server {
        location ~ ^/assets/  {
            expires max;
            add_header Cache-Control public;
            access_log off;
        }
        location /abc {
            proxy_pass http://rails2;
        }
        location / {
            proxy_pass http://rails1;
        }
    }
}

另外,我的Rails 2应用程序中的routes.rb:

Rails2App::Application.routes.draw do

  scope '/abc' do
    resources :projects
    root :to => 'home#index'
  end

end

浏览到 http://myhost/abc/ for Rails2应用程序,显示没有css的页面,并出现以下错误:

GET http://myhost/assets/application-asdasd.css 404 (Not Found)

我已经尝试在production.rb文件中使用 config.assets.prefix = '/abc' ,但它没有尝试在ngnix.conf文件中使用不同的变体也无济于事 .

有人知道我做错了什么,或者错过了什么?


UPDATE

我不太清楚为什么,但我能够使用@location而不是上游来(错误地)工作 . 但我不得不将资源文件夹从Rails2应用程序移动到Rails1应用程序 . 不完全理想 .

对服务器部分的更改:

location ~ ^/(assets)/  {
    expires max;
    add_header Cache-Control public;
    access_log off;
}

location ~ ^/(abc)/ {
    root /rails2/public;
    try_files $uri/index.html $uri.html $uri @rails2;
    error_page 404              /404.html;
    error_page 422              /422.html;
    error_page 500 502 503 504  /500.html;
    error_page 403              /403.html;
}

location / {
    root /rails1/public;
    try_files $uri/index.html $uri.html $uri @rails1;
    error_page 404              /404.html;
    error_page 422              /422.html;
    error_page 500 502 503 504  /500.html;
    error_page 403              /403.html;
}

location @rails1 {
    proxy_pass http://127.0.0.1:3000;
}

location @rails2 {
    proxy_pass http://127.0.0.1:3030;
}

2 回答

  • 1

    来自一个类似的问题:https://stackoverflow.com/a/3355589/417872

    你试过这个吗?

    config.action_controller.relative_url_root = '/rails_app'
    

    我建议googleing如何从子目录正确地提供rails应用程序 . 这是你正在处理的真正问题,快速搜索返回了几页有用的链接 .

  • 0

    我在 生产环境 环境中在Nginx上运行Rails 4时遇到了类似的问题 . 我找到的解决方案是在 nginx.conf 中指定资产位置的 root 路径:

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

    希望这可以帮助

相关问题