本文共 6408 字,大约阅读时间需要 21 分钟。
OpenResty® is a full-fledged web platform that integrates the standard Nginx core, LuaJIT, many carefully written Lua libraries, lots of high quality 3rd-party Nginx modules, and most of their external dependencies. It is designed to help developers easily build scalable web applications, web services, and dynamic web gateways.
mkdir -p /usr/servers cd /usr/servers/yum install -y readline-devel pcre-devel openssl-devel gccwget http://openresty.org/download/ngx_openresty-1.7.7.2.tar.gz tar -xzvf ngx_openresty-1.7.7.2.tar.gz cd /usr/servers/ngx_openresty-1.7.7.2/cd bundle/LuaJIT-2.1-20150120/ make clean && make && make install ln -sf luajit-2.1.0-alpha /usr/local/bin/luajitcd bundle wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz tar -xvf 2.3.tar.gz cd bundlewget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz tar -xvf v0.3.0.tar.gz cd /usr/servers/ngx_openresty-1.7.7.2 ./configure --prefix=/usr/servers --with-http_realip_module --with-pcre --with-luajit --add-module=./bundle/ngx_cache_purge-2.3/ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ -j2 make && make install cd /usr/servers/ ll/usr/servers/luajit/usr/servers/lualib/usr/servers/nginx/usr/servers/nginx/sbin/nginx -V 启动nginx: /usr/servers/nginx/sbin/nginx
vi /usr/servers/nginx/conf/nginx.conf//在http部分添加:lua_package_path "/usr/servers/lualib/?.lua;;"; lua_package_cpath "/usr/servers/lualib/?.so;;"; include lua.conf;----------/usr/servers/nginx/conf下,创建一个lua.confserver { listen 80; server_name _; location /lua { default_type 'text/html'; content_by_lua 'ngx.say("hello world")'; } }----------//验证配置文件是否正确/usr/servers/nginx/sbin/nginx -t//重新nginx加载配置/usr/servers/nginx/sbin/nginx -s reload //访问http: http://192.168.1.107/lua//查看异常日志tail -f /usr/servers/nginx/logs/error.log
hello hello.conf lua hello.lua lualib *.lua *.so
/usr/servers/nginx/conf/nginx.conf//在http下加入 lua_package_path "/usr/hello/lualib/?.lua;;"; lua_package_cpath "/usr/hello/lualib/?.so;;"; include /usr/hello/hello.conf;cp -r /usr/servers/lualib/ /usr/hello//usr/hello/hello.confserver { listen 80; server_name _; location /lua { default_type 'text/html'; content_by_lua_file /usr/hello/lua/hello.lua; } }/usr/hello/lua/hello.lua ngx.say("hello world!!!");
cd /usr/hello/lualib/resty/ wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua ----------vi /usr/hello/lua/hello.lualocal uri_args = ngx.req.get_uri_args()local productId = uri_args["productId"]local host = { "192.168.1.107", "192.168.1.104"}local hash = ngx.crc32_long(productId)hash = (hash % 2) + 1 backend = "http://"..host[hash]local requestPath = uri_args["requestPath"]requestPath = "/"..requestPath.."?productId="..productIdlocal http = require("resty.http") local httpc = http.new() local resp, err = httpc:request_uri(backend, { method = "GET", path = requestPath})if not resp then ngx.say("request error :", err) return endngx.say(resp.body) httpc:close()
//安装相应的组件:cd /usr/hello/lualib/resty/ wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua wget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.lua//nginx利用获取到的数据,动态渲染网页模板:mkdir /usr/hello/lualib/resty/htmlcd /usr/hello/lualib/resty/htmlwget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua//在hello.conf的server中配置模板的位置:vi /usr/hello/hello.confset $template_location "/templates"; set $template_root "/usr/hello/templates";vi /usr/servers/nginx/conf/nginx.conf//在http下面添加lua_shared_dict my_cache 128m; //设置nginx缓存容量,存储满后会使用LRU清除算法mkdir /usr/hello/templatescd /usr/hello/templatesvi product.htmlproduct id: {* productId *}product name: {* productName *}product picture list: {* productPictureList *}product specification: {* productSpecification *}product service: {* productService *}product color: {* productColor *}product size: {* productSize *}shop id: {* shopId *}shop name: {* shopName *}shop level: {* shopLevel *}shop good cooment rate: {* shopGoodCommentRate *}//添加业务lua://当请求由分发nginx过来之后,首先查看nginx是否有对应的缓存数据,如果有则直接返回,否则发起http请求去获取(redis集群)数据,然后设置到nginx缓存中并返回local uri_args = ngx.req.get_uri_args()local productId = uri_args["productId"]local shopId = uri_args["shopId"]local cache_ngx = ngx.shared.my_cachelocal productCacheKey = "product_info_"..productIdlocal shopCacheKey = "shop_info_"..shopIdlocal productCache = cache_ngx:get(productCacheKey)local shopCache = cache_ngx:get(shopCacheKey)if productCache == "" or productCache == nil then local http = require("resty.http") local httpc = http.new() local resp, err = httpc:request_uri("http://192.168.1.101:8080",{ method = "GET", path = "/getProductInfo?productId="..productId }) productCache = resp.body cache_ngx:set(productCacheKey, productCache, 10 * 60)endif shopCache == "" or shopCache == nil then local http = require("resty.http") local httpc = http.new() local resp, err = httpc:request_uri("http://192.168.1.101:8080",{ method = "GET", path = "/getShopInfo?shopId="..shopId }) shopCache = resp.body cache_ngx:set(shopCacheKey, shopCache, 10 * 60)endlocal cjson = require("cjson")local productCacheJSON = cjson.decode(productCache)local shopCacheJSON = cjson.decode(shopCache)local context = { productId = productCacheJSON.id, productName = productCacheJSON.name, productPrice = productCacheJSON.price, productPictureList = productCacheJSON.pictureList, productSpecification = productCacheJSON.specification, productService = productCacheJSON.service, productColor = productCacheJSON.color, productSize = productCacheJSON.size, shopId = shopCacheJSON.id, shopName = shopCacheJSON.name, shopLevel = shopCacheJSON.level, shopGoodCommentRate = shopCacheJSON.goodCommentRate}local template = require("resty.template")template.render("product.html", context)
转载地址:http://whrgi.baihongyu.com/