https for rails 1

Posted by Joel Jensen Tue, 28 Aug 2007 05:27:00 GMT

Heres some tips for rails https.

First – use the plugin SslRequirement. But REMEMBER this ONLY works on controllers that are listed in the routes.rb file as RESOURCES

map.resources :blahblah

IE they only work on resources. Period. For non resource controllers, you will have to stick it in the links.

<%= button_to "Checkout", {:action => :checkout,:controller=>"catalog",:protocol=>"https://",:only_path=>false },{}%>

Here is how to use the ssl plugin

ruby script/plugin install ssl_requirement


class ApplicationController < ActiveRecord::Base
    include SslRequirement
end


class AccountController < ApplicationController
    ssl_required :signup, :payment
    ssl_allowed :index

    def signup
      # Non-SSL access will be redirected to SSL
    end

    def payment
      # Non-SSL access will be redirected to SSL
    end

    def index
      # This action will work either with or without SSL
    end

    def other
      # SSL access will be redirected to non-SSL
    end
end

Next Here is a working apache / mongrel config for a https ssl virtualhost. REMEMBER enable proxy, rewrite, and headers in the apache modules stanza

<IfDefine SSL>
<IfDefine !NOSSL>
<VirtualHost 192.168.1.42:443>
    DocumentRoot /srv/www/site-name/current/public
    ServerName www.site-name.com:443
    ServerAdmin webmaster@site-name.com
    ErrorLog /var/log/apache2/site-name.com-error_log
    CustomLog  /var/log/apache2/site-name.com-access_log combined
    HostnameLookups Off
    UseCanonicalName Off
    ServerSignature Off

    SSLEngine on
    SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL 
    SSLCertificateFile    /etc/apache2/site-name-ssl/www.site-name.com.crt
    SSLCertificateKeyFile /etc/apache2/site-name-ssl/site-name.key
    SSLCertificateChainFile  /etc/apache2/site-name-ssl/gd_intermediate_bundle.crt
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0

    <Directory "/srv/www/site-name/current/public">
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

    RewriteEngine On


    # Don't do forward proxying
    ProxyRequests Off

    # Enable reverse proxying
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>


    RequestHeader set X_FORWARDED_PROTO 'https'

    RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
    RewriteCond %{SCRIPT_FILENAME} !maintenance.html
    RewriteRule ^.*$ /system/maintenance.html [L]

    RewriteRule ^/$ /index.html [QSA] 
    RewriteRule ^([^.]+)$ $1.html [QSA]


    # Redirect all non-static requests to cluster
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
    RewriteRule .* http://127.0.0.1:3000%{REQUEST_URI} [L,P,QSA]

    # Deflate
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/xml application/xhtml+xml text/javascript 
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

</VirtualHost>                                  

</IfDefine>
</IfDefine>

My pain is your gain.

Send HUGE files from rails

Posted by Joel Jensen Fri, 23 Mar 2007 12:11:00 GMT

I had to send large files with rails, so huge that it was killing rails.

Here is the solution, note the allow-x-send-file NOT allow-x-sendfile, this wasted a few hours of mine.

Make sure that “mod_fastcgi” is in the server.modules stanza of the lighttpd.conf file

Here is the vhosts.conf file

$HTTP["host"] =~ "www.yoursite.com" {
    server.document-root        = base + "/domains/yoursite.com/web/public/"
    server.error-handler-404 = "/dispatch.fcgi"
    compress.filetype        = ( "text/plain", "text/html", "text/css", "text/javascript" )
    compress.cache-dir       = base + "/domains/yoursite.com/web/tmp/cache"
    fastcgi.server = ( ".fcgi" =>
        ( "localhost" =>
            ( "socket" => base + "/var/run/yoursite-0.socket",
                "allow-x-send-file" => "enable",
                "bin-environment"   => ("RAILS_ENV" => "production")
            )
        )
    )
}

rails controller code

def send_this_file
    # this will force rails to read the entire file then stream it out BAD
    #send_file "#{RAILS_ROOT}/public/pdf/#{params[:id]}" , :type => mime_for(params[:id]), :disposition => 'inline', :stream => false

    # this will pass the headers to lighttpd and force it to handle the large static file, rails doesn't read the large file.
    filename = "#{RAILS_ROOT}/public/pdf/#{params[:id]}"
    response.headers['Content-Type'] = "application/force-download"
    response.headers['Content-Disposition'] = "attachment; filename=\"#{File.basename(filename)}\"" 
    response.headers["X-LIGHTTPD-send-file"] = filename
    response.headers['Content-length'] = File.size(filename)
    render :nothing => true
end

Rails Capistano Deployment

Posted by Joel Jensen Mon, 29 May 2006 23:05:00 GMT

I got Capistrano to work. Deployments now are a one line thing.

The issue I had was that the subversion repositroy I had used was http://localhost, this wont work since localhost in this context actually referrs to localhost of the remote machine that capistrano is ssh’d into.

I set up a external SVN repository and it was golden. Except for database migrations.

Capistrano will not execute commands on your local machine as far as I can tell.

Data Migrations in rails only migrate the schema changes, Yeah you can programmaticly add data via ruby. But this is a pain when you have a million rows of data.

I solved it by doing a mysqldump of the data, gzip it to save time, and use the capistrano “put” function to upload the data, then insert it into mysql on the server end.

Its not as clean as simply “rake deploy” but I can run 2 additional commands if I need.