OpenResty实现反向代理及缓存加速(到达nginx前端层面缓存前移),


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 等都进行一致的高性能响应。

这篇文章是基于Lnmp架构和memcache写的,Lnmp架构作为实验的基本,而memcache是用来对比openresty的速度以及优缺点的,下面给出前两篇博客的链接
Lnmp:https://blog.csdn.net/weixin_43287982/article/details/87806322
MemCache:https://blog.csdn.net/weixin_43287982/article/details/88399502


OpenResty的部署

[root@server1 ~]# tar zxf openresty-1.13.6.1.tar.gz -C /usr/local/
[root@server1 ~]# cd /usr/local/
[root@server1 local]# ln -s openresty-1.13.6.1 openresty
[root@server1 local]# cd openresty
[root@server1 openresty]# ls
bundle  configure  COPYRIGHT  patches  README.markdown  README-win32.txt  util
[root@server1 openresty]# ./configure 
Type the following commands to build and install:
    gmake
    gmake install
[root@server1 openresty]# gmake && gmake install

在./configure完成后会提示需要用gmake && gmake install,而不是直接make && makeinstall,gmake完成后openresty目录中会出现一个nginx的目录

  1. 拷贝index.php和example.php文件到openresty下的nginx的默认发布目录下
    此处两个文件是memcache模块中的,具体请查看我的memcache博文此处两个文件,第一个是php的默认发表界面,第二个是动态网站也是用php的,是用来测试的
[root@server1 ~]# cp /usr/local/lnmp/nginx/html/index.php /usr/local/openresty/nginx/html/
[root@server1 ~]# cp /usr/local/lnmp/nginx/html/example.php /usr/local/openresty/nginx/html/
  1. 编辑openresty配置文件,并检查是否有语法错误,无误后开启nginx,查看端口是否开启正常
    配置文件参数详解在文章末尾
[root@server1 ~]# vim /usr/local/openresty/nginx/conf/nginx.conf
 17 http {
 18     upstream memcache {
 19         server localhost:11211;
 20         keepalive 512;	##反向代理模块,keeepalive指令是指http-upstream-keepalive-module提供的功能,这里保持512个立即不关闭的连接用于提高性能
 21         }

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

 81         location ~ \.php$ {		##~ \.php$:这个location配置了缓存,这表示所有以.php结尾的都回被缓存
 82             set $key $uri$args;
 83             srcache_fetch GET /memc $key;		##srcache_fetch:表示注册一个输入拦截处理器到location,这个配置进入
 84             srcache_store PUT /memc $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         }
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 ~]# /usr/local/openresty/nginx/sbin/nginx
[root@server1 ~]# netstat -atnlp

这个故事告诉我们修改配置文件一定要认真


测试:

[root@foundation60 Desktop]# ab -c 10 -n 5000 http://172.25.254.1/index.php
[root@foundation60 Desktop]# ab -c 10 -n 5000 http://172.25.254.1/example.php


  • 配置文件详解
  • 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配置缓存。
  • 检测openresty配置文件是否有语法错误,并打开openstry

相关内容

    暂无相关文章