一、安装
CentOS
你可以在你的 CentOS 系统中添加 openresty
仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum check-update
命令)。 运行下面的命令就可以添加我们的仓库(对于 CentOS 8 或以上版本,应将下面的 yum
都替换成 dnf
):
# 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
然后就可以像下面这样安装软件包,比如 openresty
:
sudo yum install -y openresty
如果你想安装命令行工具 resty
,那么可以像下面这样安装 openresty-resty
包:
sudo yum install -y openresty-resty
CentOS方式安装,默认openresty安装目录为:
/usr/local/openresty
,
其他系统安装,请参考官方文档
# 启动openresty
openresty
# 重启openresty
openresty -s reload
# 停止openresty
openresty -s stop
UOS 用户
推荐您使用 apt-get安装以下的开发库:
sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
echo "deb http://openresty.org/package/debian buster openresty" | sudo tee /etc/apt/sources.list.d/openresty.list
sudo apt-get update
sudo apt-get -y install openresty
二、配置
1. Nginx配置
进入openresty安装目录下nginx文件夹: /usr/local/openresty/nginx
进行配置nginx配置, 如增加以下配置:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
# 调试auth-server(go)鉴权 --调试通过
location /go-auth/ {
access_by_lua_file /usr/local/openresty/lualib/check-token.lua;
alias /opt/;
try_files $uri $uri/ @router;
index index.html index.htm;
}
# 调试auth-server(go)鉴权
location /go-auth-server/ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-token,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
if ($request_method = 'OPTIONS') {
return 200;
}
proxy_pass http://10.23.119.209:8088/auth-server/;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
}
}
当前配置为调试配置,具体请根据实际情况进行修改
2. Lua配置
- 设置
lua
脚本,nginx
配置文件中通过access_by_lua_file
指令引用lua脚本 - 在配置文件中通过
content_by_lua_block
指令直接使用lua:
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}
lua鉴权相关配置可参考OpenResty 鉴权lua脚本