2013-06-26 38 views

回答

3

對於Rails3.1 +(+ Rails4),你應該按照下面這些: 首先,將gem rack-proxy添加到Gemfile中

寶石 「機架代理」

然後添加文件proxy.rb到Rails.root/lib目錄是這樣的:

require 'rack/proxy' 
class Proxy < Rack::Proxy 
    def initialize(app) 
    @app = app 
    end 
    def call(env) 
    original_host = env["HTTP_HOST"] 
    rewrite_env(env) 
    if env["HTTP_HOST"] != original_host 
     perform_request(env) 
    else 
     # just regular 
     @app.call(env) 
    end 
    end 
    def rewrite_env(env) 
    request = Rack::Request.new(env) 
    if request.path =~ /^\/blog(\/.*)$/ 
     # enable these code if you set blog as ROOT directory in wordpress folder 
     # env['REQUEST_PATH'] = '/' 
     # env['ORIGINAL_FULLPATH'] = '/' 
     # env['PATH_INFO'] = '/' # set root path request 
     env['REQUEST_URI'] = 'http://tinle1201.wordpressdomain.com' # your path 
     env["SERVER_PORT"] = 80 
     env["HTTP_HOST"] = "tinle1201.wordpressdomain.com" # point to your host 
    end 
    env 
    end 
end* 

註冊代理中間件到的config/application.rb中:

require File.expand_path('../boot', __FILE__) 
require 'rails/all' 
# Require the gems listed in Gemfile, including any gems 
# you've limited to :test, :development, or :production. 
Bundler.require(:default, Rails.env) 
module AdultSavings 
    class Application < Rails::Application 
    config.autoload_paths += %W(#{config.root}/lib) 
    config.middleware.use "Proxy" 
    # Settings in config/environments/* take precedence over those specified here. 
    # Application configuration should go into files in config/initializers 
    # -- all .rb files in that directory are automatically loaded. 
    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. 
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. 
    # config.time_zone = 'Central Time (US & Canada)' 
    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. 
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] 
    # config.i18n.default_locale = :de 
    end 
end* 

添加兩行到PHP文件的wp-config.php文件:enter code here

define('WP_SITEURL', 'http://tinle1201.wordpressdomain.com/blog'); 
define('WP_HOME', 'http://tinle1201.wordpressdomain.com/blog'); 
0

Rails的配置

加入Gemfile中:

gem "rack-reverse-proxy", :require => "rack/reverse_proxy" 

現在,我們希望/博客和/博客/被定向到從Rails應用程序WordPress的實例。

添加到您的config.ru你運行應用程序權利之前:

use Rack::ReverseProxy do 
    reverse_proxy(/^\/blog(\/.*)$/, 
    'http://CHANGEME.herokuapp.com$1', 
    opts = {:preserve_host => true}) 
end 

在配置/ routes.rb中添加一條路由:

match "/blog" => redirect("/blog/") 

更多信息here

+0

我非常瞭解那篇文章,儘管它適用於基於heroku的wordpress。這是我嘗試解決方案的一部分,我可以讓博客顯示在正確的子目錄中,但是當您單擊博客上的任何內容時,它會切換到博客的域。 – DogNibbler

+1

爲了解決此問題,我將wordpress上的網站地址URL更改爲mywebsite.com/blog,並將rewritebase/blog /添加到.htaccess。這適用於開箱即用的wordpress,因爲帖子是用/?p = 1這樣的擴展來組織的,因爲這會轉到第一篇文章。我正在嘗試使用的大量修改的博客有像blogurl.com/name-of-article那樣組織的帖子,並且由於某些原因,當我嘗試使用相同的確切解決方案單擊主頁上的帖子時,會將我帶到我的根目錄站點而不是訪問文章 – DogNibbler

+0

請注意,「機架反向代理」不適用於生產系統。最好使用您的網絡服務器(如Nginx或Apache)來配置生產中的代理傳遞設置。 –

相關問題