centos7+nginx支持lua模块+OpenResty
创始人
2024-06-03 00:56:54
0

前言

不要使用nginx添加 lua 模块进行编译安装!
nginx 中添加 lua 模块,支持lua脚本以及遇到的坑
比如添加一个lua-resty-jwt令牌校验工具,你需要重新与ngnix进行整合,上面文章只是ngnix和lua的整合
推荐使用OpenResty

安装OpenResty

OpenResty官网
推荐使用官方预编译包安装(发布在yum上面的包)
在这里插入图片描述

添加yum仓库

# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/
# update the yum index:
sudo yum check-update

安装opm

安装openresty的包管理工具opm,非常重要,后续通过opm方式下载需要lua所需的工具包

 yum install openresty-opm

OpenResty和Ngnix使用是一样的


# 查看版本和安装的模块
/usr/local/openresty/nginx/sbin/nginx -V
#检查配置
/usr/local/openresty/nginx/sbin/nginx -t
#启动
/usr/local/openresty/nginx/sbin/nginx
#重新加载配置
/usr/local/openresty/nginx/sbin/nginx -s reload
#停止
/usr/local/openresty/nginx/sbin/nginx -s stop

在ngnix执行lua,校验安装成功

    server {listen 8080;location / {default_type text/html;content_by_lua_block {ngx.say("

hello, world

")}}}

lua jwt校验

使用opm安装jwt工具

opm get SkyLothar/lua-resty-jwt

运行lua-resty-jwt示例

lua-resty-jwt官方地址


#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;
#        }
#
#        #error_page  404              /404.html;
#
#        # redirect server error pages to the static page /50x.html
#        #
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }
#
#        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#        #
#        #location ~ \.php$ {
#        #    proxy_pass   http://127.0.0.1;
#        #}
#
#        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#        #
#        #location ~ \.php$ {
#        #    root           html;
#        #    fastcgi_pass   127.0.0.1:9000;
#        #    fastcgi_index  index.php;
#        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#        #    include        fastcgi_params;
#        #}
#
#        # deny access to .htaccess files, if Apache's document root
#        # concurs with nginx's one
#        #
#        #location ~ /\.ht {
#        #    deny  all;
#        #}
#    }# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}server {listen 8080;location / {default_type text/html;content_by_lua_block {ngx.say("

hello, world

")}}}server {default_type text/plain;listen 8082;location = /verify {content_by_lua_block {local cjson = require("cjson")local jwt = require("resty.jwt")local jwt_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" ..".eyJmb28iOiJiYXIifQ" ..".VAoRL1IU0nOguxURF2ZcKR0SGKE1gCbqwyh8u2MLAyY"local jwt_obj = jwt:verify("lua-resty-jwt", jwt_token)ngx.say(cjson.encode(jwt_obj))}}location = /sign {content_by_lua_block {local cjson = require("cjson")local jwt = require("resty.jwt")local jwt_token = jwt:sign("lua-resty-jwt",{header={typ="JWT", alg="HS256"},payload={foo="bar"}})ngx.say(jwt_token)}} }}

相关内容

热门资讯

开箱安卓系统机顶盒,智能娱乐新... 你有没有想过,家里的电视也能变成智能小助手?没错,就是那种可以安装各种应用、看高清视频、还能玩游戏的...
平板电脑安卓系统包,平板电脑应... 你有没有发现,现在的生活越来越离不开平板电脑了?尤其是安卓系统的平板,简直成了我们生活中不可或缺的好...
安卓手机系统初始化,安卓手机系... 你刚刚拿到的新安卓手机,是不是兴奋得手舞足蹈?别急,别急,先得来个系统初始化,让这个小家伙焕发新生!...
安卓系统测试常见bug,安卓系... 你有没有遇到过在使用安卓手机时,突然屏幕卡顿、应用崩溃或者系统突然变得不响应的情况?别急,这可不是个...
家用备份系统和安卓手机,家用备... 你有没有想过,家里的数据安全就像是一颗定时炸弹,随时可能“爆炸”?别担心,今天就来给你揭秘如何用安卓...
安卓系统重启功能吗,操作技巧与... 你有没有遇到过手机突然卡顿,或者系统运行缓慢,然后你灵机一动,心想:“重启说不定就好了!”没错,重启...
安卓系统附带什么软件 你有没有发现,每次拿到新手机,打开安卓系统,那感觉就像打开了一个新世界的大门呢?里面藏着各种各样的软...
寨板装安卓单系统 你有没有想过,手机装上安卓系统,是不是就能像电脑一样,玩转各种软件和游戏呢?今天,就让我带你一探究竟...
安卓系统是国家的吗,非国家属性... 你有没有想过,安卓系统这个我们每天不离手的家伙,它到底是不是国家的呢?这个问题听起来可能有点奇怪,但...
安卓模拟器2.0系统,系统升级... 你有没有想过,在电脑上玩手游也能这么畅快?没错,就是安卓模拟器2.0系统!这款神奇的软件,让无数手游...
如何减少安卓系统的耗电,轻松延... 手机电量总是不够用,是不是你也和我一样,每天都要担心手机突然没电呢?别急,今天就来和你聊聊如何减少安...
荣耀安卓11系统消息栏,荣耀安... 你有没有发现,最近你的荣耀手机是不是变得聪明多了?没错,就是那个荣耀安卓11系统,它给我们的消息栏带...
苹果耳机连安卓系统,跨界体验新... 你有没有想过,苹果的耳机竟然也能和安卓系统完美搭配呢?是的,你没听错,就是那个以封闭生态著称的苹果,...
安卓系统好的平板品牌,盘点热门... 你有没有想过,拥有一款性能卓越、体验流畅的安卓平板,简直就像拥有了移动的办公桌和娱乐中心呢?没错,今...
安卓下载系统和苹果区别,系统差... 你有没有发现,手机里的安卓下载系统和苹果的App Store简直就是两个截然不同的世界?今天,就让我...
怎么重启相机安卓系统手机,轻松... 手机相机突然罢工了?别急,让我来告诉你怎么重启安卓系统的手机相机,让你瞬间恢复拍照的乐趣!一、手动重...
智慧屏设置显示安卓系统,探索智... 亲爱的读者们,你是否也像我一样,对家里的智慧屏充满了好奇和期待?想要让它更好地服务于我们的生活,那么...
更新系统安卓系统会更新吗,体验... 亲爱的手机用户们,你是不是也和我一样,时不时地收到手机上那个小小的更新提示呢?没错,就是那个让你有点...
安卓新系统怎么布局好看,打造视... 你有没有发现,每次安卓系统更新,总有些小伙伴兴奋地跑来问我:“新系统怎么布局才好看啊?”这不就是今天...
win10安卓5.1系统,跨界... 亲爱的读者们,你是否曾想过,在Windows 10的强大平台上,也能体验到安卓5.1系统的流畅与便捷...