<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>php ror together apache proxy on random thoughts</title><link>https://awesomeprogrammer.com/categories/php-ror-together-apache-proxy/</link><description>Recent content in php ror together apache proxy on random thoughts</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 10 Oct 2012 00:00:00 +0000</lastBuildDate><atom:link href="https://awesomeprogrammer.com/categories/php-ror-together-apache-proxy/index.xml" rel="self" type="application/rss+xml"/><item><title>Breaking Rails, part 2, putting your RoR inside your PHP</title><link>https://awesomeprogrammer.com/blog/2012/10/10/breaking-rails-putting-your-ror-inside-your-php/</link><pubDate>Wed, 10 Oct 2012 00:00:00 +0000</pubDate><guid>https://awesomeprogrammer.com/blog/2012/10/10/breaking-rails-putting-your-ror-inside-your-php/</guid><description><![CDATA[<p><em>Yo dawg, I heard you like&hellip;</em></p>
<p>Purely theoretical (obviously) case: you have existing php app and some legacy rails code that you refreshed a little bit and now someone asks you one simple question - <em>why can&rsquo;t you just put <strong>that</strong> code into <strong>that</strong> code and run it as one app</em>? Sometimes you can reason, sometimes not. But let&rsquo;s do it - I mean seriously - let&rsquo;s create some kind of hybrid that will work transparently and silently for the user. Unleash your imagination and get ready for breaking every programming paradigm; treat it like a sport and get ready :).</p>
<p>Let&rsquo;s ever get a little bit further, let&rsquo;s create one rails app that will distribute its content for two different domains. Where do we start?</p>
<p>I assume you have some kind of working application already, in the end we want to have:</p>
<ul>
<li>domain.com - our main application written in php</li>
<li>domain.com/superapp1 - rails app that is serving some part of content</li>
<li>domain.com/superapp2 - same rails app that is serving another part of content</li>
</ul>
<p>Moreover we want to create this whole structure in a way that it can be ran as separate apps in the future <em>just in case</em>.</p>
<p>First - get familiar with <a href="http://httpd.apache.org/docs/2.2/mod/mod_proxy.html">mod proxy</a> as I&rsquo;ll be using Apache here with with <a href="http://www.modrails.com">phusion passanger</a>. I think the most reasonable approach is to handle two different sub-domains for serving content (plus admin namespace for backend).</p>
<p>So looking at our application it would look like:</p>
<ul>
<li>superapp1.rails.app - for serving content for &lsquo;app1&rsquo;. This url will be not directly accessible on production.</li>
<li>superapp2.rails.app - same as above, for &lsquo;app2&rsquo;</li>
<li>rails.app/admin - handling backend with restricted access</li>
</ul>
<p>Be aware that I can&rsquo;t provide where working example as solutions may vary quite a lot depending on structure of your existing applications and need, but I will give you some ideas if you will need to (theoretically) create that kind of hybrid someday.</p>
<p>Configure phussion passanger for your rails app, don&rsquo;t forget to set <code>PassengerFriendlyErrorPages on</code>  and <code>RailsEnv &quot;development&quot;</code> for now.</p>
<p>Now it&rsquo;s time to start some proxy-action in your php app, setup in your virtual host config:</p>
<pre tabindex="0"><code>ProxyPass /superapp1 http://superapp1.rails.app
ProxyPassReverse /superapp1 http://superapp1.rails.app

ProxyPass /superapp1 http://superapp2.rails.app
ProxyPassReverse /superapp1 http://superapp2.rails.app
</code></pre><p>In your rails routes on the other hand you have to handle those sub-domains, after reading/watching <a href="http://railscasts.com/episodes/221-subdomains-in-rails-3?view=asciicast">this railscasts</a> I ended up with:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># config/routes.rb</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>  constraints(<span style="color:#66d9ef">Subdomain</span>) <span style="color:#66d9ef">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># some resources and stuff</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>  namespace <span style="color:#e6db74">:admin</span> <span style="color:#66d9ef">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># and here some controllers for handling backend</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>and:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/lib/subdomain.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Subdomain</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">self</span><span style="color:#f92672">.</span><span style="color:#a6e22e">matches?</span>(request)
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">%w(superapp1 superapp2)</span><span style="color:#f92672">.</span>each <span style="color:#66d9ef">do</span> <span style="color:#f92672">|</span>subdomain<span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span> <span style="color:#66d9ef">if</span> request<span style="color:#f92672">.</span>env<span style="color:#f92672">[</span><span style="color:#e6db74">&#39;SERVER_NAME&#39;</span><span style="color:#f92672">]</span> <span style="color:#f92672">=~</span> <span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">false</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>After reloading apache cofiguration (<code>service apache2 reload</code>) loading address /superapp1 from php app should bring on your brand new ror app.</p>
<p>Its not especially exciting and you may noticed some serious problems here:</p>
<ul>
<li>
<p>you ended up with broken urls when trying to move deeper inside your proxy-ed app - as you need to provide proper urls</p>
</li>
<li>
<p>your assets are broken because you are working really on subdomain, but that can be easily fixed by adding to your apache conf:
<code>ProxyPass /assets http://rails.app/assets</code></p>
</li>
<li>
<p>how can you serve diffirent part of content with same rails application depeding on those domains?</p>
</li>
<li>
<p>what about log in feature for your users?</p>
</li>
</ul>
<p>Lets stard from fixing those urls, one way is to overload <code>url_for</code> method from <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html">UrlHelper</a>. I ended up with something like that:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/helpers/url_helper.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">module</span> UrlHelper
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">url_for</span>(options <span style="color:#f92672">=</span> <span style="color:#66d9ef">nil</span>)
</span></span><span style="display:flex;"><span>    options<span style="color:#f92672">[</span><span style="color:#e6db74">:host</span><span style="color:#f92672">]</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">#{</span>request<span style="color:#f92672">.</span>domain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain_from_server<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span> <span style="color:#66d9ef">if</span> options<span style="color:#f92672">.</span>kind_of?(<span style="color:#66d9ef">Hash</span>) <span style="color:#f92672">&amp;&amp;</span> <span style="color:#f92672">!</span>subdomain_from_server<span style="color:#f92672">.</span>empty?
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">super</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/controllers/application_controllers.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ApplicationController</span> <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">ActionController</span><span style="color:#f92672">::</span><span style="color:#66d9ef">Base</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">include</span> <span style="color:#66d9ef">UrlHelper</span>
</span></span><span style="display:flex;"><span>  helper_method <span style="color:#e6db74">:subdomain_from_server</span> <span style="color:#75715e"># allow with method for views (UrlHelper)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">protected</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">subdomain_from_server</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">%w(superapp1 superapp2)</span><span style="color:#f92672">.</span>each <span style="color:#66d9ef">do</span> <span style="color:#f92672">|</span>subdomain<span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">return</span> subdomain <span style="color:#66d9ef">if</span> request<span style="color:#f92672">.</span>env<span style="color:#f92672">[</span><span style="color:#e6db74">&#39;SERVER_NAME&#39;</span><span style="color:#f92672">]</span> <span style="color:#f92672">=~</span> <span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span> <span style="color:#75715e"># as it&#39;s proxied we have to get subdomain from server</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#39;&#39;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>Now when you call eg. articles_path or whatever you should end up with pretty proxy-ready url (that will be broken when you run your app as-it obviously).</p>
<p>I decided to filter content just by using <a href="https://github.com/ryanb/cancan">CanCan</a> (once again thank to Ryan) and setup access depending on current sub-domain in <code>ApplicationController</code> (you can use <code>can</code> and <code>cannot</code> on <code>current_ability</code>).</p>
<p>What&rsquo;s left is authentication - how to setup backend access, log-in curret users - and that&rsquo;s whole different subject. I will point three things here:</p>
<ul>
<li>you can work your way with <a href="http://php.net/manual/en/book.memcached.php">php-memcached</a>, <a href="https://github.com/mperham/dalli">dalli</a> and cookies that are shared across your applications</li>
<li>you can share existing database by setting up additional adapter in config/database.yml (then use <a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection">establish_connection</a> in your model and build up proper relation(s) onto existing database structure)</li>
<li>in the end you probably would want to lock your domains they are accessible only from internal network(s), rewrite below should do it</li>
</ul>
<pre tabindex="0"><code>RewriteEngine On
RewriteCond %{HTTP_HOST} ^superapp1\.rails\.app$
RewriteCond %{REMOTE_ADDR} !^((1\.2\.3\.4)|(127\.0\.0\.1))$ # allowed address(es)
RewriteRule ^.*$ http://domain.com [R]
</code></pre><p>Well, that&rsquo;s it - very generall idea how it can be done, hope you liked this little piece of dark hackery ;-).</p>]]></description><content:encoded><![CDATA[<p><em>Yo dawg, I heard you like&hellip;</em></p>
<p>Purely theoretical (obviously) case: you have existing php app and some legacy rails code that you refreshed a little bit and now someone asks you one simple question - <em>why can&rsquo;t you just put <strong>that</strong> code into <strong>that</strong> code and run it as one app</em>? Sometimes you can reason, sometimes not. But let&rsquo;s do it - I mean seriously - let&rsquo;s create some kind of hybrid that will work transparently and silently for the user. Unleash your imagination and get ready for breaking every programming paradigm; treat it like a sport and get ready :).</p>
<p>Let&rsquo;s ever get a little bit further, let&rsquo;s create one rails app that will distribute its content for two different domains. Where do we start?</p>
<p>I assume you have some kind of working application already, in the end we want to have:</p>
<ul>
<li>domain.com - our main application written in php</li>
<li>domain.com/superapp1 - rails app that is serving some part of content</li>
<li>domain.com/superapp2 - same rails app that is serving another part of content</li>
</ul>
<p>Moreover we want to create this whole structure in a way that it can be ran as separate apps in the future <em>just in case</em>.</p>
<p>First - get familiar with <a href="http://httpd.apache.org/docs/2.2/mod/mod_proxy.html">mod proxy</a> as I&rsquo;ll be using Apache here with with <a href="http://www.modrails.com">phusion passanger</a>. I think the most reasonable approach is to handle two different sub-domains for serving content (plus admin namespace for backend).</p>
<p>So looking at our application it would look like:</p>
<ul>
<li>superapp1.rails.app - for serving content for &lsquo;app1&rsquo;. This url will be not directly accessible on production.</li>
<li>superapp2.rails.app - same as above, for &lsquo;app2&rsquo;</li>
<li>rails.app/admin - handling backend with restricted access</li>
</ul>
<p>Be aware that I can&rsquo;t provide where working example as solutions may vary quite a lot depending on structure of your existing applications and need, but I will give you some ideas if you will need to (theoretically) create that kind of hybrid someday.</p>
<p>Configure phussion passanger for your rails app, don&rsquo;t forget to set <code>PassengerFriendlyErrorPages on</code>  and <code>RailsEnv &quot;development&quot;</code> for now.</p>
<p>Now it&rsquo;s time to start some proxy-action in your php app, setup in your virtual host config:</p>
<pre tabindex="0"><code>ProxyPass /superapp1 http://superapp1.rails.app
ProxyPassReverse /superapp1 http://superapp1.rails.app

ProxyPass /superapp1 http://superapp2.rails.app
ProxyPassReverse /superapp1 http://superapp2.rails.app
</code></pre><p>In your rails routes on the other hand you have to handle those sub-domains, after reading/watching <a href="http://railscasts.com/episodes/221-subdomains-in-rails-3?view=asciicast">this railscasts</a> I ended up with:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># config/routes.rb</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>  constraints(<span style="color:#66d9ef">Subdomain</span>) <span style="color:#66d9ef">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># some resources and stuff</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>  <span style="color:#f92672">...</span>
</span></span><span style="display:flex;"><span>  namespace <span style="color:#e6db74">:admin</span> <span style="color:#66d9ef">do</span>
</span></span><span style="display:flex;"><span>    <span style="color:#75715e"># and here some controllers for handling backend</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>and:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/lib/subdomain.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">Subdomain</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">self</span><span style="color:#f92672">.</span><span style="color:#a6e22e">matches?</span>(request)
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">%w(superapp1 superapp2)</span><span style="color:#f92672">.</span>each <span style="color:#66d9ef">do</span> <span style="color:#f92672">|</span>subdomain<span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span> <span style="color:#66d9ef">if</span> request<span style="color:#f92672">.</span>env<span style="color:#f92672">[</span><span style="color:#e6db74">&#39;SERVER_NAME&#39;</span><span style="color:#f92672">]</span> <span style="color:#f92672">=~</span> <span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">false</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>After reloading apache cofiguration (<code>service apache2 reload</code>) loading address /superapp1 from php app should bring on your brand new ror app.</p>
<p>Its not especially exciting and you may noticed some serious problems here:</p>
<ul>
<li>
<p>you ended up with broken urls when trying to move deeper inside your proxy-ed app - as you need to provide proper urls</p>
</li>
<li>
<p>your assets are broken because you are working really on subdomain, but that can be easily fixed by adding to your apache conf:
<code>ProxyPass /assets http://rails.app/assets</code></p>
</li>
<li>
<p>how can you serve diffirent part of content with same rails application depeding on those domains?</p>
</li>
<li>
<p>what about log in feature for your users?</p>
</li>
</ul>
<p>Lets stard from fixing those urls, one way is to overload <code>url_for</code> method from <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html">UrlHelper</a>. I ended up with something like that:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/helpers/url_helper.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">module</span> UrlHelper
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">url_for</span>(options <span style="color:#f92672">=</span> <span style="color:#66d9ef">nil</span>)
</span></span><span style="display:flex;"><span>    options<span style="color:#f92672">[</span><span style="color:#e6db74">:host</span><span style="color:#f92672">]</span> <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;</span><span style="color:#e6db74">#{</span>request<span style="color:#f92672">.</span>domain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain_from_server<span style="color:#e6db74">}</span><span style="color:#e6db74">&#34;</span> <span style="color:#66d9ef">if</span> options<span style="color:#f92672">.</span>kind_of?(<span style="color:#66d9ef">Hash</span>) <span style="color:#f92672">&amp;&amp;</span> <span style="color:#f92672">!</span>subdomain_from_server<span style="color:#f92672">.</span>empty?
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">super</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-ruby" data-lang="ruby"><span style="display:flex;"><span><span style="color:#75715e"># app/controllers/application_controllers.rb</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ApplicationController</span> <span style="color:#f92672">&lt;</span> <span style="color:#66d9ef">ActionController</span><span style="color:#f92672">::</span><span style="color:#66d9ef">Base</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">include</span> <span style="color:#66d9ef">UrlHelper</span>
</span></span><span style="display:flex;"><span>  helper_method <span style="color:#e6db74">:subdomain_from_server</span> <span style="color:#75715e"># allow with method for views (UrlHelper)</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">protected</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">def</span> <span style="color:#a6e22e">subdomain_from_server</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">%w(superapp1 superapp2)</span><span style="color:#f92672">.</span>each <span style="color:#66d9ef">do</span> <span style="color:#f92672">|</span>subdomain<span style="color:#f92672">|</span>
</span></span><span style="display:flex;"><span>      <span style="color:#66d9ef">return</span> subdomain <span style="color:#66d9ef">if</span> request<span style="color:#f92672">.</span>env<span style="color:#f92672">[</span><span style="color:#e6db74">&#39;SERVER_NAME&#39;</span><span style="color:#f92672">]</span> <span style="color:#f92672">=~</span> <span style="color:#e6db74">/</span><span style="color:#e6db74">#{</span>subdomain<span style="color:#e6db74">}</span><span style="color:#e6db74">/</span> <span style="color:#75715e"># as it&#39;s proxied we have to get subdomain from server</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#39;&#39;</span>
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">end</span>
</span></span></code></pre></div><p>Now when you call eg. articles_path or whatever you should end up with pretty proxy-ready url (that will be broken when you run your app as-it obviously).</p>
<p>I decided to filter content just by using <a href="https://github.com/ryanb/cancan">CanCan</a> (once again thank to Ryan) and setup access depending on current sub-domain in <code>ApplicationController</code> (you can use <code>can</code> and <code>cannot</code> on <code>current_ability</code>).</p>
<p>What&rsquo;s left is authentication - how to setup backend access, log-in curret users - and that&rsquo;s whole different subject. I will point three things here:</p>
<ul>
<li>you can work your way with <a href="http://php.net/manual/en/book.memcached.php">php-memcached</a>, <a href="https://github.com/mperham/dalli">dalli</a> and cookies that are shared across your applications</li>
<li>you can share existing database by setting up additional adapter in config/database.yml (then use <a href="http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection">establish_connection</a> in your model and build up proper relation(s) onto existing database structure)</li>
<li>in the end you probably would want to lock your domains they are accessible only from internal network(s), rewrite below should do it</li>
</ul>
<pre tabindex="0"><code>RewriteEngine On
RewriteCond %{HTTP_HOST} ^superapp1\.rails\.app$
RewriteCond %{REMOTE_ADDR} !^((1\.2\.3\.4)|(127\.0\.0\.1))$ # allowed address(es)
RewriteRule ^.*$ http://domain.com [R]
</code></pre><p>Well, that&rsquo;s it - very generall idea how it can be done, hope you liked this little piece of dark hackery ;-).</p>]]></content:encoded></item></channel></rss>