docker安装openresty

 

一、创建网络

docker network create -d bridge my_network

 

二、拉取镜像

docker pull openresty/openresty

 

三、运行临时容器

docker run -itd --name openresty -p 80:80 --restart=always --net my_network openresty/openresty

 

四、宿主机创建相应目录

mkdir -p /data/openresty/nginx/conf
mkdir -p /data/openresty/nginx/conf.d
mkdir -p /data/openresty/nginx/html
mkdir -p /data/openresty/nginx/logs
mkdir -p /data/openresty/nginx/lua

 

五、将容器内必要的文件拷贝到宿主机上

docker cp openresty:/usr/local/openresty/nginx/conf/nginx.conf /data/openresty/nginx/conf/nginx.conf
docker cp openresty:/etc/nginx/conf.d/default.conf /data/openresty/nginx/conf.d/default.conf
# 以上两个配置文件也可以直接去 https://hub.docker.com/r/openresty/openresty下载 docker cp openresty:
/usr/local/openresty/nginx/html/index.html /data/openresty/nginx/html/index.html

 

六、删除临时容器

docker stop openresty
docker rm openresty

 

七、创建正式容器

docker run -it --name openresty -p 80:80 \
--privileged=true \
--net my_network \
--restart=always \
-v /data/openresty/nginx/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /data/openresty/nginx/conf.d:/etc/nginx/conf.d \
-v /data/openresty/nginx/html:/usr/local/openresty/nginx/html \
-v /data/openresty/nginx/logs:/usr/local/openresty/nginx/logs \
-v /data/openresty/nginx/lua:/usr/local/openresty/nginx/lua \
-d openresty/openresty

 

八、配置容器支持lua

  

1、修改配置文件nginx.conf

http {
    server_tokens off;
    include       mime.types;
    default_type  application/octet-stream;

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

2、创建lua文件

在/data/openresty/nginx/lua/下创建item.lua文件并写入下面内容

ngx.say('{"id":"10001","name":"SALSA"}')

 

3、修改default.conf

server {
        listen       80;
        server_name  127.0.0.1;
        
        location /api/item {
            # 默认的响应类型
            default_type application/json;
            # 响应结果有lua/item.lua文件来决定
            content_by_lua_file lua/item.lua;
         
        }
        
        location / {
            root   html;
            index  index.html index.htm;
        }

   }

 

九、重启openresty容器进行访问测试

 

http://192.168.33.88/api/item

 

posted @ 2024-09-30 14:54  程序员小艺  阅读(310)  评论(0编辑  收藏  举报