在nginx的站点配置文件中使用了rewrite,检查rewrite规则确定是没问题,但是在rewrite中带有"{}"时,此时检查nginx语法报错
rewrite规则:

[root@test01 ~]# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf 
   rewrite ^/.{4}[0-9]{4}[0-9]{8}/(.*)$ /uploads/picture/$1 last;

检查语法报错:

[root@test01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] directive "rewrite" is not terminated by ";" in /usr/local/nginx/conf/vhost/img.test.conf:8
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

错误原因:这是由于规则里有大括号“{ }”的符号,Nginx误以为这是规则的结尾,产生错误
解决的办法:把规则段使用单引号或者是双引号引起来

[root@test01 ~]# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf 
   rewrite '^/.{4}[0-9]{4}[0-9]{8}/(.*)$' /uploads/picture/$1 last;

或者:

[root@test01 ~]# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf 
   rewrite "^/.{4}[0-9]{4}[0-9]{8}/(.*)$" /uploads/picture/$1 last;
[root@test01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

此规则简单说明:

.{4}指的是任意4个字符,[0-9]{4}任意4个数字,[0-9]{8}任意8个数字

应用环境说明:
此服务器充当图片服务器,由nginx来提供服务,应用配置环境如下:

[root@test01 ~]# grep root /usr/local/nginx/conf/vhost/img.test.conf 
   root  /data/www/images;
[root@test01 ~]# ll /data/www/images/uploads/picture/2016/07/28/1469696883.jpeg 
-rw-r--r--. 1 root root 65824 Jul 28  2016 /data/www/images/uploads/picture/2016/07/28/1469696883.jpeg
[root@test01 ~]# ll /data/www/images/uploads/picture/2016/07/28/1520924032.png 
-rw-r--r--. 1 root root 89032 Mar 13 14:53 /data/www/images/uploads/picture/2016/07/28/1520924032.png
[root@test01 ~]# 

浏览器请求图片,此时图片正常显示:


http://img.test.com/MTEy114512345678/2016/07/28/1469696883.jpeg
http://img.test.com/MTEy114512345678/2016/07/28/1520924032.png 

nginx之rewrite规则未加引号导致检查nginx语法报错_第1张图片