nginx之rewrite規則未加引號導致檢查nginx語法報錯
阿新 • • 發佈:2018-08-12
data log sha text upload conf roc water rect 在nginx的站點配置文件中使用了rewrite,檢查rewrite規則確定是沒問題,但是在rewrite中帶有"{}"時,此時檢查nginx語法報錯
rewrite規則:
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語法報錯