简体   繁体   中英

Nginx location not working with regex

I would like to catch all urls with this route /al/ or /ca/ etc.

so www.mysite.com/ca/ or www.mysite/al/

I tried

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

but couldn't get it working, not sure how to go about the regex in a location block. Nginx wont start, but when I hard code it to

location ~ /gb/ {
}

it works fine.

You location block contains { and should be quoted. If you test your config with nginx -t command, you'll see error:

# 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

Just put location into quotes and it will work fine.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM