一、OpenResty的介绍
-
OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web平台,由中国人章亦春发起,提供了很多高质量的第三方模块
-
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web 应用系统
-
360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户
OpenResty 的目标是让你的 Web 服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL,PostgreSQL,~Memcaches 以及 ~Redis 等都进行一致的高性能响应。 所以对于一些高性能的服务来说,可以直接使用 OpenResty 访问 Mysql或Redis等,而不需要通过第三方语言(PHP、Python、Ruby)等来访问数据库再返回,这大大提高了应用的性能。
二、配置
此时,源码编译的nginx不能和memcache结合,用增强版的nginx,应先关掉
1)关闭原nginx
[root@server1 html]# cd
[root@server1 ~]# cd lnmp/
[root@server1 lnmp]# ls
[root@server1 lnmp]# nginx -s stop
2) 解压编译安装
[root@server1 lnmp]# tar zxf openresty-1.13.6.1.tar.gz
[root@server1 lnmp]# cd openresty-1.13.6.1
[root@server1 openresty-1.13.6.1]# ls
build bundle configure COPYRIGHT Makefile patches README.markdown README-win32.txt util
[root@server1 openresty-1.13.6.1]# ./configure
[root@server1 openresty-1.13.6.1]# gmake && gmake install ##注意此处安装命令
[root@server1 openresty-1.13.6.1]# cd /usr/local/
[root@server1 local]# ls
bin etc games include lib lib64 libexec lnmp nginx openresty sbin share src
[root@server1 local]# cd openresty
[root@server1 openresty]# ls
bin COPYRIGHT luajit lualib nginx pod resty.index site
[root@server1 openresty]# cd nginx/
[root@server1 nginx]# ls
conf html logs sbin
3) 将index.php复制到新的软件路径下
[root@server1 nginx]# pwd
/usr/local/openresty/nginx
[root@server1 nginx]# cp /usr/local/nginx/html/index.php . ##拷贝到当下
4)修改配置文件
[root@server1 nginx]# vim conf/nginx.conf
2 user nginx nginx;
3 worker_processes auto;
20 upstream memcache {
21 server localhost:11211;
22 keepalive 512;
23 }
46 location / {
47 root html;
48 index index.php index.html index.htm;
49 }
50 location /memc {
51 internal; ##只接受内部访问,不接受外部http访问(安全)
52 memc_connect_timeout 100ms;
53 memc_send_timeout 100ms; ##后端服务器数据回传时间
54 memc_read_timeout 100ms; ##连接成功后,后端服务响应时间
55 set $memc_key $query_string;
56 set $memc_exptime 300;
57 memc_pass memcache;
58 }
76 location ~ \.php$ {
77 set $key $uri$args; ##把http的GET --> memcache的get http的PUT--> memcache中的set
78 srcache_fetch GET /memc $key; ##请求php页面的时候,先会去memcache中找,如果没有,正常访问
79 srcache_store PUT /memc $key; ##访问结束后将结果存到memcache中一份
80 root html;
81 fastcgi_pass 127.0.0.1:9000;
82 fastcgi_index index.php;
83 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
84 include fastcgi.conf;
85 }
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx
配置文件详细说明
-
upstream属于handler,只是他不产生自己的内容,而是通过请求后端服务器得到内容,所以才称为upstream(上游)。请求并取得响应内容的整个过程已经被封装到nginx内部,所以upstream模块只需要开发若干回调函数,完成构造请求和解析响应等具体的工作。nginx将memcache缓存前移,客户端请求到来,先查看nginx缓存
-
所有请求都通过请求这个location来操作 memcache,memc-nginx-module存取memcache是基于http method语义的
-
使用http的GET方法表示get、PUT方法表示set、这里我们将/memc设为internal表示只接受内部访问
-
不接收外部http请求,这是为了安全考虑,当然如果需要通过http协议开放外部访问,可以去掉internal然后使用deny和allow指令控制权限。比较重要的是memckey这个变量,它表示以什么作为key,这里我们直接使用Nginx内置的query_string来作为key,$memc_exptime表示缓存失效时间,以秒记
-
这里统一设为300(5分钟),在实际应用中可以根据具体情况为不同的内容设置不同的过期时间
-
为“~.php$”这个location配置了缓存,这表示所有以“.php”结尾的请求都会结果被缓存,当然这里只是示例需要,实际中一般不会这么配,而是为特定需要缓存的location配置缓存
测试:
物理真机
[kiosk@foundation47 Desktop]$ ab -c 10 -n 5000 http://172.25.47.4/index.php
Server Software: openresty/1.13.6.1
Server Hostname: 172.25.47.4
Time taken for tests: 4.386 seconds
服务器上关闭OpenResty,开启为原来nginx,即缓存未前移状态
可以看到缓存前一之后,速率明显提高
[root@server1 nginx]# /usr/local/openresty/nginx/sbin/nginx -s stop
[root@server1 nginx]# nginx
真机
[kiosk@foundation23 ~]$ ab -c 10 -n 5000 http://172.25.47.4/index.php
Server Software: nginx/1.14.0
Server Hostname: 172.25.47.4
Time taken for tests: 48.532 seconds