如何在子URI中安装Redmine¶
本页解释了如何在您的站点子目录中运行Redmine,例如 http://www.mysite.com/redmine/;在这种情况下,您可能会感到困惑,因为经典的Redmine安装不能直接工作,并且指向CSS或javascript文件的链接似乎已损坏。在本页中,我们假设您想在您的站点“/redmine/”子目录下运行Redmine。
工作版本(2016年6月26日星期日 13:48:50 MSK)¶
在config/environment.rb文件底部更改以下行
# Initialize the Rails application
Rails.application.initialize!
到
RedmineApp::Application.routes.default_scope = "/redmine"
# Initialize the Rails application
Rails.application.initialize!
使用Redmine::Utils(首选解决方案)¶
在config/environment.rb文件底部添加以下行
Redmine::Utils::relative_url_root = "/redmine"
然后重新启动您的应用程序。
使用Rails功能¶
在config/environment.rb文件末尾添加以下行
ActionController::AbstractRequest.relative_url_root = "/redmine"Rails将自动为所有链接添加前缀“/redmine”。这可以被认为是简单、干净且最灵活的解决方案。然后重新启动您的应用程序。在Rails的较新版本中,类层次结构略有变化,您需要使用
ActionController::Base.relative_url_root = "/redmine"作为类名。
使用Mongrel功能¶
如果您在Mongrel服务器下运行Redmine,您还可以使用Mongrel的“--prefix”选项
mongrel_rails start --prefix=/redmine
Mongrel_rails服务“--prefix”指令与Rails 2.3.x版本不兼容
要解决这个问题,请创建一个文件config/initializers/patch_for_mongrel.rb [文件名可以是任何名称]
# Fix for mongrel which still doesn't know about Rails 2.2's changes,
# We provide a backwards compatible wrapper around the new
# ActionController::base.relative_url_root,
# so it can still be called off of the actually non-existing
# AbstractRequest class.
module ActionController
class AbstractRequest < ActionController::Request
def self.relative_url_root=(path)
ActionController::Base.relative_url_root=(path)
end
def self.relative_url_root
ActionController::Base.relative_url_root
end
end
end
#
# Thanks to http://www.ruby-forum.com/topic/190287
您可能无法在端口80上运行Mongrel:如果您在同一主机上有Apache服务器,并且您在端口8000上运行Mongrel,您可以使用以下Apache配置进行重定向(假设已启用Apache的mod_proxy)
ProxyPass /redmine https://:8000/redmine ProxyPassReverse /redmine https://:8000/redmine
使用Passenger(又名mod_rails)功能¶
如果您在Apache web服务器下使用Phusion Passenger模块运行Redmine,请遵循此指南;请注意,在某些Passenger版本或某些Linux发行版上可能无法正确工作。
使用Passenger+Nginx功能¶
请参阅官方指南(这是我唯一成功的方法 - 2012年10月30日)
使用Unicorn+Nginx¶
nginx配置
location /redmine {
alias <PATH_TO>/redmine/public;
try_files $uri/index.html $uri.html $uri @app;
}
config/routes.rb
Redmine::Utils::relative_url_root = "/redmine" RedmineApp::Application.routes.draw do scope Redmine::Utils::relative_url_root do root :to => 'welcome#index', :as => 'home' ..... end end
使用反向代理¶
如果您前面有Apache web服务器(已启用mod_proxy)或另一台机器上的Apache反向代理,您可以在特定端口上运行Redmine,并使用此类配置,使其看起来像在子目录中运行
ProxyPass /redmine http://real-redmine-server.localdomain:3000/ ProxyPassReverse /redmine http://real-redmine-server.localdomain:3000/参阅Apache官方文档以进行适配。
使用puma¶
- 定义RAILS_RELATIVE_URL_ROOT环境变量
或使用RedmineUtils方法export RAILS_RELATIVE_URL_ROOT=/redmine
这允许rails生成以RAILS_RELATIVE_URL_ROOT为前缀的URL。 - 然后更新您的config.ru
这允许rack处理以RAILS_RELATIVE_URL_ROOT为前缀的URL。# This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) map ENV['RAILS_RELATIVE_URL_ROOT'] || '/' do run RedmineApp::Application end
这两点都是必要的。
如果您使用的是Docker官方镜像的passenger版本¶
在一个空目录中创建一个Dockerfile,内容如下,调整标签为要使用的版本
FROM redmine:2.6.10-passenger COPY assets/passenger-nginx-config-template.erb /passenger-nginx-config-template.erb CMD ["passenger", "start", "--nginx-config-template", "/passenger-nginx-config-template.erb"]
在同一个目录中创建一个passenger-nginx-config-template.erb文件,内容如下,调整您要使用的子URI
<%= include_passenger_internal_template('global.erb') %>
worker_processes 1;
events {
worker_connections 4096;
}
http {
<%= include_passenger_internal_template('http.erb', 4) %>
default_type application/octet-stream;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
client_max_body_size 1024m;
access_log off;
keepalive_timeout 60;
underscores_in_headers on;
gzip on;
gzip_comp_level 3;
gzip_min_length 150;
gzip_proxied any;
gzip_types text/plain text/css text/json text/javascript
application/javascript application/x-javascript application/json
application/rss+xml application/vnd.ms-fontobject application/x-font-ttf
application/xml font/opentype image/svg+xml text/xml;
server {
server_name _;
listen 0.0.0.0:3000;
root '/usr/src/redmine/public';
passenger_app_env 'production';
passenger_spawn_method 'smart';
passenger_load_shell_envvars off;
location ~ ^/suburi(/.*|$) {
alias /usr/src/redmine/public$1;
passenger_base_uri /suburi;
passenger_app_root /usr/src/redmine;
passenger_document_root /usr/src/redmine/public;
passenger_enabled on;
}
}
passenger_pre_start http://0.0.0.0:3000/;
}
构建Docker镜像并启动容器
docker build -t redmine-in-a-sub-uri:2.6.10-passenger . docker run -d --name redmine-in-a-sub-uri -p 3000:3000 redmine-in-a-sub-uri:2.6.10-passenger
结果Redmine可以通过https://:3000/suburi访问
旧版本的Redmine和Rails¶
如果您运行的是非常旧的Redmine版本(不知道具体是哪些),那么您可能使用的Rails' ActionController版本不支持上面提到的"relative_url_root"。然后您可以去这个页面来重现相同的行为,但这在大多数情况下并不是一个好主意,您应该考虑升级Redmine。
参考¶
如果这个页面没有解决您的问题,您可以查看#2508或这个线程。
Windows:在Apache子目录下配置Ruby On Rails应用程序 - http://stackoverflow.com/a/470973/663172
配置ruby on rails Action Controller - https://guides.rubyonrails.cn/configuring.html#configuring-action-controller
由David Navarro Solans更新,超过5年前· 18次修订