openresty实现nginx+lua
之前直接用nginx+lua,实在麻烦,用openresty简单多了。更简单的是用docker实现openresty,参考链接:Docker 安装OpenResty,步骤如下:
1、拉取镜像:docker pull openresty/openresty
2、启动:docker run --name openresty
-p 80:80 -d openresty/openresty
3、创建宿主机目录(用于映射进docker):
mkdir -p /app/docker/openresty/conf
mkdir -p /app/docker/openresty/html
mkdir -p /app/docker/openresty/lua
mkdir -p /app/docker/openresty/logs
4、拷贝容器中文件到宿主机目录
//拷贝nginx配置文件
docker cp openresty:/usr/local/openresty/nginx/conf/nginx.conf /app/docker/openresty/conf
//拷贝lua库
docker cp openresty:/usr/local/openresty/lualib /app/docker/openresty/lua
5、删除容器,新建容器:
//删除容器
docker rm -f openresty
//启动
docker run --name openresty \
-v /app/docker/openresty/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /app/docker/openresty/lua/:/usr/local/openresty/lualib \
-v /app/docker/openresty/html/:/usr/local/openresty/nginx/html \
-v /app/docker/openresty/logs/:/usr/local/openresty/nginx/logs \
-p 8080:80 -d openresty/openresty
6、配置容器:
# /app/docker/openresty/lua/test.lua
ngx.say('{"id" : "10086", "name":"test"}')
# /app/docker/openresty/conf/nginx.conf http { server_tokens off; include mime.types; default_type application/octet-stream; underscores_in_headers on;#表示如果header name中包含下划线,则不忽略 sendfile on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; #include /etc/nginx/conf.d/*.conf; #lua 模块 lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #c模块 lua_package_cpath "/usr/local/openresty/lualib/?.so;;"; server { listen 80; server_name 127.0.0.1; location /test { # 默认的响应类型 default_type application/json;ng content_by_lua_file lua/test.lua; } location / { root html; index index.html index.htm; } } }
7、进入容器,删除default.conf。这个文件中有默认的80端口配置,会影响nginx.conf文件中的配置。参考链接中没有这个动作。
docker exec -it openresty/openresty /bin/bash
mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.back
8、测试:
curl http://localhost:8080/test
{"id" : "10086", "name":"test"}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2015-05-15 MacPE+WinPE-黑苹果之路
2009-05-15 转文一篇
2008-05-15 页面跳转的方法