OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
OpenResty通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
软件下载: https://openresty.org/cn/
软件安装:
[root@server1 ~]# tar zxf openresty-1.19.9.1.tar.gz
[root@server1 ~]# cd openresty-1.19.9.1/
[root@server1 openresty-1.19.9.1]# ./configure --with-http_ssl_module
[root@server1 openresty-1.19.9.1]# make && make install
软件配置;
[root@server1 openresty-1.19.9.1]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# systemctl start memcached.service ##开启11211端口
[root@server1 conf]# netstat -antlp ##查看端口
[root@server1 conf]# vim nginx.conf
user nginx;
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 65535;
}
http {
upstream memcache{
server 127.0.0.1:11211;
keepalive 512; ##保持 512 个不立即关闭的连接用于提升性能
}
location /memc {
internal; ##表示只接受内部访问
memc_connect_timeout 100ms;
memc_send_timeout 100ms;
memc_read_timeout 100ms;
set $memc_key $query_string; ##使用内置的$query_string来
set $memc_exptime 300; ##表示缓存失效时间
memc_pass memcache;
}
location ~ \.php$ {
set $key $uri$args;
srcache_fetch GET /memc $key;
srcache_store PUT /memc $key;
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
当所请求的u r i 以 “ .php ” 结尾时 , 首先到 memcache 中查询有没有以 $uri$args 为 key 的数据 , 如果有则直接返回;否则,执行 location 的逻辑,如果返回的 http 状态码为 200 ,则在输出前以$uri$args 为 key , 将输入结果存入 memcache 。
[root@server1 html]# systemctl start php-fpm.service
开启nginx
[root@server1 conf]# nginx
在真机中测试:
[kiosk@foundation6 Desktop]$ ab -c10 -n5000 http://172.25.6.1/example.php
可以看到缓存速度为1690.92
关闭之前的nginx
[root@server1 conf]# nginx -s stop
开启高速缓存nginx
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx
再次在真机中测试
[kiosk@foundation6 Desktop]$ ab -c10 -n5000 http://172.25.6.1/example.php
可以看到缓存速度增加到8053.2