I’ve recently begun working with Nginx as we’re finally moving off of Apache in our infrastructure. The first thing I noticed is documentation for various types of configurations is a bit sporadic. A lot is outdated and many don’t use best practices.
The first application I’m working on is a CodeIgniter-based PHP site which needs a few rewrite rules to function properly. I dug up this tutorial but it unfortunately uses a lot of if statements which I quickly learned are evil in Nginx.
Below is the configuration that I’ve settled upon (for now) which takes into account some best practices including:
- uses php-fpm for the upstream server via a unix socket
- redirects all http://www.example.com traffic to example.com
- passes all requests under the root to the front-controller as long as they don’t exist (allows nginx to directly serve static files)
- prevents uncontrolled requests from being passed to php
- prevents any access to leftover .htaccess files
I’m hoping to add some of the additional rewrite functionality found in the tutorial at a later date.