Combine Symphony CMS with Nginx
Nginx is a powerful, lightweight web server, today I'll show you how to rewrite your URLs for a typical Symphony installation.
The first thing to do is create a new location rule in your Nginx configuration, this can be in the main server configuration or in an included configuration file in your web folder. If you didn't configure Nginx yourself, then your hosting provider may give more detailed instructions on what file to edit.
I'm assuming that your Symphony installation is in a sub folder in your web space, if it isn't, don't worry, because I cover that later.
Add this line to your configuration:
location ~ ^(?<path>/symphony/2.1.0)/(?<admin>symphony)?(?<page>.*)$ {Looks a little confusing, I know, so here's a quick breakdown:
location ~- Create a new location that matches this expression
^(?<path>/symphony...)/- Match any URL that starts with
/symphony/2.1.0and assign it to the$pathvariable (?<admin>symphony)?- Optionally match
symphonyand assign it to the$adminvariable (?<page>.*)$- Match anything until the end of the URL and assign it to the
$pagevariable
Essentially, this matches the basic structure of any Symphony URL and lets us run more tests on $path, $admin and $page.
Ignore files
The next thing to do is prevent Nginx from rewriting files that actually exist in your Symphony installation. If we don't do this step, then you won't be able to load and images, stylesheets or scripts on your website:
# Allow access to files:
if (-f $request_filename) {
break;
}This tells Nginx to stop (break) rewriting when the current URL matches a file that exists on the file system (-f).
Image manipulation
If you use Alistairs JIT Image Manipulation extension, then you should add the following rule:
# Just In Time Image Manipulation:
if ($page ~ ^image/(.+\.(jpg|gif|jpeg|png|bmp))$) {
rewrite . $path/extensions/jit_image_manipulation/lib/image.php?param=$1 last;
}Trailing slashes
Some people prefere to force trailing slashes on the and of their URLs, so if you went to http://localhost/symphony/2.1.0/about it would automatically send you to about/. This is easy enough to do:
# Add trailing slashes:
if ($request_filename !~ "/$") {
rewrite ^(.*)$ $1/ redirect;
}However, if you want to remove trailing slashes, then things get a little more interesting. Because Nginx doesn't support nested if statements, or let you test two conditions in one if statement, we find ourselfs doing this:
# Remove trailing slashes:
set $test "";
if ($request_filename ~ "/$") {
set $test "${test}o";
}
if (!-d $request_filename) {
set $test "${test}k";
}
if ($test = "ok") {
rewrite ^(.*)/$ $1 redirect;
}What does this do?
- Check to see if the current URL has a trailing slash, if it does, then add
"o"to$test, - Check to see if the current URL is not a directory, then add
"k"to$test, - Check to make sure
$testis equal to"ok", in which case, redirect to the current URL without a trailing slash.
Deny access
Symphony stores some potentially sensetive files in the manifest folder, you don't want anything inside of it to be publicly visible:
# Block access to manifest:
if ($page ~ "^manifest/") {
return 403;
}Yes, that matches $path when it starts with manifest/ and returns a 403 Forbidden message to the browser.
The main rewrite
Now all that's left to do is send $page to Symphony:
# Access Symphony backend:
if ($admin) {
rewrite . $path/index.php?mode=administration&symphony-page=$page last;
}
# Access Symphony frontend:
if ($page) {
rewrite . $path/index.php?symphony-page=$page last;
}In these two rewrites, we don't actually care about matching anything, as all of the information we need is in the $path, $admin and $page varables.
So, what does the entire location look like?
location ~ ^(?<path>/symphony/2.1.0)/(?<admin>symphony)?(?<page>.*)$ {
# Allow access to files:
if (-f $request_filename) {
break;
}
# Just In Time Image Manipulation:
if ($page ~ ^image/(.+\.(jpg|gif|jpeg|png|bmp))$) {
rewrite . $path/extensions/jit_image_manipulation/lib/image.php?param=$1 last;
}
# Add trailing slashes:
if ($request_filename !~ "/$") {
rewrite ^(.*)$ $1/ redirect;
}
# Block access to manifest:
if ($page ~ "^manifest/") {
return 403;
}
# Access Symphony backend:
if ($admin) {
rewrite . $path/index.php?mode=administration&symphony-page=$page last;
}
# Access Symphony frontend:
if ($page) {
rewrite . $path/index.php?symphony-page=$page last;
}
}Again, the above only works for a Symphony installation in the /symphony/2.1.0 folder of your web server, if your installation is in the root folder, then change the first line to read:
location ~ ^(?<path>)/(?<admin>symphony)?(?<page>.*)$ {Yep, it's that simple.
Share your thoughts...
Brian Zerangue wrote on :
Rowan, you are a genius! You never cease to amaze me! Quite amazing man! Thanks for this tutorial. I've been wanting to test out the Symphony on nginx, and this is awesome!!!!
Rowan Lewis wrote on :
No problem, really.
I needed to find out myself, as I plan on using Nginx on my Linode account. I also needed to improve my technical writing, as I'm probably going to do a lot of it very soon.