首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么nginx在我的“`rewrite`”语句中声称没有终止分号?

为什么nginx在我的“`rewrite`”语句中声称没有终止分号?
EN

Stack Overflow用户
提问于 2016-01-17 22:50:37
回答 2查看 3.1K关注 0票数 3

我想重定向一个URL到Django平台(通过uwsgi)当且仅当存在cookie。否则,我需要将执行推迟到content_by_lua插件。

以下是我对这种逻辑的尝试:

代码语言:javascript
复制
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认为用以下日志信息侮辱我的智力是必要的,也是恰当的:

代码语言:javascript
复制
nginx: [emerg] directive "rewrite" is not terminated by ";" in /opt/openresty/nginx/conf/nginx.conf:34

也许我像nginx所想的那样笨,但我错过了什么呢?

的额外问题:是我的一般方法安全和理智吗?有更好的方法来实现我的目标吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-18 00:10:40

额外的答案:您也可以在位置匹配期间捕获UUID值,以避免重写时的额外regex,如下所示:

代码语言:javascript
复制
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)
 ';
}
票数 1
EN

Stack Overflow用户

发布于 2016-01-17 23:33:09

对我来说,这是一件非常愚蠢的事情,我错了。Nginx使用大括号{}来分隔块,因此当这些块用于regexes时,表达式必须用双引号括起来。

票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34844728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档