【问题标题】:Nginx location not working with regexNginx 位置不适用于正则表达式
【发布时间】:2014-08-12 10:41:25
【问题描述】:

我想用这条路线 /al/ 或 /ca/ 等捕获所有 url。

所以 www.mysite.com/ca/ 或 www.mysite/al/

我试过了

location ~ ^/([a-z]{2})/ {
}

但无法使其正常工作,不确定如何在位置块中处理正则表达式。 Nginx 无法启动,但是当我将其硬编码为

location ~ /gb/ {
}

效果很好。

【问题讨论】:

    标签: regex nginx location


    【解决方案1】:

    您的位置块包含{ 并且应该被引用。如果你使用nginx -t 命令测试你的配置,你会看到错误:

    # nginx -t
    nginx: [emerg] pcre_compile() failed: missing ) in "^/([a-z]" in /etc/nginx/sites-enabled/test:5
    nginx: configuration file /etc/nginx/nginx.conf test failed
    

    只需将位置放在引号中,它就可以正常工作。

    location ~ "^/([a-z]{2})/" {
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 2017-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多