• nginx+lua_nginx+GraphicsMagick生成实时缩略图


    暂做笔记,带后续验证通过后,再补充 1、2、3 步。

    一、安装 lua

       首先确认是否安装 readline

      yum -y install readline-devel ncurses-devel

     进入页面:http://www.lua.org/download.html

    wget http://www.lua.org/ftp/lua-5.3.1.tar.gz  
    
    tar zxvf lua-5.3.1.tar.gz 
    
    #进入解压目录
    make linux && make install
    wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
    tar zxvf LuaJIT-2.0.4.tar.gz 
    cd LuaJIT-2.0.4
    make && make install
    ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
    export LUAJIT_LIB=/usr/local/lib
    export LUAJIT_INC=/usr/local/include/luajit-2.0/

    二、安装 GraphicsMagick

       进入页面:http://www.graphicsmagick.org/1.3/download.html

    tar zxvf GraphicsMagick-1.3.21.tar.gz
    cd GraphicsMagick-1.3.21
    ./configure  --prefix=/usr/local/graphicsmagick
    make && make install

    #验证

       /usr/local/graphicsmagick/bin/gm version

    三、安装nginx

      进入页面:http://tengine.taobao.org/opensource_cn.html

    ./configure --prefix=/usr/local/nginx 
                --dso-path=/usr/local/nginx/modules 
                --with-http_realip_module 
                --with-http_gzip_static_module 
                --with-http_stub_status_module 
                --with-http_concat_module 
                --with-http_lua_module 
                --with-pcre=/usr/local/src/pcre-8.37
                --with-zlib=/usr/local/src/zlib-1.2.8
                --with-openssl=/usr/local/src/openssl-1.0.0s
                --http-proxy-temp-path=/var/tmp/nginx/proxy_temp 
                --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi_temp 
                --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi_temp 
                --http-scgi-temp-path=/var/tmp/nginx/cgi_temp 
                --http-client-body-temp-path=/var/tmp/nginx/client_body_temp 
                --http-log-path=/var/log/nginx/access.log 
                --error-log-path=/var/log/nginx/error.log
                --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"
    
    make && make install
    
    #启动nginx
    #测试配置文件  
    /usr/local/nginx/sbin/nginx -t
    
    #启动
    /usr/local/nginx/sbin/nginx 

     设置开机启动

      

    vi /etc/rc.d/init.d/nginx 

      

    #!/bin/bash
    # Tengine Startup script# processname: nginx
    # chkconfig: - 85 15
    # description: nginx is a World Wide Web server. It is used to serve
    # pidfile: /var/run/nginx.pid
    # config: /usr/local/nginx/conf/nginx.conf
    nginxd=/usr/local/nginx/sbin/nginx
    nginx_config=/usr/local/nginx/conf/nginx.conf
    nginx_pid=/usr/local/nginx/logs/nginx.pid
    RETVAL=0
    prog="nginx"
    # Source function library.
    . /etc/rc.d/init.d/functions
    # Source networking configuration.
    . /etc/sysconfig/network
    # Check that networking is up.
    [ ${NETWORKING} = "no" ] && exit 0
    [ -x $nginxd ] || exit 0
    # Start nginx daemons functions.
    start() {
    if [ -e $nginx_pid ];then
    echo "tengine already running...."
    exit 1
    fi
    echo -n $"Starting $prog: "
    daemon $nginxd -c ${nginx_config}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    return $RETVAL
    }
    # Stop nginx daemons functions.
    stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
    }
    reload() {
    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo
    }
    # See how we were called.
    case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    reload)
    reload
    ;;
    restart)
    stop
    start
    ;;
    
    status)
    status $prog
    RETVAL=$?
    ;;
    *)
    echo $"Usage: $prog {start|stop|restart|reload|status|help}"
    exit 1
    esac
    exit $RETVAL

    保存退出

    chmod 775 /etc/rc.d/init.d/nginx   #赋予文件执行权限
    chkconfig  --level 012345 nginx on   #设置开机启动
    service nginx start

    四、配置 nginx

      nginx.conf

    http
       {
            lua_package_path '/usr/local/openresty/nginx/lua/?.lua;;';
            
            server {
                    listen       80;
                    server_name  img.rhythmk.org;
                    root  /home/wwwroot/static/image;
                    
                    #对类似_100x100.gif/jpg/png/jpeg进行缩略图处理
                    location ~* _([0-9]+)x([0-9]+).(gif|jpg|png|jpeg)$ {                 #匹配文件名规则
                            root  /home/wwwroot/static/image;                             #站点根目录
                            set $image_root /home/wwwroot/static/image;                   #图片目录
                            set $thumbnail_root /home/wwwroot/static/thumbnail;           #缩略图存放目录
                            #如果缩略图文件存在,直接返回
                            set $file $thumbnail_root$uri;
                            if (-f $file) {
                                    rewrite ^/(.*)$ /thumbnail/$1 last;
                            }
                            #如果缩略图文件不存在,则应用缩略图模块处理
                            if (!-f $file) {
                                    rewrite_by_lua_file lua/thumbnail.lua;
                            }
                    }
             }
     
         #include conf/*.conf;
    }

    lua/thumbnail.lua

        local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
        local originalUri = string.sub(ngx.var.uri, 0, index-2);  
        local area = string.sub(ngx.var.uri, index);  
        index = string.find(area, "([.])");  
        area = string.sub(area, 0, index-1);  
      
        local image_sizes = {"80x80", "800x600", "40x40"};  
        function table.contains(table, element)  
           for _, value in pairs(table) do  
              if value == element then  
                 return true  
              end  
           end  
           return false  
        end  
      
        if table.contains(image_sizes, area) then  
            local command = "gm convert " .. ngx.var.image_root .. originalUri .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;  
            os.execute(command);  
            ngx.req.set_uri(ngx.var.uri, true);  
        else  
            ngx.exit(404);  
        end;  
  • 相关阅读:
    [Leetcode] Longest Substring Without Repeating Characters
    [Leetcode] Clone Graph
    [Leetcode] LRU Cache
    行转列
    微信 Demo
    微信开发-step by stemp
    知识库
    SSAS GUID 添加 行计数,非重复计数 等 遇到的莫名其妙的问题
    javascript 前段MVVM 框架
    微信接口开发
  • 原文地址:https://www.cnblogs.com/rhythmK/p/4734673.html
Copyright © 2020-2023  润新知