我想重定向一个URL到Django平台(通过uwsgi)当且仅当存在cookie。否则,我需要将执行推迟到content_by_lua插件。
以下是我对这种逻辑的尝试:
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认为用以下日志信息侮辱我的智力是必要的,也是恰当的:
nginx: [emerg] directive "rewrite" is not terminated by ";" in /opt/openresty/nginx/conf/nginx.conf:34也许我像nginx所想的那样笨,但我错过了什么呢?
的额外问题:是我的一般方法安全和理智吗?有更好的方法来实现我的目标吗?
发布于 2016-01-18 00:10:40
额外的答案:您也可以在位置匹配期间捕获UUID值,以避免重写时的额外regex,如下所示:
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)
';
}发布于 2016-01-17 23:33:09
对我来说,这是一件非常愚蠢的事情,我错了。Nginx使用大括号{}来分隔块,因此当这些块用于regexes时,表达式必须用双引号括起来。
https://stackoverflow.com/questions/34844728
复制相似问题