一、介绍:
中文官网: http:openresty.org/cn/
下载地址: http://openresty.org/cn/download.html
编译安装: http://openresty.org/cn/installation.html
核心关键词: Nginx+Lua、高性能、单机实现10K 乃至 1000K并发
详细介绍如下:
OpenResty? 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty? 通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty? 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
我个人的使用场景:基于lua快速脚本做数据(访问日志等)收集、一些接口的访问限制(如,根据ua\ip\请求参数\cookie等)、复杂的负载均衡等
二、编译安装:
依赖库:`perl 5.6.1+`, `libpcre`, `libssl`
Debian 和 Ubuntu 用户
sudo apt-get install libpcre3-dev libssl-dev perl make build-essential curl
Fedora 和 RedHat 用户
yum install pcre-devel openssl-devel gcc curl
默认方式安装和编译(不推荐-测试目的)
tar -xzvf openresty-VERSION.tar.gz
cd openresty-VERSION/
./configure
make
sudo make install
我个人推荐安装方式:
# 一、配置(生成Makefile文件)
# 打开更多模块可以自行: ./configure --help 查看其它模块
# 以下是我项目中用到的,我觉得也适合绝大多数场景了
./configure --prefix=/data/openresty/build \
--with-luajit \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_gzip_static_module \
--with-stream \
--with-stream_ssl_module
# 二、编译
# 使用多核编译,加快编译速度
# 注意自己电脑的核心数,最好不要用完,除非你不做其它事情
make -j6
# 三、安装,安装在--prefix指定目录下
make install
三、体验
1. Hello World体验
mkdir -p work/helloworld
cd work/helloworld
mkdir conf logs
vim conf/nginx.conf
`conf/nginx.conf`内容如下:
# 注意实际项目中,这里会设置为实际的cpu核心数,大家自行压力测试来选择
worker_processes 1;
error_log logs/error.log;
events {
# 注意实际项目中,这里会会设置更高的值,大家自行压力测试来选择
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
default_type text/html;
# 注意这里是by_lua,就是来自Lua脚本
content_by_lua 'ngx.say("<h1>hello, world</h1>")';
}
}
}
启动服务:
# 运行程序: /data/openresty/build/nginx/sbin/nginx
# -p 指定工作目录为当前的目录
# -c 指定运行的配置文件
/data/openresty/build/nginx/sbin/nginx \
-p `pwd`/ -c conf/nginx.conf
浏览器访问效果:
2. Hello World 进阶
mkdir -p work/helloworld2
cd work/helloworld2
mkdir conf logs
# 在 work/helloworld2 下新建lua来保存我们的lua脚本
mkdir lua
vim lua/hello.lua
`lua/hello.lua`文件内容如下:
ngx.say("< h1 >hello, world2< /h1 >")
`conf/nginx.conf`内容如下:
# 注意实际项目中,这里会设置为实际的cpu核心数,大家自行压力测试来选择
worker_processes 1;
error_log logs/error.log;
events {
# 注意实际项目中,这里会会设置更高的值,大家自行压力测试来选择
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
default_type text/html;
# 注意这里是by_file,就是来自lua脚本文件
content_by_lua_file lua/hello.lua;
}
}
}
启动服务:
# 运行程序: /data/openresty/build/nginx/sbin/nginx
# -p 指定工作目录为当前的目录
# -c 指定运行的配置文件
/data/openresty/build/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf
浏览器访问效果:
3. 更多更强大的lua操作技巧
略,大家自行搜索了解,网上资料不少的。
四、附送Nginx跨域配置
【注:这个其实是nginx的内容,也适用于openresty】
mkdir -p work/cros
cd work/cros
mkdir conf logs html
vim conf/nginx.conf
`conf/nginx.conf`内容如下:
# 注意实际项目中,这里会设置为实际的cpu核心数,大家自行压力测试来选择
worker_processes 1;
error_log logs/error.log;
events {
# 注意实际项目中,这里会会设置更高的值,大家自行压力测试来选择
worker_connections 1024;
}
http {
server {
listen 8080;
server_name localhost;
location / {
# 如果是OPTIONS,则返回跨域配置
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'User-Agent,Keep-Alive,Content-Type';
# 过期时间:1天,单位秒
add_header 'Access-Control-Max-Age' 86400;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
root /data/openresty/work/cros/html;
index index.html index.htm;
}
}
}
vim html/index.html
`html/index.html`内容如下:
<h1> Hello cros is success. </h1>
启动服务:
# 运行程序: /data/openresty/build/nginx/sbin/nginx
# -p 指定工作目录为当前的目录
# -c 指定运行的配置文件
/data/openresty/build/nginx/sbin/nginx -p `pwd`/ -c conf/nginx.conf
curl发起OPTIONS请求:
通过`curl`命令进行OPTIONS请求和配置的预期一样。完美!!!