yii nginx fastcgi subdirectory config
Posted Mar 13 2010, 03:30 by William Shallum [updated Mar 13 2010, 03:57]
This is a configuration for use with the yii Framework and the nginx web server. Features of this config:
- Is for use in a subdirectory.
- Supports clean URLs with no “index.php”.
- Puts the yii application directory (protected/) outside the document root so there’s no danger of leaking your app files.
We don’t use rewrites at all, just try to find a file under the public directory, if not found, try going through FastCGI instead. We hardcode the SCRIPT_FILENAME and SCRIPT_NAME to go through the main index.php file.
I’m not sure this works for any PHP app since yii does quite a bit of work to normalize stuff (in CWebRequest class).
The public directory just contains symlinks to the assets, images, and css directory. Note that this is only for a local deployment! If we were going remote (FastCGI server on different host) with this, I’m not sure what we should do with the assets directory since it should be writable by PHP and here we are assuming that it is available locally.
location ^~ /my-yii-app/ {
try_files $uri @yii-php ;
}
location @yii-php {
fastcgi_pass unix:/var/run/php-fpm-socket;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /physical/path/to/my-yii-app/index.php;
fastcgi_param SCRIPT_NAME /my-yii-app/index.php;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_client_abort on;
}
location ~ \.php$ {
# regular php script handling for random php scripts
# This won't be executed for locations inside /my-yii-app/ because /my-yii-app/ is marked as ^~
}
The corresponding snippet for the urlManager config:
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName' => FALSE,
),