專案

一般

個人檔案

動作

如何在子 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://127.0.0.1:8000/redmine
ProxyPassReverse /redmine https://127.0.0.1:8000/redmine

使用 Passenger(又稱 mod_rails)功能

如果您使用 Phusion Passenger 模組在 Apache Web 伺服器下執行 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 網頁伺服器(啟用了 mod_proxy),或者在另一台機器上有一個 Apache 反向代理,則您可以在特定端口上運行 Redmine 並使用這種配置,使其看起來像是在子目錄中運行

ProxyPass /redmine http://real-redmine-server.localdomain:3000/
ProxyPassReverse /redmine http://real-redmine-server.localdomain:3000/
請參閱 Apache 官方文件以進行調整。

使用 Puma

  1. 定義 RAILS_RELATIVE_URL_ROOT 環境變數
    
    export RAILS_RELATIVE_URL_ROOT=/redmine
    
    或使用 RedmineUtils 方法
    這允許 Rails 生成以 RAILS_RELATIVE_URL_ROOT 為前綴的網址。
  2. 然後更新您的 config.ru
    # 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
    
    這允許 Rack 處理以 RAILS_RELATIVE_URL_ROOT 為前綴的網址。

兩點都是必要的。

如果使用 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

可以在 https://127.0.0.1:3000/suburi 中訪問生成的 Redmine

舊版本的 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://rails-guides.dev.org.tw/configuring.html#configuring-action-controller

David Navarro Solans超過 5 年前 更新 · 18 個版本