一、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的搭建

该实验在上一篇`memcache+php页面访问的加速`的基础上进行如下的操作,并于上一篇做对比。

1)关闭nginx服务,否则会与openResty服务端口冲突

--->  nginx  -s stop

2)下载openResty源码包,解压并编译

--->  tar zxf openresty-1.13.6.1.tar.gz

--->  cd openresty-1.13.6.1

--->  ./configure --prefix=/usr/local/lnmp/openresty --with-http_ssl_module  --with-http_stub_status_module --user=nginx --group=nginx  --with-threads --with-file-aio

--->  gmake && gmake install

3)修改openResrty的配置文件

--->  cd /usr/local/lnmp/openresty/nginx/conf/

--->  vim nginx.conf

user  nginx  nginx;
 worker_processes  1;

#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 localhost:11211;
         keepalive  512;
         }  

 #upstream属于handler,他不产生自己的内容,而是通过请求后端服务器得到内容,所以才称为upstream(上游)。请求并取得响应内容的整个过
 #程已经被封装到nginx内部,所以upstream模块只需要开发若干回调函数,完成构造请求和解析响应等具体的工作。
 # nginx将memcache缓存前移,客户端请求到来,先查看memcache缓存

include       mime.types;
     default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
     #                  '$status $body_bytes_sent "$http_referer" '
     #                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
     #tcp_nopush     on;
 
     #keepalive_timeout  0;
     keepalive_timeout  65;
     #gzip  on;

server {
         listen       80;
         server_name  localhost;

#charset koi8-r;
         #access_log  logs/host.access.log  main;

location / {
             root   html;
             index  index.html index.htm;
         }
         #error_page  404              /404.html;
         # redirect server error pages to the static page /50x.html
         #
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
            root   html;
         }

# 所有请求都通过请求这个location来操作 memcache,memc-nginx-module存取memcache是基于http method语义的

location /memc{

internal;         # 只接收内部访问,不接受外部http访问。比较安全。也可用deny和allow来做权限设定
         memc_connect_timeout  100ms;
         memc_send_timeout 100ms;
         memc_read_timeout 100ms;
         set $memc_key $query_string;     # 以键值对的形式存储;memc_key表示以什么为key
         set $memc_exptime 300;           # 表示缓存失效时间,单位为s(一般为5分钟)
         memc_pass memcache;
         }
         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
         #
         #location ~ \.php$ {
         #    proxy_pass   http://127.0.0.1;
         #}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
         #
            location ~ \.php$ {
            set $key $uri$args;

srcache_fetch  GET /memc  $key;    #http的GET方法表示get、PUT方法表示set

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;
         }

# deny access to .htaccess files, if Apache's document root
         # concurs with nginx's one
         #
         #location ~ /\.ht {
         #    deny  all;
         #}
     }

# another virtual host using mix of IP-, name-, and port-based configuration
     #
     #server {
     #    listen       8000;
     #    listen       somename:8080;
     #    server_name  somename  alias  another.alias;

#    location / {
     #        root   html;
     #        index  index.html index.htm;
     #    }
     #}
 }

4)打开openresty服务。检查是否有错误

--->  /usr/local/lnmp/openresty/nginx/sbin/nginx  -t    # 检测语法是否有错误
   --->  /usr/local/lnmp/openresty/nginx/sbin/nginx        # 启动openresty服务
   5)在真机中测试

# 在真机解析example.php文件,每秒请求的次数较之前有很大的提高:

openResty缓存前移(到达nginx端)的更多相关文章

  1. ASP.NET Core 缓存技术 及 Nginx 缓存配置

    前言 在Asp.Net Core Nginx部署一文中,主要是讲述的如何利用Nginx来实现应用程序的部署,使用Nginx来部署主要有两大好处,第一是利用Nginx的负载均衡功能,第二是使用Nginx ...

  2. Nginx缓存配置以及nginx ngx_cache_purge模块的使用

    web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...

  3. 使用auth_request模块实现nginx端鉴权控制

    使用auth_request模块实现nginx端鉴权控制 nginx-auth-request-module 该模块是nginx一个安装模块,使用配置都比较简单,只要作用是实现权限控制拦截作用.默认高 ...

  4. openresty如何完美替换nginx

    下载openresty wget https://openresty.org/download/openresty-1.15.8.1.tar.gz tar zxvf openresty-1.15.8. ...

  5. openresty安装lua和nginx相关

    server{ listen ; server_name a.com; index index.php; root /usr/share/nginx/html; location / { if (!- ...

  6. OpenResty+缓存穿透基本介绍

    OpenResty                1.lua表达式                 2.Nginx的安装                3.OpenResty              ...

  7. openresty HTTP status constants nginx api for lua

    https://github.com/openresty/lua-nginx-module context: init_by_lua, set_by_lua, rewrite_by_lua, acce ...

  8. H5 缓存机制浅析 移动端 Web 加载性能优化

    腾讯Bugly特约作者:贺辉超 1 H5 缓存机制介绍 H5,即 HTML5,是新一代的 HTML 标准,加入很多新的特性.离线存储(也可称为缓存机制)是其中一个非常重要的特性.H5 引入的离线存储, ...

  9. Nginx缓存配置及nginx ngx_cache_purge模块的使用

    ngx_cache_purge模块的作用:用于清除指定url的缓存 下载地址:http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz   1. ...

随机推荐

  1. mysql 数据操作 单表查询

    单表查询的语法 distinct 去重 SELECT 字段1,字段2... FROM 表名 库.表名 WHERE 条件 过滤 符合条件的 GROUP BY field 分组条件 HAVING 筛选 过 ...

  2. python初识(三)

    目录: 字符编码 文件操作 循环结构拾遗 函数 整体介绍 函数的参数 函数的返回值 函数的调用 自定义函数 一.字符编码 1.了解字符编码的知识储备 a. 文件编辑存取文件的原理(nodepad++, ...

  3. python之celery队列模块

    一.celery队列简介 Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 1 ...

  4. PHP 自动加载的简单实现(推荐)

    基于psr的规范,使用命名空间和spl_autoload_register()来实现自动加载 文件结构: |--Api |--Account.php |--User.php |--Service |- ...

  5. (转)《SSO CAS单点系列》之 15分钟让你了解SSO技术到底是个什么鬼!

    Web应用系统的演化总是从简单到复杂,从单功能到多功能模块再到多子系统方向发展. .当前的大中型Web互联网应用基本都是多系统组成的应用群,由多个web系统协同为用户提供服务. 多系统应用群,必然意味 ...

  6. Java基础教程:注解

    Java基础教程:注解 本篇文章参考的相关资料链接: 维基百科:https://zh.wikipedia.org/wiki/Java%E6%B3%A8%E8%A7%A3 注解基础与高级应用:http: ...

  7. 2016 CCPC 长春 Solution

    A - Hanzo vs. Genji 留坑. B - Fraction 水. #include <bits/stdc++.h> using namespace std; inline i ...

  8. hdu 5068 线段树加+dp

    这题说的是 有n 层每层 有两个门 每个门 可以到达上一层的两个门,然后求从a 层到达b 层的方案总数, 不能后退, 在同一层中不能从第一个门到达另一层 我们只要我们可以对于每个 区间内 有dp[o] ...

  9. 2018 eclipse安装反编译插件

    1.在eclipse的help—>Install New Software...中添加新软件开发,添加它的源:     name:jd-eclipse_update_site address:h ...

  10. zabbix-2.4.7环境部署与初始化安装

    一.zabbix简介: zabbix的特点: - 安装与配置简单,学习成本低 - 支持多语言(包括中文) - 免费开源 - 自动发现服务器与网络设备 - 分布式监视以及WEB集中管理功能 - 可以无a ...