续我的上篇博文:https://mp.csdn.net/postedit/89669399。即Memcache实现php页面的加速缓存已经配置好。
一、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 等都进行一致的高性能响应。
二、实验环境(rhel6.5版本)
主机环境:rhel6.5 selinux 和iptables 都必须是disabled状态
各主机信息
| 主机名 | IP | 服务 |
|---|---|---|
| server1 | 172.25.83.1 | lnmp架构+memcache |
| 物理机 | 172.25.83.83 | 用于测试 |
三、OpenResty实现反向代理及缓存加速
1、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(根据预编译的结果提示,使用gmake)


(3)拷贝index.php和example.php文件到openresty下的nginx的默认发布目录下
此处example.php文件是memcache模块中的,具体请查看我的上片博文:https://mp.csdn.net/postedit/89669399
[root@server1 openresty-1.13.6.1]# 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 html]# cd ../conf/
[root@server1 conf]# pwd
/usr/local/openresty/nginx/conf
[root@server1 conf]# vim nginx.conf
#反向代理模块,keeepalive指令是指http-upstream-keepalive-module提供的功能,这里保持512个立即不关闭的连接用于提高性能
17 http { #在该http模块中加入19-22行的反向代理内容
18
19 upstream memcache {
20 server localhost:11211;
21 keepalive 512;
22 }
23
24 include mime.types;
25 default_type application/octet-stream;
56 location /memc { #在http模块中加入该location模块
57 internal; #internal:表示只接受内部网络,不接受外部http请求,如果需要接受访问可以使用allow,deny来控制
58 memc_connect_timeout 100ms; #与memcached服务器建立连接的超时时间。不得超过597 hours。
59 memc_send_timeout 100ms; #设置发送请求到memcached服务器的超时时间。不得超过597 hours。
60 memc_read_timeout 100ms; #定义从memcached服务器读取响应超时时间。不得超过597 hours。
61 set $memc_key $query_string; #$memc_key:表示以什么key,这里使用nginx内置的$query_string来作为key
62 set $memc_exptime 300; #$memc_exptime为缓存失效时间
63 memc_pass memcache; #使用上面编写的以memcache为名字的方向代理内容
64 }
81 location ~ \.php$ { #打开http模块中的该location模块,并进行修改。#~ \.php$:这个location配置了缓存,这表示所有以.php结尾的都会被缓存
82 set $key $uri$args;
83 srcache_fetch GET /mem $key; #srcache_fetch:表示注册一个输入拦截处理器到location,这个配置进入时被执行
84 srcache_store PUT /mem $key; #srcache_store:输出拦截
85 root html;
86 fastcgi_pass 127.0.0.1:9000;
87 fastcgi_index index.php;
88 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
89 include fastcgi.conf;
90 }



(5)检查配置文件是否语法错误
[root@server1 sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@server1 sbin]# ./nginx -t #检查语法错误

(6)语法无误后开启nginx,查看端口
[root@server1 sbin]# ./nginx
[root@server1 sbin]# netstat -antulpe

2、测试
(1)网页测试:输入172.25.4.1

(2)访问速度测试
【1】、真机访问index.php,查看速度及出错情况
[root@foundation83 Desktop]# ab -c 10 -n 5000 http://172.25.83.1/index.php


比memcache模块中访问时速度稍微快点
【2】、真机访问example.php,查看速度及出错情况
[root@foundation83 Desktop]# ab -c 10 -n 5000 http://172.25.83.1/example.php


同样比memcache模块中访问速度要快
本文介绍如何利用OpenResty结合Memcache实现Web服务的反向代理与缓存加速,详细步骤包括OpenResty的部署、配置以及性能测试对比。
1888

被折叠的 条评论
为什么被折叠?



