Why is nginx claiming there's no terminating semicolon in my `rewrite` statement?
Asked Answered
I

2

7

I'd like to redirect a URL to a Django platform (via uwsgi) if and only if a cookie exists. Failing that, I need to defer execution to the content_by_lua plugin.

Below is my attempt at such logic:

location ~* "^/[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$" {  # match a UUID v4
    include uwsgi_params;
    if ($cookie_admin) {
        # if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
        rewrite ^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$ /modif/$1 break; 
        uwsgi_pass frontend;
    }
    content_by_lua '
        ngx.say("Ping!  You got here because you have no cookies!")
    ';
}

Nginx has deemed it necessary and proper to insult my intelligence with the following log message:

nginx: [emerg] directive "rewrite" is not terminated by ";" in /opt/openresty/nginx/conf/nginx.conf:34

Perhaps I am as dense as nginx seems to think, but what have I missed?

Bonus question: Is my general approach safe and sane? Is there a better way of achieving my goals?

Impenetrable answered 17/1, 2016 at 22:50 Comment(0)
R
1

Bonus answer: Also you could just capture the UUID value during the location matching to avoid the additional regex on the rewrite, like this:

location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$" {  # match and capture a UUID v4
  include uwsgi_params;
  set $uuid $1;
  if ($cookie_admin) {
    # if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
    rewrite / /modif/$uuid break; 
    uwsgi_pass frontend;
  }
  content_by_lua '
    ngx.say("Ping!  You got here because you have no cookies!")
    ngx.say("UIID: " .. ngx.var.uuid)
 ';
}
Rabies answered 18/1, 2016 at 0:10 Comment(1)
This is, indeed, much better. Thank you!Impenetrable
I
21

This is actually a very silly thing for me to have gotten wrong. Nginx uses curly-braces {} to delimit blocks, so when these are employed in regexes, the expression must be enclosed in double-quotes.

Impenetrable answered 17/1, 2016 at 23:33 Comment(0)
R
1

Bonus answer: Also you could just capture the UUID value during the location matching to avoid the additional regex on the rewrite, like this:

location ~* "^/([0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})$" {  # match and capture a UUID v4
  include uwsgi_params;
  set $uuid $1;
  if ($cookie_admin) {
    # if cookie exists, rewrite /<uuid> to /modif/<uuid> and pass to uwsgi
    rewrite / /modif/$uuid break; 
    uwsgi_pass frontend;
  }
  content_by_lua '
    ngx.say("Ping!  You got here because you have no cookies!")
    ngx.say("UIID: " .. ngx.var.uuid)
 ';
}
Rabies answered 18/1, 2016 at 0:10 Comment(1)
This is, indeed, much better. Thank you!Impenetrable

© 2022 - 2024 — McMap. All rights reserved.