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

在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语法报错

转载于:https://blog.51cto.com/wujianwei/2158306

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值