OpenResty实现反向代理及缓存加速,


有关memcache模块实现缓存加速情况请查看我上篇博文:https://blog.csdn.net/y_yang666/article/details/87794129
OpenResty简介:
  • 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 等都进行一致的高性能响应。

OpenResty的部署

1.官网下载openresty安装包并解压

此步骤可以省略:[root@server1 ~]# nginx -s stop   ##因为我之前做了lnmp架构的搭建,而openresty自带nginx,所以要停掉之前的nginx
[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz


2.编译安装

[root@server1 ~]# cd openresty-1.13.6.1   ##进入解压目录
[root@server1 openresty-1.13.6.1]# ./configure 
[root@server1 openresty-1.13.6.1]# gmake && gmake install   ##此处要用gmake而不是make



3.拷贝index.php和example.php文件到openresty下的nginx的默认发布目录下
此处两个文件是memcache模块中的,具体请查看我的上片博文:https://blog.csdn.net/y_yang666/article/details/87794129

[root@server1 ]# cd /usr/local/openresty/nginx/html/
[root@server1 html]# cp /usr/local/lnmp/nginx/html/index.php .
[root@server1 html]# cp /usr/local/lnmp/nginx/html/example.php .
[root@server1 html]# ls
50x.html  example.php  index.html  index.php


4.编辑openresty配置文件

[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf 
 ##反向代理模块,keeepalive指令是指http-upstream-keepalive-module提供的功能,这里保持512个立即不关闭的连接用于提高性能
 17 http {         
 18           upstream memcache {
 19               server localhost:11211;
 20               keepalive 512;
 21          }
 22     include       mime.types;
 23     default_type  application/octet-stream;


 54         location /memc {
 55             internal;       ##internal:表示只接受内部网络,不接受外部http请求,如果需要接受访问可以使用allow,deny来控
 56             memc_connect_timeout 100ms;
 57             memc_send_timeout 100ms;
 58             memc_read_timeout 100ms;
 59             set $memc_key $query_string;     ##$memc_key:表示以什么key,这里使用nginx内置的$query_string来作为key
 60             set $memc_exptime 300;             ##$memc_exptime为缓存失效时间
 61             memc_pass memcache;
 62         }

 80         location ~ \.php$ {             ##~ \.php$:这个location配置了缓存,这表示所有以.php结尾的都回被缓存
 81             set $key $uri$args;
 82             srcache_fetch GET /memc $key;     ##srcache_fetch:表示注册一个输入拦截处理器到location,这个配置进入时被执行
 83             srcache_store PUT /memc $key;      ##srcache_store:输出拦截
 84             root           html;
 85             fastcgi_pass   127.0.0.1:9000;
 86             fastcgi_index  index.php;
 87             #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_    name;
 88             include        fastcgi.conf;
 89         }




5.检查配置文件是否语法错误

[root@server1 nginx]# cd sbin/
[root@server1 sbin]# ./nginx -t   ##检差语法错误


6.语法无误后开启nginx,查看端口

[root@server1 sbin]# ./nginx 
[root@server1 sbin]# netstat -tnlp


测试:

1.网页测试:输入172.25.4.1
2.访问速度测试

1).真机访问index.php,查看速度及出错情况

[kiosk@foundation4 ~]$ ab -c 10 -n 5000 http://172.25.4.1/index.php



比memcache模块中访问时速度稍微快点
2).真机访问example.php,查看速度及出错情况

[kiosk@foundation4 ~]$ ab -c 10 -n 5000 http://172.25.4.1/example.php



同样比memcache模块中访问速度更快

相关内容

    暂无相关文章