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\]$