设为首页 收藏本站
查看: 1564|回复: 0

[经验分享] nginx+lua_nginx+GraphicsMagick缩略图+tfs获取原图+ngx_cache_purge

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-7-31 09:20:10 | 显示全部楼层 |阅读模式
环境介绍
root@ubuntu-1:~# uname -a
Linux ubuntu-1.230 3.2.0-29-generic #46-Ubuntu SMP Fri Jul 27 17:03:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
root@ubuntu-1:~# cat /etc/issue
Ubuntu 12.04.1 LTS \n \l
root@ubuntu-1:~#
PS:以下操作我只在如上系统操作,仅供参考
1.安装GraphicsMagick
GraphicsMagick 是配合im4java使用的图片处理软件,在linux/windows下均能使用。在ubuntu下安装很简单:
apt-get install graphicsmagick
yum -y install GraphicsMagick GraphicsMagick-devel (Centos安装方法)
然后运行 man gm,如果显示graphicsmagick的manual,则证明graphicsmagick安装成功。
2.安装lua_nginx
下载 LuaJIT-2.0.3.tar.gz  http://luajit.org/download.html
tar -zxvf LuaJIT-2.0.3.tar.gz
cd LuaJIT-2.0.3/
make && make install
所以lib和include是直接放在/usr/local/lib和usr/local/include
3.安装nginx以及需要安装模块
查看现有版本号 nginx -v
下载nginx源码,解压
wget http://www.nginx.org/download/nginx-1.7.3.tar.gz
tar -zxvf nginx-1.7.3.tar.gz
下载ngx_devel_kit HERE 解压
https://github.com/simpl/ngx_devel_kit/tags
tar -zxvf ngx_devel_kit-0.2.19.tar.gz
下载nginx_lua_module HERE 解压
https://github.com/chaoslawful/lua-nginx-module/tags
tar -zxvf lua-nginx-module-0.9.5rc2.tar.gz
下载ngx_cache_purge-1.0.tar.gz
http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar -zxvf ngx_cache_purge-1.0.tar.gz
PS:
这里cache模块最好一起和lua进行编译安装避免后期不必要的麻烦
导入环境变量,编译
vi /etc/profile
export LUAJIT_LIB=/usr/local/lib    #这个很有可能不一样
export LUAJIT_INC=/usr/local/include/luajit-2.0  #这个很有可能不一样
是文件环境变量生效
source /etc/profile
cd nginx-1.7.3
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_spdy_module --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-rpath,$LUAJIT_LIB' --with-ipv6 --add-module=/root/ngx_devel_kit-0.2.19 --add-module=/root/lua-nginx-module-0.9.5rc2
--add-module=/path/to/ngx_devel_kit    #ngx_devel_kit 的源码路径
--add-module=/path/to/lua-nginx-module  #nginx_lua_module 的源码路径
--add-module=/path/to/ngx_cache_purge-1.0.tar.gz

make -j2
make install
报错:
安装nginx报错没有找到pcre包
解决办法:
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载pcre模块
编译nginx的时候添加--with-pcre=pcre源码path
****************************************************************************
这种解决pcre方法网上说,但是我按照安装出错提示,直接指定源码文件  *
安装 pcre报错                                                                                    *
checking windows.h usability... no                                                       *
checking windows.h presence... no                                                     *
checking for windows.h... no                                                              *
configure: error: You need a C++ compiler for C++ support.                   *
解决办法:                                                                                         *
apt-get install build-essential                                                               *
继续安装pcre 继续安装nginx                                                                 *
--with-pcre=pcar-path                                                                         *
编译再次出错:                                                                                   *
checking for OpenSSL library ... not found                                            *
****************************************************************************
继续报错:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
解决办法:
apt-get install libssl-dev
apt-get install openssl
安装成功
4.测试nginx-lua模块安装是否成功
在nginx的配置文件server模块添加
location /hello {
      default_type 'text/plain';
      content_by_lua 'ngx.say("hello, lua")';
}
启动测试报错:
nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
我在网上寻找解决办法,找了三个办法 直接使用方法三,如果还是依旧报错在使用方法一
===========================================================================================
解决方法一
在 Nginx 编译时,需要指定 RPATH,加入下面选项即可:
./configure --with-ld-opt="-Wl,-rpath,$LUAJIT_LIB"  
或者  
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
vi /etc/profile
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
source /etc/profile

解决方法二:
# ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

解决方法三: 这才搞定
http://www.mirrtalk.net/?p=759
#ln -s /usr/local/lib/libluajit-5.1.so.2 /usr/lib/libluajit-5.1.so.2
#ln -s /usr/local/lib/libluajit-5.1.so.2.0.2 /usr/lib/libluajit-5.1.so.2.0.2
再次nginx –t 检查nginx配置  OK
===========================================================================================
4.安装lua
    PS:如果上面安装了luaJit这里依旧需要安装lua,原因还没深究
首先安装lua的依赖包
sudo apt-get install libreadline5
apt-get install libreadline-gplv2-dev
下载lua源码
http://download.iyunv.com/detail/xinsir88/7694571
tar -zxvf lua-5.2.3.tar.gz
cd lua-5.2.3/
make linux
make install
5.配置
nginx nginx-lua GraphicsMagick ngx_cache_purge安装完毕后,我们来修改配置文件 如下:
添加三个个location
第一个location用作图片,第二个location用作html,第三个location用作承接第一个location,第四个location用作承接第三location 内容如下:
server {
    listen       80;
    server_name  localhost;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    location / {
    }
############################################################################################
location /images/ {
set $image_root /usr/local/tfsimage;
     if ($uri ~* "/images/([0-9a-zA-Z_]+).(([^.]+).*)") {
       set $filePath "$image_root/$1.$2";
       set $reqPath  "/$1.$2";
       set $filename "$1.$3";
    }
   set $file "$image_root$reqPath";
   set $fileone "$image_root/$filename";
    if (-f $file) {
       rewrite "/images/(.+)" /innerImages$reqPath last;
   }
        if (!-f $file){
rewrite_by_lua '

//为了避免在nginx服务器上产生太多垃圾图片,在下载原图时先进行判断
function file_exists(name)  //定义方法file_exists
      local f=io.open(name,"r")
      if f~=nil then io.close(f) return true else return false end
      end ;
     if file_exists(ngx.var.fileone) then print("111") else  //调用方法file_exists      
        command = string.format("wget -P /usr/local/tfsimage/http://192.168.1.230/v1/tfs/"..ngx.var.filename)  //定义下载命令
        os.execute(command)//执行下载命令
       end;

                  local originalUri;
                  local area;
                  local index = string.find(ngx.var.filePath, "([0-9]+)x([0-9]+)");
                  if index==nil then     originalUri = ngx.var.filePath;
                  else
           originalUri = string.sub(ngx.var.filePath, 0, index-2);
           area = string.sub(ngx.var.filePath, index);
          index = string.find(area, "([.])");
          area = string.sub(area, 0, index-1);
          end
          local image_sizes = {"155x155", "400x400","104x104", "50x50", "40x40", "56x56", "172x172","800x600"};
          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 " ..  originalUri  .. " -thumbnail " .. area .. " -background white -gravity center -extent " .. area .. " " .. ngx.var.file;
             os.execute(command);
             ngx.req.set_uri("/innerImages" .. ngx.var.reqPath, true);
          else
             ngx.req.set_uri("/innerImages" .. ngx.var.reqPath, true);
          end;
      
       ';
    }
}

location  /html/ {
set $image_root /usr/local/tfshtml;
if ($uri ~* "/html/([0-9a-zA-Z]+).(.*)") {
       set $filePath "$image_root/$1.$2";
       set $reqPath  "/$1.$2";
    }
    if (-f $filePath) {   
        rewrite "/html/(.+)" /innerhtml$reqPath last;
    }
//由于html不涉及到原文件的问题我们就不做判断性下载
    if (!-f $filePath) {
       rewrite_by_lua '  
        command = string.format("wget -P /usr/local/tfshtml/ http://192.168.1.230/v1/tfs/"..ngx.var.reqpath);
        os.execute(command);
        ngx.req.set_uri("/innerhtml" .. ngx.var.reqPath, true);
       ';
    }
}

location /innerImages {
   alias /usr/local/tfsimage;
   expires max;
tcp_nodelay off;
tcp_nopush on;
}
location /innerhtml {
   alias /usr/local/tfshtml;
   expires max;
tcp_nodelay off;
tcp_nopush on;
}
##############################################################################################
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-22978-1-1.html 上篇帖子: Centos5下安装tfs并使用nginx做访问 下篇帖子: Redhat下Nginx安装配置 缩略图
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表