zoukankan      html  css  js  c++  java
  • Nginx 设置rewrite规则是遇到的一个{}大括号引发的报错问题

    一个群友提到:

    用nginx image_filter模块裁图,用!拼宽高能够实现,现在想用参数传宽高总是报错,配置如下:

     

    location ~ ^/images/.* {

        if ( $query_string ~ "w=(d+)&h=(d+)" ) {

            set $w $1;

            set $h $2;

            rewrite "/images/(.*)" ${uri}!${w}x${h} last;

        }

    }

    location ~* ^/images/(.*)!(d+)x(d+) {

        set $w $2;

        set $h $3;

        image_filter_jpeg_quality 50;

        image_filter resize $w $h;

        image_filter_buffer 10M;

        try_files /images/$1  404;

    }

     

    在使用这个rewrite规则的时候 主要是想通过query_string传宽和高,通过uri传递是没有问题的。但是报错。

    nginx: [emerg] directive "rewrite" is not terminated by ";"

     

    然后有其他群友点到:

    猜测是引号的问题,加个引号试试

    测试了一下,加双引号就好了~

    8DP631@5TB3IHG[)QM)TN%D

     

    我顺带google了下原因,在使用 rewrite "/images/(.*)" ${uri}!${w}x${h} last;    这段时,你是想表达变量,但是{} 被解析成规则的起始和结束标记,为了安全起见 需要带上引号

     

    其他讨论:

    刚开始用nginx的时候坑过几次,空间快满了,rm掉 nginx的log~~ 结果空间释放不出来。。最后要 lsof 才能发现问题~~

    其他建议:

    location ~* /img/(.+)_(d+)x(d+).(jpg|gif|png)$ {           
        set $h $2;
        set $w $3;
        if ($h = "0") {
            rewrite /img/(.+)_(d+)x(d+).(jpg|gif|png)$ /img/$1.$4 last;
        }
        if ($w = "0") {
            rewrite /img/(.+)_(d+)x(d+).(jpg|gif|png)$ /img/$1.$4 last;
        }
        #根据给定的长宽生成缩略图
        image_filter resize $h $w;
        #原图最大2M,要裁剪的图片超过2M返回415错误,需要调节参数image_filter_buffer
        image_filter_buffer 2M;                         
        #error_page  415              /img/notfound.jpg;
        try_files /img/$1.$4  /img/notfound.jpg;   
    }

    location ~* /img {
    }

    这个rewrite规则作为参考。

  • 相关阅读:
    基于【 Docker】四 || Docker常用镜像安装
    【静态延迟加载】self关键字和static关键字的区别
    【php设计模式】单例模式
    为什么要使用 SPL中的 SplQueue实现队列
    php中连接tcp服务的三种方式
    使用rsync工具构建php项目管理平台
    php 求两个数组的差集应该注意的事情
    lnmp环境快速搭建及原理解析
    nginx + lua 限制ip地址访问
    mysql主从复制搭建
  • 原文地址:https://www.cnblogs.com/logon/p/4235384.html
Copyright © 2011-2022 走看看