原创

diy 你的nginx-OpenResty

温馨提示:
本文最后更新于 2020年11月22日,已超过 1,240 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

OpenResty

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

安装

openresty依赖库为:perl 5.6.1+, libreadline, libpcre, libssl

centos:

yum install readline-devel pcre-devel openssl-devel

ubuntu:

apt-get install libreadline-dev libpcre3-dev libssl-dev perl

编译安装:

下载地址:http://openresty.org/cn/download.html

curl -O https://openresty.org/download/openresty-1.17.8.2.tar.gz
tar -zvxf openresty-1.17.8.2.tar.gz 
cd openresty-1.17.8.2
make 
make install

将默认安装到/usr/local/openresty 目录中

使用

创建一个新目录 openrestyTest,以及创建logs和conf目录:

[tioncico@tioncico-server homeTest]$ mkdir openrestyTest
[tioncico@tioncico-server homeTest]$ cd openrestyTest/
[tioncico@tioncico-server openrestyTest]$ mkdir ./logs ./conf
[tioncico@tioncico-server openrestyTest]$

logs将存放openresty的日志,conf存放配置文件

在conf中新建一个nginx.conf文件:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

可以看到,配置跟nginx的一致,不过在location块中多了个content_by_lua块,通过这个可以执行lua脚本

启动openresty:

 /usr/local/openresty/nginx/sbin/nginx -p ./ -c ./conf/nginx.conf

-p后面为我们的项目目录,也就是 openrestyTest目录中,-c为配置文件路径

访问 ip:9000即可看到输出:

仙士可博客

停止openresty

 /usr/local/openresty/nginx/sbin/nginx -p ./ -s stop

重启 openresty

/usr/local/openresty/nginx/sbin/nginx -p ./ -s reload

注意,启动和停止都必须指定-p,否则守护进程找不到pid文件

通过openresty配置项目

nginx.conf写法:

worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 9000;
        server_name xxx.easyswoole.cn;
        index index.php index.html index.htm default.php default.htm default.html;
        root /www/wwwroot/homeTest/openrestyTest;
        ################################### 宝塔 php-fpm支持  #################################
        location ~ [^/]\.php(/|$)
        {
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi-72.sock;
            fastcgi_index index.php;

            set $real_script_name $fastcgi_script_name;
            if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
                  set $real_script_name $1;
                  set $path_info $2;
             }

            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param  QUERY_STRING       $query_string;
            fastcgi_param  REQUEST_METHOD     $request_method;
            fastcgi_param  CONTENT_TYPE       $content_type;
            fastcgi_param  CONTENT_LENGTH     $content_length;

            fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
            fastcgi_param  REQUEST_URI        $request_uri;
            fastcgi_param  DOCUMENT_URI       $document_uri;
            fastcgi_param  DOCUMENT_ROOT      $document_root;
            fastcgi_param  SERVER_PROTOCOL    $server_protocol;
            fastcgi_param  REQUEST_SCHEME     $scheme;
            fastcgi_param  HTTPS              $https if_not_empty;

            fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
            fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

            fastcgi_param  REMOTE_ADDR        $remote_addr;
            fastcgi_param  REMOTE_PORT        $remote_port;
            fastcgi_param  SERVER_ADDR        $server_addr;
            fastcgi_param  SERVER_PORT        $server_port;
            fastcgi_param  SERVER_NAME        $server_name;

            # PHP only, required if PHP was built with --enable-force-cgi-redirect
            fastcgi_param  REDIRECT_STATUS    200;

            fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
            fastcgi_param SCRIPT_NAME $real_script_name;
            fastcgi_param PATH_INFO $path_info;
        }
        ################ 上面都是宝塔fpm支持的写法 ###################################

        ## 这个是额外的openresty定义写法
        location /openrestyTest {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>Hello, World!</p>")
            ';
        }
    }
}

在openrestyTest目录新增index.php:

<?php
echo "666";

重启之后,继续访问:

[tioncico@tioncico-server openrestyTest]$ curl http://xxx.easyswoole.cn:9000/openrestyTest
<p>Hello, World!</p>
[tioncico@tioncico-server openrestyTest]$ curl http://xxx.easyswoole.cn:9000/
666[tioncico@tioncico-server openrestyTest]$
正文到此结束
本文目录