nginx Directive rewrite is not terminated
Published on October 17, 2014
By Pete Freitag
By Pete Freitag
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/example.com.conf:26
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).
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:
Weekly Security Advisories Email
Advisory Week is a new weekly email containing security advisories published by major software vendors (Adobe, Apple, Microsoft, etc).
Comments
I came across this SO question/answer, http://stackoverflow.com/questions/14684463/curly-braces-and-from-apache-to-nginx-rewrite-rules 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.
So,
rewrite "^/archive/([0-9]{4})/ /archive.cfm?";
So,
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