[Linux] 解决nginx: [emerg] directive "rewrite" is not terminated by ";"

解决nginx: [emerg] directive "rewrite" is not terminated by ";"
nginx的rewite规则有时候没注意会报这个错误,原因是规则中存在{}会被认为是规则结尾报错,使用""双引号把规则包起来可以避免这个错误
还有就是nginx中的规则中/斜杠不必要反斜杠转义\/,自动会认识的

例如:
rewrite "^\/rny\/webface\/mailApps\/(.*)(\/\d{6,})(.*)$" /webface/mailapps/dev/$1$3

.*和.*?的区别:
.*?是非贪婪的,匹配到第一个/就停了
[root@localhost riadev]# echo "/rny/webface/mailApps/xxx/dfdffd/123456_abc.js"|grep -oP "^/rny/webface/mailApps/(.*?)/"
/rny/webface/mailApps/xxx/

.*是贪婪的,匹配到了最后一个/
[root@localhost riadev]# echo "/rny/webface/mailApps/xxx/dfdffd/123456_abc.js"|grep -oP "^/rny/webface/mailApps/(.*)/"
/rny/webface/mailApps/xxx/dfdffd/

\d{6,}这个的意思是最少6个数字,超过的也能匹配到,少的匹配不到
grep是不能捕获()中的内容,在nginx中后面的$就是可以捕获到
[root@localhost riadev]# echo "/rny/webface/mailApps/xxx/dfdffd/123456abc.js"|grep -oP "^/rny/webface/mailApps/(.*)/(\d{6,})(.*)"
/rny/webface/mailApps/xxx/dfdffd/123456abc.js
这个时候的$1和$3分别是xxx/dfdffd 和 abc.js

rewrite "^\/rny\/webface\/mailApps\/(.*)(\/\d{6,})(.*)$" /webface/mailapps/dev/$1$3
会把/rny/webface/mailApps/xxx/dfdffd/123456abc.js 匹配到 /webface/mailapps/dev/xxx/dfdffd/abc.js

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...