Saki's 研究记录

docker-compose部署openresty

字数统计: 568阅读时长: 3 min
2021/11/11

图片来源于网络

简介

OpenResty 可以直接使用Lua脚本编写复杂的逻辑运行在Nginx之上,可以做很多好玩的应用,快速构造出足以胜任10K乃至1000K以上单机并发连接的高性能Web应用系统。

环境:
机型: 1核 1GB 1Mbps 腾讯云服务器
系统: CentOS Linux release 8.4.2105 (Core)

注意:需要预先安装dockerdocker-compose服务。

部署步骤

  1. 创建挂载目录

    1
    mkdir -p openresty/{conf.d,logs}
  2. 准备镜像

  • 打开Dock Hub搜索openresty
  • 下载官方镜像,命令:docker pull openresty/openresty:alpine
  1. 编写 docker-compose.yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    version: '3'
    services:
    openresty:
    image: openresty/openresty:alpine
    restart: always
    volumes:
    - $PWD/logs:/usr/local/openresty/nginx/logs
    - $PWD/conf.d/:/etc/nginx/conf.d
    ports:
    - "8080:80"
    networks:
    - appnet

    networks:
    appnet:
  2. 启动openresty

    1
    docker-compose up -d
  3. 测试是否成功

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    curl http://localhost:8080
    <!DOCTYPE html>
    <html>
    <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
    <title>Welcome to OpenResty!</title>
    <style>
    body {
    width: 35em;
    margin: 0 auto;
    font-family: Tahoma, Verdana, Arial, sans-serif;
    }
    </style>
    </head>
    <body>
    <h1>Welcome to OpenResty!</h1>
    <p>If you see this page, the OpenResty web platform is successfully installed and
    working. Further configuration is required.</p>

    <p>For online documentation and support please refer to our
    <a href="https://openresty.org/">openresty.org</a> site<br/>
    Commercial support is available at
    <a href="https://openresty.com/">openresty.com</a>.</p>
    <p>We have articles on troubleshooting issues like <a href="https://blog.openresty.com/en/lua-cpu-flame-graph/?src=wb">high CPU usage</a> and
    <a href="https://blog.openresty.com/en/how-or-alloc-mem/">large memory usage</a> on <a href="https://blog.openresty.com/">our official blog site</a>.
    <p><em>Thank you for flying <a href="https://openresty.org/">OpenResty</a>.</em></p>
    </body>
    </html>
  4. 按照Openresty文档进行测试

  • conf.d文件夹中创建nginx.conf配置
    1
    2
    3
    4
    5
    6
    7
    8
    9
    server {
    listen 80;
    location / {
    default_type text/html;
    content_by_lua '
    ngx.say("<p>hello, world</p>")
    ';
    }
    }
  • 重启docker-compose
    1
    docker-compose restart
  • 验证
    1
    2
    curl http://localhost:8080
    <p>hello, world</p>

总结

Done.

CATALOG
  1. 1. 简介
  2. 2. 部署步骤
  3. 3. 总结