nginx Directive rewrite is not terminated

October 17, 2014

I have been setting up some sites on nginx today (moving from an apache server) and have been pretty happy with how an Apache rewrite rule like:

RewriteRule /foo/([0-9]+)/ /foo.cfm?id=$1

Can be done in nginx like this:

rewrite /foo/([0-9]+)/ /foo.cfm?id=$1;

This was working great until I ran into this error:

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

But the line referenced did end with a semicolin!

It turns out the problem was that my rewrite rule had {} in it, for example:

rewrite ^/archive/([0-9]{4})/ /archive.cfm?year=$1;

Replacing the {4} with simply a + worked (though is less precise).

Like this? Follow me ↯

You might also like:

2 people found this page useful, what do you think?


I came across this SO question/answer, that refers to wrapping the regex in double quotes to make use of the brackets and eliminate the semicolon error.
Did you try {4,4}?
Not sure my last comment took? but I believe you can avoid the semicolon error and keep the intended regex by wrapping it in double quotes.


rewrite "^/archive/([0-9]{4})/ /archive.cfm?";
Thanks Dan & Tony I didn't look into alternatives too closely but thanks for the suggestions I'll give them a try when I have a min.

Post a Comment


Foundeo Inc.