日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

实战:Nginx集成Lua脚本并调用memcached

發布時間:2025/3/21 Nginx 58 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实战:Nginx集成Lua脚本并调用memcached 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

之前在阿里云的centos系統上已經安裝過nginx,但是沒有安裝lua所需的模塊,因此重新編譯nginx來擴展lua模塊。

ngx_lua_module 是一個nginx http模塊,它把 lua 解析器內嵌到 nginx,用來解析并執行lua 語言編寫的網頁后臺腳本。這里主要是示范一下,如何在Nginx下安裝lua-nginx-module模塊

?

首先先安裝好:

yum -y install openssl openssl-devel? ? ? ?注意:ubuntu系統安裝openssl-devel 命令:sudo apt-get install libssl-dev

yum install lua-devel

1)首先下載安裝Lua腳本解釋器 LuaJIT

wget?http://luajit.org/download/LuaJIT-2.0.2.tar.gz

tar -zxvf??LuaJIT-2.0.2.tar.gz

cd?LuaJIT-2.0.2

make install PREFIX=/usr/local/LuaJIT

==>在/etc/profile?文件中加入環境變量

#luajit
export LUAJIT_LIB=/usr/local/LuaJIT/lib

export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

2) 下載ngx_devel_kit(NDK)模塊 :https://github.com/simpl/ngx_devel_kit/tags,不需要安裝

cd /usr/local/src
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xzvf v0.2.19.tar.gz

3)下載最新的lua-nginx-module 模塊 :https://github.com/openresty/lua-nginx-module/tags,不需要安裝?

cd /usr/local/src
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz
tar -xzvf v0.10.2.tar.gz

4)通過nginx -V 獲得之前編譯的模塊信息,拷貝下來。

--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'?

5)下載nginx的源碼包

wget?http://nginx.org/download/nginx-1.16.0.tar.gz

tar -zxvf?nginx-1.16.0.tar.gz

cd?nginx-1.16.0
?

6)加入lua模塊,重新編譯安裝nginx

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'??--add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.9rc7
?

7)make & make install

8)? 加載lua庫,加入到ld.so.conf文件

9)? echo "/usr/local/lib" >> /etc/ld.so.conf

10) ldconfig

11)? nginx -V查看

至此nginx加載lua完成;測試一下:

配置如下

訪問:


接下來就是lua調用memcached數據庫;

首先,安裝memcached:yum install memcached

然后,在memchched設置一個key為 “183.6.159.136”,value為1的鍵值對,方便后面測試。

下載lua-resty-memchached,此模塊是lua鏈接memcached數據庫的開發包。下圖中復制的路徑自己決定;

在nginx中配置:

上圖紅色框框是添加庫文件路徑,參考?ttps://blog.csdn.net/langeldep/article/details/8289199

當我們訪問host/luatest時,nginx會去執行dep.lua腳本文件

對應的dep.lua文件:

在lua腳本中,我們鏈接memcached,獲取鍵值,打印輸出給客戶端;

當我們在瀏覽器訪問:

可以正確顯示,至此nginx運行lua腳本,lua腳本連接memcached的實戰完成

?

?

參考:

當然,如果你之前沒有安裝過Nginx,而且嫌安裝麻煩,可直接下載openresty安裝簡單快捷,http://openresty.org/cn/installation.html(阿里的大牛章亦春的作品,膜拜~~~)

https://blog.csdn.net/qq_25551295/article/details/51744815

http://www.imooc.com/article/19597

https://www.imooc.com/article/67583

總結

以上是生活随笔為你收集整理的实战:Nginx集成Lua脚本并调用memcached的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。