Nginx URL Rewrite Error

Recently when I updating this blog system with new URL rewrite rules, I'm getting error:

Restarting nginx: [emerg]: directive "rewrite" is not terminated by ";" in /etc/nginx/sites-enabled/

After looking through all the rules, I found that these error only occured in rules with length limiter, such as:

rewrite ^/(2[0-9]{3})/([0-9]{2})/$ /index.php?q=archives&year=$1&month=$2 last;

The limiter, the limiters in the regular expression {3}, {2} are throwing these errors. After a quick check from Google, I found that all rewrite rules with length limiter has to quoted. So, the correct way to write URL rewrite in Nginx shoud be this:

rewrite "^/(2[0-9]{3})/([0-9]{2})/$" /index.php?q=archives&year=$1&month=$2 last;

Hope this can help someone else.

Leave a Comment

Your email address will not be published. Required fields are marked *