OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
官方中文网站https://openresty.org/cn/
官方Github https://github.com/openresty/openresty

安装

Linux

安装参考https://openresty.org/cn/linux-packages.html,例如Ubuntu安装方式如下

  1. # 安装导入 GPG 公钥时所需的几个依赖包(整个安装过程完成后可以随时删除它们):
  2. sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates
  3. # 导入我们的 GPG 密钥:
  4. wget -O - https://openresty.org/package/pubkey.gpg | sudo apt-key add -
  5. # 安装 add-apt-repository 命令
  6. # (之后你可以删除这个包以及对应的关联包)
  7. sudo apt-get -y install --no-install-recommends software-properties-common
  8. # 添加我们官方 official APT 仓库:
  9. sudo add-apt-repository -y "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main"
  10. # 更新 APT 索引:
  11. sudo apt-get update
  12. 然后就可以像下面这样安装软件包,比如 openresty
  13. sudo apt-get -y install openresty

macOS

推荐使用brew

  1. brew install openresty/brew/openresty

启动方式

  1. To have launchd start openresty/brew/openresty now and restart at login:
  2. brew services start openresty/brew/openresty
  3. Or, if you don't want/need a background service you can just run:
  4. openresty

docker

官方地址https://github.com/openresty/docker-openresty

  1. $ docker run -d --name openresty -p80:80 openresty/openresty

访问http://localhost/
image.png
如果需要配置自定义的docker-compose

  1. version: "3.7"
  2. services:
  3. web:
  4. image: openresty/openresty
  5. container_name: openresty
  6. ports:
  7. - "80:80"
  8. volumes:
  9. - ./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf

默认的配置文件

  1. worker_processes auto;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. # See Move default writable paths to a dedicated directory (#119)
  9. # https://github.com/openresty/docker-openresty/issues/119
  10. client_body_temp_path /var/run/openresty/nginx-client-body;
  11. proxy_temp_path /var/run/openresty/nginx-proxy;
  12. fastcgi_temp_path /var/run/openresty/nginx-fastcgi;
  13. uwsgi_temp_path /var/run/openresty/nginx-uwsgi;
  14. scgi_temp_path /var/run/openresty/nginx-scgi;
  15. sendfile on;
  16. keepalive_timeout 65;
  17. include /etc/nginx/conf.d/*.conf;
  18. }

helloworld

修改配置文件如下

  1. events {
  2. worker_connections 1024;
  3. }
  4. http {
  5. server {
  6. listen 80;
  7. location / {
  8. content_by_lua '
  9. ngx.say("hello, lua")
  10. ';
  11. }
  12. }
  13. }
  1. $ curl -i 127.0.0.1:80
  2. HTTP/1.1 200 OK
  3. Server: openresty/1.15.8.2
  4. Date: Sat, 07 Mar 2020 10:04:07 GMT
  5. Content-Type: text/plain
  6. Transfer-Encoding: chunked
  7. Connection: keep-alive
  8. hello, lua