Symfony and lighttpd

Update: I have written a tool than can generate automatically the proper configuration and much more!

I’ve seen some articles on how to configure lighttpd to serve a Symfony project, however they usually did at least one mistake:

  • Assuming that requests with periods (‘.‘) are for static files (the period is a default separator in Symfony, and is extensively used in the new admin generators).
  • Ignoring parameters after a ‘?‘ (they are not widely used, except… in the new admin generators, and can be very useful if your application)

For the first part, there is a much simpler solution to handle static files: most of them are in specific directories, except for a very limited number of ones.

My solution also handles assets published by plugins (you might want to edit the corresponding line to a more liberal one though).

You might want to add your sitemap.xml.gz or robots.txt to this list if you generate them statically.

For the second part, you simply have to match explicitly the ‘?‘ part.

Here is the magic:

alias.url = (
  "/sf/" => "/home/web/symfony_12/data/web/sf/"
)
 
url.rewrite-once = (
  "^/css/.+" => "$0", # directories with static files
  "^/js/.+" => "$0",
  "^/images/.+" => "$0",
  "^/uploads/.+" => "$0",
  "^/favicon\.ico$" => "$0", # static file example
  "^/sf[A-z]+Plugin.*" => "$0", # plugins
  "^/sf/.+" => "$0", # symfony assets
  "^/backend\.php(/[^\?]*)(\?.*)?" => "/backend.php$1$2", # allow access to another application
  "^(/[^\?]*)(\?.*)?" => "/index.php$1$2" # default application
)

I guess the usage of periods in the rules also had the benefit of allowing the access to any alternative application automatically. With my solution you have to add each appname.php file manually, unless you use:

  "^/([a-z]+)\.php(/[^\?]*)(\?.*)?" => "/$1.php$2$3", # any app (prod)

Or for allowing any environment:

  "^/([a-z_]+)\.php(/[^\?]*)(\?.*)?" => "/$1.php$2$3", # any app (any env)

Note: your application must contain only lowercase letters, but you’re free to adapt it to your own usage.

This entry was posted in Symfony, Sysadmin and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted 2009-04-13 at 1258 | Permalink
    Safari 3.2.1 Safari 3.2.1 Mac OS X  10.5.6 Mac OS X 10.5.6
    Lighttpd 1.4.23 will include a handy helper to do a rewrite based on whether or not a file exists, which means you will be able to take all those static file rules out. See: http://redmine.lighttpd.net/issues/985
  2. Posted 2009-04-13 at 1658 | Permalink
    Firefox 3.0.8 Firefox 3.0.8 Ubuntu 8.04 Ubuntu 8.04
    Thanks for the information.
    I always felt like the absence of a “file exists” rule was a feature of lighttpd. I don’t think it’s that a good idea . But it will make hosting some applications much easier; I’ve seen some horrific rewriting examples ;-).
  3. Posted 2009-05-08 at 1713 | Permalink
    Firefox 3.0.10 Firefox 3.0.10 Windows Vista Windows Vista
    Maybe this will finally explain all the difficulties I’ve had working with querystrings that no one else was able to understand… I never thought to look in the lighttpd.conf … argh!

    Thanks for the post.

    By the way, I just released a new plugin for dealing with domains and subdomains in an elegant way straight from the routing system… It’s called sfDomainRoutePlugin (http://www.symfony-project.org/plugins/sfDomainRoutePlugin) and it lets you do some fancy things with subdomains, or domains in an elegant way. The kind of things that most of us had to resort to url rewrite hacks for.

    For example, you can restrict rules to certain domains or subdomains (e.g. http://www.site.com and blog.site.com will go to different routes in the same application) , or even pass parameters in the subdomain (e.g. username.site.com) or even in the domain (e.g. PS2News.com vs. XboxNews.com).

  4. brainfault
    Posted 2009-10-07 at 0925 | Permalink
    Firefox 3.5.3 Firefox 3.5.3 Windows XP Windows XP
    what about disabling php execution in the upload folder ?
  5. Posted 2009-10-07 at 1036 | Permalink
    Firefox 3.5.3 Firefox 3.5.3 Ubuntu 9.10 Ubuntu 9.10
    Interesting idea. I’d put the fastcgi.server line inside of a $HTTP["url"] conditional (I already have one fastcgi.server line per $HTTP["host"] and it works great).

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*
*