码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-12 14:08:18      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:文件中   directive   rect   RoCE   code   led   not   pre   The   

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

标签:文件中   directive   rect   RoCE   code   led   not   pre   The   

原文地址:http://blog.51cto.com/wujianwei/2158306

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!