neocai 发表于 2018-11-12 08:35:33

nginx之rewrite规则未加引号导致检查nginx语法报错

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

# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf  rewrite ^/.{4}{4}{8}/(.*)$ /uploads/picture/$1 last;
  

  检查语法报错:
  

# /usr/local/nginx/sbin/nginx -t  
nginx: 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误以为这是规则的结尾,产生错误
  解决的办法:把规则段使用单引号或者是双引号引起来
  

# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf  rewrite '^/.{4}{4}{8}/(.*)$' /uploads/picture/$1 last;
  

  或者:
  

# grep rewrite /usr/local/nginx/conf/vhost/img.test.conf  rewrite "^/.{4}{4}{8}/(.*)$" /uploads/picture/$1 last;
  
# /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个字符,{4}任意4个数字,{8}任意8个数字  

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

# grep root /usr/local/nginx/conf/vhost/img.test.conf  root/data/www/images;
  
# ll /data/www/images/uploads/picture/2016/07/28/1469696883.jpeg
  
-rw-r--r--. 1 root root 65824 Jul 282016 /data/www/images/uploads/picture/2016/07/28/1469696883.jpeg
  
# 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
  
#
  

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

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




页: [1]
查看完整版本: nginx之rewrite规则未加引号导致检查nginx语法报错