本文介绍: nginx默认配置文件注释太多,影响后续我们编辑这里nginx.conf中的注释部分删除,保留有效部分。”linux链接文件 “l”是链接文件,l是link的意思。看到里面nginx目录了吗,OpenResty就是在Nginx基础上集成了一些Lua模块opm是OpenResty一个管理工具可以帮助我们安装一个第三方的Lua模块。其实就是把初始nginx.conf配置文件里所有的注释删除了,看起来清爽多了。NGINX_HOME:后面是OpenResty安装目录下的nginx目录

安装OpenResty

1.安装

首先你的Linux虚拟机必须联网

1)安装开发

首先要安装OpenResty依赖开发库,执行命令

yum install -y pcre-devel openssl-devel gcc --skip-broken

2)安装OpenResty仓库

可以在你的 CentOS 系统添加 openresty 仓库,这样就可以便于未来安装或更新我们软件包通过 yum check-update 命令)。运行下面的命令就可以添加我们仓库

yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

如果提示说命令不存在,则运行

yum install -y yum-utils 

然后重复上面的命令

3)安装OpenResty

然后就可以像下面这样安装软件包比如 openresty

yum install -y openresty

4)安装opm工具

opm是OpenResty的一个管理工具,可以帮助我们安装一个第三方的Lua模块。

如果你想安装命令行工具 opm,那么可以像下面这样安装 openresty-opm 包:

yum install -y openresty-opm

5)目录结构

默认情况下,OpenResty安装的目录是:/usr/local/openresty
在这里插入图片描述

看到里面nginx目录了吗,OpenResty就是在Nginx基础上集成了一些Lua模块。
继续查看bin目录:
在这里插入图片描述
linux链接文件 “l”是链接文件,l是link的意思。 相当于windows快捷方式;”

6)配置nginx环境变量

打开配置文件

vim /etc/profile

在最下面加入两行:

export NGINX_HOME=/usr/local/openresty/nginx
export PATH=${NGINX_HOME}/sbin:$PATH

NGINX_HOME:后面是OpenResty安装目录下的nginx的目录

然后配置生效

source /etc/profile

2.启动和运行

OpenResty底层基于Nginx的,查看OpenResty目录的nginx目录,结构windows中安装的nginx基本一致:

在这里插入图片描述

所以运行方式nginx基本一致:

# 启动nginx
nginx
# 重新加载配置
nginx -s reload
# 停止
nginx -s stop

nginx默认配置文件注释太多,影响后续我们的编辑,这里将nginx.conf中的注释部分删除,保留有效部分

修改/usr/local/openresty/nginx/conf/nginx.conf文件,内容如下

其实就是把初始nginx.conf配置文件里所有的注释给删除了,看起来清爽多了

#user  nobody;
worker_processes  1;
error_log  logs/error.log;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       8081;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

在Linux的控制台输入命令以启动nginx:

nginx

然后访问页面http://192.168.141.100:8081,注意ip地址替换为你自己虚拟机IP:

nginx.conf配置文件里面改成了监听8081端口,所以这里访问8081端口

在这里插入图片描述

3.备注

加载OpenResty的lua模块:

#lua 模块
lua_package_path "/usr/local/openresty/lualib/?.lua;;";
#c模块     
lua_package_cpath "/usr/local/openresty/lualib/?.so;;";  

common.lua

-- 封装函数发送http请求,并解析响应
local function read_http(path, params)
    local resp = ngx.location.capture(path,{
        method = ngx.HTTP_GET,
        args = params,
    })
    if not resp then
        -- 记录错误信息返回404
        ngx.log(ngx.ERR, "http not found, path: ", path , ", args: ", args)
        ngx.exit(404)
    end
    return resp.body
end
-- 将方法导出
local _M = {  
    read_http = read_http
}  
return _M

释放Redis连接API:

-- 关闭redis连接工具方法,其实是放入连接池
local function close_redis(red)
    local pool_max_idle_time = 10000 -- 连接空闲时间单位毫秒
    local pool_size = 100 --连接池大小
    local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
    if not ok then
        ngx.log(ngx.ERR, "放入redis连接池失败: ", err)
    end
end

读取Redis数据的API:

-- 查询redis方法 ip和portredis地址,key查询key
local function read_redis(ip, port, key)
    -- 获取一个连接
    local ok, err = red:connect(ip, port)
    if not ok then
        ngx.log(ngx.ERR, "连接redis失败 : ", err)
        return nil
    end
    -- 查询redis
    local resp, err = red:get(key)
    -- 查询失败处理
    if not resp then
        ngx.log(ngx.ERR, "查询Redis失败: ", err, ", key = " , key)
    end
    --得到的数据为空处理
    if resp == ngx.null then
        resp = nil
        ngx.log(ngx.ERR, "查询Redis数据为空, key = ", key)
    end
    close_redis(red)
    return resp
end

开启共享词典:

# 共享字典,也就是本地缓存名称叫做:item_cache大小150m
lua_shared_dict item_cache 150m; 

原文地址:https://blog.csdn.net/hza419763578/article/details/133620436

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_27474.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注