{h1: Combine Symphony CMS with {em: Nginx}} {a @href http://wiki.nginx.org/Main: 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 {code: 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: {pre: location ~ ^(?/symphony/2.1.0)/(?symphony)?(?.*)$ \{} Looks a little confusing, I know, so here's a quick breakdown: {dl: {dt: {code: location ~}} {dd: Create a new location that matches this expression} {dt: {code: ^(?/symphony...)/}} {dd: Match any URL that starts with {code: /symphony/2.1.0} and assign it to the {code: $path} variable} {dt: {code: (?symphony)?}} {dd: Optionally match {code: symphony} and assign it to the {code: $admin} variable} {dt: {code: (?.*)$}} {dd: Match anything until the end of the URL and assign it to the {code: $page} variable} } Essentially, this matches the basic structure of any Symphony URL and lets us run more tests on {code: $path}, {code: $admin} and {code: $page}. {h1: 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: {pre: # Allow access to files: if (-f $request_filename) { break; }} This tells Nginx to stop ({code: break}) rewriting when the current URL matches a file that exists on the file system ({code: -f}). {h1: Image manipulation} If you use {a @href http://symphony-cms.com/download/extensions/view/20046/: Alistairs JIT Image Manipulation} extension, then you should add the following rule: {pre: # 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; \}} {h1: Trailing slashes} Some people prefere to force trailing slashes on the and of their URLs, so if you went to {code: http://localhost/symphony/2.1.0/about} it would automatically send you to {code: about/}. This is easy enough to do: {pre: # 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: {pre: # 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? {ol: {li: Check to see if the current URL has a trailing slash, if it does, then add {code: "o"} to {code: $test},} {li: Check to see if the current URL is {em: not} a directory, then add {code: "k"} to {code: $test},} {li: Check to make sure {code: $test} is equal to {code: "ok"}, in which case, redirect to the current URL without a trailing slash.} } {h1: Deny access} Symphony stores some potentially sensetive files in the {code: manifest} folder, you don't want anything inside of it to be publicly visible: {pre: # Block access to manifest: if ($page ~ "^manifest/") \{ return 403; \}} Yes, that matches {code: $path} when it starts with {code: manifest/} and returns a 403 Forbidden message to the browser. {h1: The main rewrite} Now all that's left to do is send {code: $page} to Symphony: {pre: # 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 {code: $path}, {code: $admin} and {code: $page} varables. So, what does the entire {code: location} look like? {pre: location ~ ^(?/symphony/2.1.0)/(?symphony)?(?.*)$ \{ # 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 {code: /symphony/2.1.0} folder of your web server, if your installation is in the root folder, then change the first line to read: {pre: location ~ ^(?)/(?symphony)?(?.*)$ \{} Yep, it's that simple.