openrestry的使用

  1. 安装依赖包
    dos2unix start.sh
    centos:
    yum install readline-devel pcre-devel openssl-devel gcc git zip unzip
    ubuntu:
    apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl
    dpkg -l | grep zlib
    apt-get install openssl libssl-dev
    apt-get install libpq-dev
    apt-get install libpcre3 libpcre3-dev
    apt-get install zlib1g-dev zip unzip
  2. 解压openresty压缩包
    tar -xzvf openresty-VERSION.tar.gz

  3. 解压并编译安装openresty
    cd openresty-VERSION/

make
make install

4.修改nginx配置文件
修改nginx.conf文件,将内容全部删除,拷贝如下内容
include /opt/openresty/conf.d/common.conf;

5.配置普通用户dh reload nginx

chmod u+s /usr/local/openresty/nginx/sbin/nginx

6.将uag的配置文件、lua等文件上传到/opt/openresty目录下
将所有文件所属人改为iwecloud用户
chown -R dh:dh /opt/openresty
chown -R dh:dh /backup
chown -R dh:dh /usr/local/openresty/nginx/proxy_temp
chown -R dh:dh /usr/local/openresty/nginx/fastcgi_temp
chown -R dh:dh /usr/local/openresty/nginx/scgi_temp
chown -R dh:dh /usr/local/openresty/nginx/uwsgi_temp
chown -R dh:dh /usr/local/openresty/nginx/client_body_temp
同时需要配置nginx的配置 user dh;

7.使用root用户安装luarocks
wget http://luarocks.org/releases/luarocks-2.0.13.tar.gz
tar -xzvf luarocks-2.0.13.tar.gz
cd luarocks-2.0.13/
./configure —prefix=/usr/local/openresty/luajit \
—with-lua=/usr/local/openresty/luajit/ \
—lua-suffix=jit \
—with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1
make
sudo make install

8.使用luarocks安装luaxml和hmac
/usr/local/openresty/luajit/bin/luarocks install luaxml 101012-2
/usr/local/openresty/luajit/bin/luarocks install lua-resty-hmac

  1. 日志分割

添加logrotate配置文件
sudo vim /etc/logrotate.d/nginx

内容为:
/opt/openresty/log/.log /opt/openresty/log/**/.log /opt/openresty/log///.log /opt/openresty/log/////.log {
notifempty
daily
rotate 10
nocompress
copytruncate
postrotate
if [ -f /var/run/nginx.pid ]; then
kill -USR1 cat /var/run/nginx.pid
fi
endscript
}

备注:/opt/openresty/log/*.log为log日志位置,多个用空格分隔

10.使用dh用户启动nginx

11:修改common.conf 的配置 resolver 8.8.8.8 114.114.114.114 valid=30s;
user dh

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,OpenResty可以通过Lua脚本和Redis数据库来动态获取upstream。具体实现方式如下: 1. 在Nginx配置文件中定义upstream,并将upstream的backend地址设置为一个占位符,例如: ``` upstream backend { server 127.0.0.1:8080; } server { location / { proxy_pass http://backend; } } ``` 2. 在Lua脚本中连接Redis数据库,并获取upstream的backend地址列表。例如: ``` local redis = require "resty.redis" local red = redis:new() -- 连接Redis数据库 red:set_timeout(1000) local ok, err = red:connect("127.0.0.1", 6379) if not ok then ngx.log(ngx.ERR, "failed to connect to Redis: ", err) return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end -- 获取backend地址列表 local backend_list, err = red:smembers("backend_list") if not backend_list then ngx.log(ngx.ERR, "failed to get backend list from Redis: ", err) return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end -- 关闭Redis连接 red:set_keepalive(10000, 100) -- 将backend地址列表设置到upstream中 local upstream = require "ngx.upstream" local backend_servers = {} for i, backend in ipairs(backend_list) do table.insert(backend_servers, {["server"] = backend}) end local ok, err = upstream.set_servers("backend", backend_servers) if not ok then ngx.log(ngx.ERR, "failed to update backend servers: ", err) return ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end ``` 3. 在Nginx配置文件中引入Lua脚本,并定时执行该脚本。例如: ``` lua_shared_dict backend 1m; upstream backend { server 127.0.0.1:8080; } server { location / { access_by_lua_file "/path/to/lua/script.lua"; proxy_pass http://backend; } } ``` 这样,当Redis数据库中的backend地址列表发生变化时,Lua脚本会自动更新upstream的backend地址,并实现动态负载均衡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值