阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

nginx+lua_nginx+GraphicsMagick缩略图+tfs获取原图+ngx_cache_purge

143次阅读
没有评论

共计 12511 个字符,预计需要花费 32 分钟才能阅读完成。

环境介绍

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
安装成功

Lua 语言 15 分钟快速入门 http://www.linuxidc.com/Linux/2013-06/86582.htm

Lua 程序设计(第 2 版)中文 PDF http://www.linuxidc.com/Linux/2013-03/81833.htm

Lua 程序设计(第二版)阅读笔记 http://www.linuxidc.com/Linux/2013-03/81834.htm

NetBSD 将支持用 Lua 脚本开发内核组件 http://www.linuxidc.com/Linux/2013-02/79527.htm

CentOS 编译安装 Lua LuaSocket http://www.linuxidc.com/Linux/2011-08/41105.htm

4. 测试 nginx-lua 模块安装是否成功

在 nginx 的配置文件 server 模块添加

location /hello {
      default_type ‘text/plain’;
      content_by_lua ‘ngx.say(“hello, lua”)’;
}

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-07/104897p2.htm

启动测试报错:
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

解决方法三:这才搞定

#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 源码

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 = {“155×155”, “400×400″,”104×104”, “50×50”, “40×40”, “56×56”, “172×172″,”800×600”};
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;
}

}

Lua 的详细介绍:请点这里
Lua 的下载地址:请点这里

环境介绍

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
安装成功

Lua 语言 15 分钟快速入门 http://www.linuxidc.com/Linux/2013-06/86582.htm

Lua 程序设计(第 2 版)中文 PDF http://www.linuxidc.com/Linux/2013-03/81833.htm

Lua 程序设计(第二版)阅读笔记 http://www.linuxidc.com/Linux/2013-03/81834.htm

NetBSD 将支持用 Lua 脚本开发内核组件 http://www.linuxidc.com/Linux/2013-02/79527.htm

CentOS 编译安装 Lua LuaSocket http://www.linuxidc.com/Linux/2011-08/41105.htm

4. 测试 nginx-lua 模块安装是否成功

在 nginx 的配置文件 server 模块添加

location /hello {
      default_type ‘text/plain’;
      content_by_lua ‘ngx.say(“hello, lua”)’;
}

更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-07/104897p2.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-20发表,共计12511字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中