Pete Freitag Pete Freitag

nginx Directive rewrite is not terminated


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 ↯

nginx Directive rewrite is not terminated was first published on October 17, 2014.

If you like reading about nginx, apache, rewrite, or regex then you might also like:

Want Security Advisories via Email?

Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).


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.
by Tony Junkes on 10/17/2014 at 9:04:03 PM UTC
Did you try {4,4}?
by Dan G. Switzer, II on 10/17/2014 at 11:41:10 PM UTC
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?";
by Tony Junkes on 10/19/2014 at 3:32:13 AM UTC
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.
by Pete Freitag on 10/22/2014 at 2:58:10 AM UTC