nginx+lua+GraphicsMagick实现图片自动缩放功能(强烈推荐)

时间:2022-11-25 10:14:49
详细配置步骤:
lua+nginx+GraphicsMagick 

环境: CentOSsh5.4_x64.iso 

虚拟机: Vmware9.0

yum install readline-devel pcre-devel openssl-devel
cd /opt/Source
wget http://agentzh.org/misc/nginx/ngx_openresty-1.0.15.10.tar.gz
useradd -r www
tar vxzf ngx_openresty-1.0.15.10.tar.gz 
cd ngx_openresty-1.0.15.10
./configure --user=www --group=www --prefix=/usr/local/openresty \
            --with-luajit \
            --without-http_redis2_module \
            --with-http_iconv_module 
make -j4   # -j后面的cpu核心数,如果是多核可以写相应的个数
make install
 
  安装jpg环境:

tar xf libjpeg-6b.tar.gz
./configure 
make && make install 

tar xf libpng-1.4.7.tar.gz

./configure
make && make install 
注意: 64位系统需要拷贝
cp /usr/local/lib/libpng14.so.14 /lib64/   或者做软连接,不然执行gm命令的时候会报错!
   
cd   /opt/Source 
wget http://downloads.sourceforge.net/project/graphicsmagick/graphicsmagick/1.3.16/GraphicsMagick-1.3.16.tar.gz
tar vxzf GraphicsMagick-1.3.16.tar.gz 
cd GraphicsMagick-1.3.16
./configure
make&& make install

配置nginx nginx.conf
#user   nobody;
worker_processes   1;
#error_log   logs/error.log;
#error_log   logs/error.log   notice;
#error_log   logs/error.log   info;
#pid         logs/nginx.pid;
events {
    worker_connections   1024;
}
http {
    include       mime.types;
    default_type   application/octet-stream;


    #log_format   main   '$remote_addr - $remote_user [$time_local] "$request" '
    #                   '$status $body_bytes_sent "$http_referer" '
    #                   '"$http_user_agent" "$http_x_forwarded_for"';


    #access_log   logs/access.log   main;


    sendfile         on;
    #tcp_nopush     on;


    #keepalive_timeout   0;
    keepalive_timeout   65;


    #gzip   on;


    server {
        listen       80;
        server_name   localhost;


        #charset koi8-r;


        #access_log   logs/host.access.log   main;


        location / {
            root   html;
            index   index.html index.htm;
        }
location /lua1 {
  default_type 'text/plain';
                    rewrite_by_lua 'ngx.say("hello, lua")';
                }
location /upload/   {
                set $image_root /usr/local/openresty/nginx/html;
                set $file "$image_root$uri";
                if (!-f $file) {
                        rewrite_by_lua '
                                local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");
                                if (index == nil) then