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)}} }}

相关内容

热门资讯

安卓系统自动定位关闭,隐私保护... 你有没有发现,手机里的安卓系统有时候会自动定位,这可真是让人又爱又恨啊!有时候,我们并不想让别人知道...
安卓系统电量耗尽测试,全面解析... 手机电量耗尽,这可是每个手机用户都头疼的问题。你有没有想过,你的安卓手机在电量耗尽前,到底经历了哪些...
如何升级车载安卓系统,车载安卓... 亲爱的车主朋友们,你是不是也和我一样,对车载安卓系统升级这件事充满了好奇和期待呢?想象当你驾驶着爱车...
安卓办公哪个系统好,深度解析哪... 你有没有想过,在安卓办公的世界里,哪个系统才是你的最佳拍档呢?在这个信息爆炸的时代,选择一个既强大又...
安卓系统差劲怎么解决,重拾流畅... 你有没有发现,安卓系统有时候真的让人头疼得要命?手机卡顿、应用崩溃、电池续航短,这些问题是不是让你抓...
喜欢安卓系统的原因,探索用户偏... 你有没有发现,身边的朋友、同事,甚至家人,越来越多的人开始使用安卓手机了呢?这可不是简单的潮流,而是...
安卓系统金立手机,品质生活新选... 你有没有发现,最近安卓系统下的金立手机突然火了起来?没错,就是那个曾经陪伴我们走过无数时光的金立手机...
无安卓系统的电视,新型无系统电... 亲爱的读者们,你是否厌倦了那些充斥着安卓系统的电视?想要尝试一些新鲜玩意儿?那就跟我一起探索一下无安...
麒麟系统能刷安卓系统吗,轻松刷... 你有没有想过,你的麒麟手机能不能装上安卓系统呢?这可是个让人好奇不已的问题。现在,就让我来带你一探究...
手机公司安卓系统吗,手机公司引... 你有没有想过,为什么你的手机里装的是安卓系统而不是苹果的iOS呢?这背后可是有着不少故事和门道的哦!...
安卓系统 文件网络传输,安卓系... 你有没有想过,手机里的文件怎么才能轻松地传给朋友呢?今天,就让我来给你揭秘安卓系统中的文件网络传输技...
安卓手机系统怎样备份,安卓手机... 你有没有想过,如果你的安卓手机突然“罢工”了,里面的照片、联系人、应用和数据怎么办?别担心,今天就来...
安卓系统怎样分享app,安卓系... 你是不是也和我一样,手机里装了超多好用的APP,但是有时候想和朋友分享这些宝藏,却不知道怎么操作呢?...
sonicarekids安卓系... 最近是不是你也遇到了Sonicare Kids安卓系统打不开的烦恼?别急,让我来帮你一探究竟,找出解...
安卓刷mac系统教程,体验全新... 你有没有想过,让你的安卓手机也来个华丽变身,摇身一变成为一台Mac电脑呢?别惊讶,这可不是天方夜谭,...
安卓系统根目录删除,深度揭秘删... 你有没有遇到过这种情况:手机里的安卓系统突然出了点小状况,比如不小心点错了某个按钮,结果发现根目录里...
怎么在安卓系统装windows... 你是不是也和我一样,对安卓手机的强大性能爱不释手,但又时不时地想念Windows系统的熟悉界面和那些...
kindle安卓系统壁纸设置,... 亲爱的Kindle用户,你是否曾为你的Kindle设备挑选过一款心仪的壁纸呢?今天,就让我带你一起探...
一加降级安卓系统,回顾与展望 你有没有想过,你的手机系统升级后,竟然还能降级回旧版本?这听起来是不是有点像穿越时空的魔法?没错,今...
凤凰安卓电视系统安装,畅享智能... 亲爱的读者们,你是否也像我一样,对凤凰安卓电视系统安装充满了好奇?想象一台普通的电视,通过安装这个系...