Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total

regex matching in lighttpd configs

Match the host name and rewrite paths like /images to blah.com/images:

$HTTP["host"] =~ "^.*\.([^.]+\.[^.:]+)(:|^)" {
  url.rewrite = ( "^/images/(.*)" => "/%1/images/$1",
                 "^/files/(.*)" => "/%1/files/$1",
 "^/$" => "index.html", "^([^.]+)$" => "$1.html")
}


provided by Dreamer3 and annotated here so I can quit bugging him about it.

find the http-auth username in rails

in lighttpd
 auth.backend = your_preferred_method
 auth.backend.your_method.userfile = "yourfile.password"
 auth.require = ("/" => ("method" => "digest", "realm" => "your site", "require"=>"valid-user" ) )



in rails
@request.env["HTTP_AUTHORIZATION"].scan(/Digest username=\W+["]?([a-z0-9]+)/i).to_s

running multiple domains off a single rails app with different page caching directories

Problem: You have multiple instances of a rails app that use page caching, but you'd rather not set up a separate instance for each one.

Lighttpd config:
#... normal lighty conf...
server.document-root = "/home/rails/app/public/"
server.error-handler-404 = "/dispatch.fcgi"
fastcgi.server = (
  ".fcgi" =>
    ("localhost" =>
      (
       "min-procs" => 1,
       "max-procs" => 1,
       "socket" => "/tmp/rails-fcgi1.socket",
       "bin-path" => "/home/rails/app/public/dispatch.fcgi",
       "bin-environment" => (
         "RAILS_ENV" => "production",
         "TZ"        => "America/Chicago"
       )
     )
    )
 )

$HTTP["host"] =~ "site1.net" {
  server.document-root = "/home/rails/app/public/site1.net"
}
$HTTP["host"] =~ "site2.net" {
  server.document-root = "/home/rails/app/public/site2.net"
}


Rails code to set the page cache directory:

before_filter { |c| c.class.page_cache_directory = "#{RAILS_ROOT}/public/#{c.request.host}" }


Remember to symlink your necessary files AND your dispatch.fcgi to each page cache directory.

redirecting wordpress xml links to typo in lighttpd

Update: go for the simplest regex possible...

$HTTP["host"] =~ "awesomerailsapp.com" {
  server.error-handler-404 = "/dispatch.fcgi"
  server.document-root = "/awesomerailsapp/public/"
  url.redirect = (
    "^/feed/atom" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    "^/feed" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-rss2\.php" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-atom\.php" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    # my wp article format to typo's
    "^/(\d{4})/(\d{2})/(\d{2})/([\w-_]+)" => "/articles/$1/$2/$3/$4"
  )

  # bring on the rest of the config
}

manual reaping of fcgi processes

These are the signals that SwitchTower's reaper uses:

# reload
kill -s HUP @pid

# graceful
kill -s TERM @pid

# kill
kill -9 @pid

# usr1
kill -s USR1 @pid

Lighttpd Rails Script ( do not require config file)

Based By http://www.bigbold.com/snippets/posts/show/303

 #!/usr/bin/env ruby
 
 require 'optparse'
 require 'fileutils'
 require 'tmpdir'
 
 OPTIONS = {
    :port        => 8000,
    :ip          => "0.0.0.0",
    :daemon      => false,
    :environment => "development",
    :app_name    => Process::pid.to_s,
    :max_procs   => 3,
    :min_procs   => 1,
 }
 
 ARGV.options do |opts|
    script_name = File.basename($0)
    opts.banner = "Usage: ruby #{script_name} [options]"
 
    opts.separator ""
 
    opts.on("-p", "--port=port", Integer,
            "Runs Rails on the specified port.",
            "Default: 8000") { |OPTIONS[:port]| }
    opts.on("-b", "--binding=ip", String,
            "Binds Rails to the specified ip.",
            "Default: 0.0.0.0") { |OPTIONS[:ip]| }
    opts.on("-e", "--environment=name", String,
            "Specifies the environment to run this server under (test/development/production).",
            "Default: development") { |OPTIONS[:environment]| }
    opts.on("-a", "--app-name=name", String,
            "Specifies the application name.",
            "Default: process_id") { |OPTIONS[:app_name]| }
    opts.on("-d", "--daemon",
            "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
            ) { OPTIONS[:daemon] = true }
    opts.on("-n", "--min-procs=number", Integer,
            "Minimum number of FastCGI processes allowed.",
            "Default: 1") { |OPTIONS[:min_procs]| }
    opts.on("-m", "--max-procs=number", Integer,
            "Maximum number of FastCGI processes allowed.",
            "Default: 3") { |OPTIONS[:max_procs]| }
 
    opts.separator ""
 
    opts.on("-h", "--help",
            "Show this help message.") { puts opts; exit }
 
    opts.parse!
 
 end
 
 ENV["RAILS_ENV"] = OPTIONS[:environment]
 RAILS_ROOT = Dir.pwd + "/./"
 TMP_DIR = Dir.tmpdir
 LIGHTTPD_CONF_FILE = TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.conf"
 
 conf = DATA.read
 conf.gsub!('__PORT__', OPTIONS[:port].to_s)
 conf.gsub!('__BINDING__', OPTIONS[:ip])
 conf.gsub!('__RAILS_ROOT__', File.expand_path(RAILS_ROOT))
 conf.gsub!('__APP_NAME__', OPTIONS[:app_name])
 conf.gsub!('__MIN_PROCS__', OPTIONS[:min_procs].to_s)
 conf.gsub!('__MAX_PROCS__', OPTIONS[:max_procs].to_s)
 conf.gsub!('__RAILS_ENV__', ENV['RAILS_ENV'])
 conf.gsub!('__TMP_DIR__', TMP_DIR)
 File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
 
 CMD = "lighttpd -f #{LIGHTTPD_CONF_FILE}"
 CMD << " -D" if not OPTIONS[:daemon]
 
 puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
 puts "=> Ctrl-C to shutdown server; call with --help for options" if not OPTIONS[:daemon]
 
 puts CMD
 `#{CMD}`
 
 FileUtils.rm Dir.glob(TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.*") if not OPTIONS[:daemon]
 
 __END__
 server.port                = __PORT__
 server.bind                = "__BINDING__"
 server.pid-file             = "__TMP_DIR__/lighttpd.__APP_NAME__.pid"
 
 #server.event-handler = "freebsd-kqueue"
 
 server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
 server.document-root        = "__RAILS_ROOT__/public/"
 server.indexfiles           = ( "index.html" ,"dispatch.fcgi")
 accesslog.filename          = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.access.log"
 server.errorlog             = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.error.log"
 server.error-handler-404 = "/dispatch.fcgi"
 
 #### fastcgi module
 
 ## read fastcgi.txt for more info
 fastcgi.server =  (
    ".fcgi" => (
      "__APP_NAME__" => (
        "socket" => "__TMP_DIR__/lighttpd.__APP_NAME__.fcgi.socket",
        "bin-path" => "__RAILS_ROOT__/public/dispatch.fcgi",
        "min-procs" => __MIN_PROCS__,
        "max_procs" => __MAX_PROCS__
      )
    )
 )
 
 
 mimetype.assign             = (
    ".rpm"          =>      "application/x-rpm",
    ".pdf"          =>      "application/pdf",
    ".sig"          =>      "application/pgp-signature",
    ".spl"          =>      "application/futuresplash",
    ".class"        =>      "application/octet-stream",
    ".ps"           =>      "application/postscript",
    ".torrent"      =>      "application/x-bittorrent",
    ".dvi"          =>      "application/x-dvi",
    ".gz"           =>      "application/x-gzip",
    ".pac"          =>      "application/x-ns-proxy-autoconfig",
    ".swf"          =>      "application/x-shockwave-flash",
    ".tar.gz"       =>      "application/x-tgz",
    ".tgz"          =>      "application/x-tgz",
    ".tar"          =>      "application/x-tar",
    ".zip"          =>      "application/zip",
    ".mp3"          =>      "audio/mpeg",
    ".m3u"          =>      "audio/x-mpegurl",
    ".wma"          =>      "audio/x-ms-wma",
    ".wax"          =>      "audio/x-ms-wax",
    ".ogg"          =>      "audio/x-wav",
    ".wav"          =>      "audio/x-wav",
    ".gif"          =>      "image/gif",
    ".jpg"          =>      "image/jpeg",
    ".jpeg"         =>      "image/jpeg",
    ".png"          =>      "image/png",
    ".xbm"          =>      "image/x-xbitmap",
    ".xpm"          =>      "image/x-xpixmap",
    ".xwd"          =>      "image/x-xwindowdump",
    ".css"          =>      "text/css",
    ".html"         =>      "text/html",
    ".htm"          =>      "text/html",
    ".js"           =>      "text/javascript",
    ".asc"          =>      "text/plain",
    ".c"            =>      "text/plain",
    ".conf"         =>      "text/plain",
    ".text"         =>      "text/plain",
    ".txt"          =>      "text/plain",
    ".dtd"          =>      "text/xml",
    ".xml"          =>      "text/xml",
    ".mpeg"         =>      "video/mpeg",
    ".mpg"          =>      "video/mpeg",
    ".mov"          =>      "video/quicktime",
    ".qt"           =>      "video/quicktime",
    ".avi"          =>      "video/x-msvideo",
    ".asf"          =>      "video/x-ms-asf",
    ".asx"          =>      "video/x-ms-asf",
    ".wmv"          =>      "video/x-ms-wmv",
    ".bz2"          =>      "application/x-bzip",
    ".tbz"          =>      "application/x-bzip-compressed-tar",
    ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
   )

WordPress with clean urls on Lighttpd

This single line seems to work for my WP install. I needed to add a couple of redirects and rewrites to handle my RSS feed, since I use Feedburner, but this line does all the heavy lifting...

Place it after server.document-root, either in the main section or in your virtual host sub-sections...

server.error-handler-404 = "/index.php?error=404"


and use this as your clean urls template

/%year%/%monthnum%/%day%/%postname%/

Typo under lighttpd

Example vhost of a typo install under lighttpd

$HTTP["host"] =~ "typo.jasonhoffman.org" {
server.document-root = "/home/jah/apps/typo/trunk/public/"
server.error-handler-404   = "/dispatch.fcgi"
server.indexfiles           = ( "dispatch.fcgi")
server.errorlog = "/home/jah/logs/error.typo.log"
  fastcgi.server = ( ".fcgi" =>
     ( "localhost" =>
       ( "socket" => "/home/jah/tmp/typo-jah.socket",
                            "min-procs" => 1,
                            "max-procs" => 5,
         "bin-path" => "/home/jah/apps/typo/trunk/public/dispatch.fcgi",
         "bin-environment" => ( "RAILS_ENV" => "production" )
  )))
}

Automatic sub-domains with lighttpd mapping to a specific folder

Using lighttpd's mod_evhost with

# %% => % sign
# %0 => domain name + tld
# %1 => tld
# %2 => domain name without tld
# %3 => subdomain 1 name
# %4 => subdomain 2 name
#


$HTTP["host"] =~ "jasonhoffman.org" {
evhost.path-pattern        = "/home/jah/public/%3/"
}

My lighttpd php-fastcgi config with it's own php.ini

fastcgi.server = (
                ".php" =>
                    ( "localhost" =>
                        (
                            "socket" => "/home/jah/tmp/jah-php5-fcgi.socket",
                            "bin-path" => "/usr/local/www/cgi-bin/php5-fcgi -c /home/jah/etc/php.ini",
                            "bin-environment" => (
                            "PHP_FCGI_CHILDREN" => "32",
                            "PHP_FCGI_MAX_REQUESTS" => "5000"
                                                 )
                        )
                    )
                 )
« Newer Snippets
Older Snippets »
Showing 11-20 of 22 total