title: Run rails on secure (https) and redirect non secure(http) request to secure through webricks
body: <p>I am using the below environments:</p>
  
  <pre><code>Server: Webrick
  Rails: 3.2.6
  Ruby: 1.9.3p555
  </code></pre>
  
  <p>I have added the below code in /script/rails :</p>
  
  <pre><code>require 'rails/commands/server'
  require 'rack'
  require 'webrick'
  require 'webrick/https'
  
  if ENV['SSL'] == "true"
    module Rails
        class Server &lt; ::Rack::Server
            def default_options
                super.merge({
                    :Port =&gt; 3000,
                    :environment =&gt; (ENV['RAILS_ENV'] || "production").dup,
                    :daemonize =&gt; false,
                    :debugger =&gt; false,
                    :pid =&gt; File.expand_path("tmp/pids/server.pid"),
                    :config =&gt; File.expand_path("config.ru"),
                    :SSLEnable =&gt; true,
                    :SSLVerifyClient =&gt; OpenSSL::SSL::VERIFY_NONE,
                    :SSLPrivateKey =&gt; OpenSSL::PKey::RSA.new(
                                     File.open("certs/project.key").read),
                    :SSLCertificate =&gt; OpenSSL::X509::Certificate.new(
                                     File.open("certs/project.crt").read),
                    :SSLCertName =&gt; [["CN", WEBrick::Utils::getservername]],
                })
            end
        end
    end
  end
  
  APP_PATH = File.expand_path('../../config/application',  __FILE__)
  require File.expand_path('../../config/boot',  __FILE__)
  require 'rails/commands'
  </code></pre>
  
  <p>I have added the below line in /config/environments/production.rb:</p>
  
  <pre><code>config.force_ssl = true
  </code></pre>
  
  <p>Now I have tried the below attempts:</p>
  
  <ol>
  <li><p>Start rails on 3000</p>
  
  <p><code>SSL=true rails s -e production -p 3000</code></p></li>
  </ol>
  
  <p>It runs rails on <a href="https://project.com" rel="nofollow">https://project.com</a> but 404 error on <a href="http://project.com" rel="nofollow">http://project.com</a></p>
  
  <ol start="2">
  <li><p>Start rails on 443 and mentioned the same port in script:</p>
  
  <p><code>rvmsudo rails s -p 443</code></p></li>
  <li><p>Start rails on 80 and 443 both using two different pid:</p>
  
  <p><code>rvmsudo rails s -p 80 -P PID1
   rvmsudo rails s -p 443 -P PID2</code></p></li>
  </ol>
  
  <p>4.Finally I have tried to forward request to 3000 from 443 and 80:</p>
  
  <pre><code>sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000
  sudo iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 3000
  </code></pre>
  
  <p>Also I have tried to run same thing with thin gem as well but results are same.</p>