1、网址:http://openresty.org/cn/download.html ,下载安装:64 位 Windows: openresty-1.19.9.1-win64.zip。

2、解压缩至目录:C:\openresty-1.19.9.1-win64 。如图1

图1

3、针对 Windows 版 OpenResty 的用法文档:https://github.com/openresty/openresty/blob/master/doc/README-windows.md 。启动 OpenResty 的 Win64 二进制发行版的 nginx 服务器的 NGINX 服务器。

PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64>

4、查看任务管理器,除了原生的 Nginx 软件,还增加了:nginx.exe 。如图2

图2

5、然后可以使用 tasklist 命令查看后台运行的 nginx 进程。其中 2 个为原生的 Nginx 软件。如图3

图3

PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                    15816 Console                    1      7,400 K
nginx.exe                    93052 Console                    1     11,380 K
nginx.exe                    97096 Console                    1      8,848 K
nginx.exe                   117120 Console                    1      9,364 K
PS C:\openresty-1.19.9.1-win64> ./nginx -s stop
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                    15816 Console                    1     11,652 K
nginx.exe                   114616 Console                    1     15,888 K
PS C:\openresty-1.19.9.1-win64>

6、resty 命令行实用程序需要在您的系统中安装 Perl 解释器并且对您的 PATH 环境可见。 任何 perl 发行版都应该可以工作,包括 StrawberryPerl、ActivePerl 和 MSYS2 perl。网址:https://strawberryperl.com/ 。用于 MS Windows 的 Perl,免费!下载并安装 strawberry-perl-5.32.1.1-64bit.msi。

7、编辑 PATH 环境变量,新建:C:\openresty-1.19.9.1-win64 。如图4

图4

8、重启电脑。运行 resty 脚本。后续计划卸载原生的 Nginx 软件。因为两者功能重叠得厉害。如图5

图5

PS C:\WINDOWS\system32> tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。
PS C:\WINDOWS\system32> cd ..
PS C:\WINDOWS> cd ..
PS C:\> cd .\openresty-1.19.9.1-win64\
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                    12652 Console                    1      8,972 K
nginx.exe                    17092 Console                    1      9,512 K
PS C:\openresty-1.19.9.1-win64> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\openresty-1.19.9.1-win64> cd ..
PS C:\> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\>

9、迁移原生的 Nginx 软件 nginx.conf 的配置项至 openresty 的 nginx.conf。重新加载,报错。如图6

图6

PS C:\openresty-1.19.9.1-win64> nginx -s reload
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in ./conf/nginx.conf:48
nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in C:/openresty-1.19.9.1-win64/conf/vhosts/msi-main.conf:14
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64
PS C:\openresty-1.19.9.1-win64>

10、编辑 nginx.conf 后,重新加载,不再报错。如图7

图7


调整前:
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       443;
        server_name  localhost;

  client_max_body_size 200m;
  client_body_buffer_size 1024k;

  fastcgi_read_timeout 180s;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

  ssl on;
  ssl_certificate localhost.pem;
  ssl_certificate_key localhost-key.pem;

调整后:

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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

 server_names_hash_max_size  1024;
    server_names_hash_bucket_size  128;

    server {
        listen       443 ssl;
        server_name  localhost;

  client_max_body_size 200m;
  client_body_buffer_size 1024k;

  fastcgi_read_timeout 180s;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

  ssl_certificate localhost.pem;
  ssl_certificate_key localhost-key.pem;

        location / {
            root   E:/wwwroot;
            index  index.html index.htm index.php;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
            root           E:/wwwroot;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

 include C:/openresty-1.19.9.1-win64/conf/vhosts/*.conf;

 ## PHP-FPM Servers ##
 upstream php-fpm {
  server 127.0.0.1:9000;
 }

}
PS C:\openresty-1.19.9.1-win64> nginx -s reload
PS C:\openresty-1.19.9.1-win64> resty -e "ngx.say('Hello, OpenResty!')"
Hello, OpenResty!
PS C:\openresty-1.19.9.1-win64>

11、注意事项,所有命令必须进入目录:C:\openresty-1.19.9.1-win64 后执行。否则,会出现一些预料之外的报错。

PS C:\WINDOWS\system32> cd ..
PS C:\WINDOWS> cd ..
PS C:\> start nginx
PS C:\> php-cgi.exe -b 127.0.0.1:9000-c C:/php-7.4.6/php.ini
PS C:\> nginx -s reload
nginx: [alert] could not open error log file: CreateFile() "./logs/error.log" failed (3: The system cannot find the path specified)
2021/09/17 19:04:54 [emerg] 3708#3712: CreateFile() "./conf/nginx.conf" failed (3: The system cannot find the path specified)
PS C:\> cd .\openresty-1.19.9.1-win64\
PS C:\openresty-1.19.9.1-win64> nginx -s reload
nginx: [error] CreateFile() "./logs/nginx.pid" failed (2: The system cannot find the file specified)
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"
信息: 没有运行的任务匹配指定标准。
PS C:\openresty-1.19.9.1-win64> start nginx
PS C:\openresty-1.19.9.1-win64> tasklist /fi "imagename eq nginx.exe"

映像名称                       PID 会话名              会话#       内存使用
========================= ======== ================ =========== ============
nginx.exe                     3712 Console                    1     18,068 K
nginx.exe                    13172 Console                    1     18,560 K
PS C:\openresty-1.19.9.1-win64> nginx -s reload
PS C:\openresty-1.19.9.1-win64>

12、运行 PHP 程序,正常运行。如图8

图8

13、添加 Lua 的 server

server {
 listen 443 ssl;
 server_name morefunresty.dev.chinamcloud.cn;
 charset utf-8;
 lua_need_request_body on;

 access_log  logs/morefunresty.dev.chinamcloud.cn.access.log;
    error_log   logs/morefunresty.dev.chinamcloud.cn.error.log;

 ssl_certificate vhosts/morefunresty.dev.chinamcloud.cn.pem;
 ssl_certificate_key vhosts/morefunresty.dev.chinamcloud.cn-key.pem;

 location / {
  if ($request_method = OPTIONS ) {
   add_header Content-Length 0;
   add_header Content-Type text/plain;
   return 200;
  }
 }

 location ~ ^/lua/api/vote/([-_a-zA-Z0-9/]+) {
  default_type application/json;
  access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
  content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/vote/$1.lua;
 }

 location ~ ^/lua/api/poll/([-_a-zA-Z0-9/]+) {
  default_type application/json;
  access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
  content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/poll/$1.lua;
 }

 location ~ ^/lua/api/draw/([-_a-zA-Z0-9]+) {
  default_type application/json;
  access_by_lua_file  E:/wwwroot/msi_main/resty/app/common/boot/bootstrap.lua;
  content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/draw/$1.lua;
 }

 location /draw/times {
  internal;
  content_by_lua_file E:/wwwroot/msi_main/resty/app/mods/draw/times_check.lua;
 }
}

14、参考入门文档:https://openresty.org/cn/getting-started.html 。运行 lua 程序,响应正常。符合预期。如图9

图9

永夜