Pretty URLS and Nginx

The Elements manual entry for pretty URLs with the CMS assumes Apache hosting (.htaccess file). I use Nginx. Here is what I put into my configuration to enable pretty URLs on my blog section today:

# 1) Redirect /news/posts/?item=slug  -->  /news/posts/slug   (301)
location = /news/posts/ {
    if ($arg_item) { return 301 /news/posts/$arg_item; }
}

# 2) Internally rewrite /news/posts/slug --> /news/posts/index.php?item=slug
#    Avoid loops by excluding index.php (and any *.php).
location ~ ^/news/posts/([A-Za-z0-9_-]+)/?$ {
    rewrite ^ /news/posts/index.php?item=$1 last;
}

When I first wrote an nginx configuration in an attempt to match the suggested .htaccess update, while also adding the manual’s recommended href header template entry, my posts page became unformatted. It was not locating the .css file anymore (though they were at root). The above works and I have no HTML updates (no href addition to the global template header).

Three points to this post:

  1. any suggestions for a better nginx configuration?

  2. did I hurt myself in some long term way by not having the href in the project template header?

  3. Perhaps my config code helps someone else using Nginx as their web server.

Everything seems to be working right now so I move on to other things, but if there is a better way or if I have been shortsighted, let me know. Otherwise, I hope this helps other Nginx users.