OpenResty实现LNMP的缓存前移(到达nginx前端层面)

一.什么是OpenResty
OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。通过汇聚各种设计精良的 Nginx 模块,从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。
OpenResty® 的目标是让你的Web服务直接跑在 Nginx 服务内部,充分利用 Nginx 的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。
二.缓存前移的实现(本实验是为了与上篇博客中的实验做对比,基于上边的实验)
1.关闭之前的nginx服务,因为我们要安装可以代替普通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   # 编译预环境,与nginx的源码编译一样,唯一改了一下安装路径  
 --with-http_ssl_module  
 --with-http_stub_status_module  
 --user=nginx  
 --group=nginx  
 --with-threads  
 --with-file-aio
 gmake
 gmake install

3.修改openresty的配置文件

cd /usr/local/lnmp/openresty/nginx/conf/
vim nginx.conf
#user  nobody;
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.php index.html index.htm;
        }

        location /memc {
        internal;     # 只允许内部
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;         # 键值对的形式存储
        set $memc_exptime 300;
        memc_pass memcache;
        }
        #所有请求都通过请求这个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分钟),在实际应用中可以根据具体情况为不同的内容设置不同的过期时间。

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

        # 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;
            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_params;
        }
       #为“~ \.php$”这个location配置了缓存,这表示所有以“.php”结尾的请求都会结果被缓存,当然这里只是示例需要,实际中一般不会这么配,而是为特定需要缓存的location配置缓存。
        # 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;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

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

}

4.检测openresty配置文件是否有语法错误,并打开openstry
/usr/local/lnmp/openresty/nginx/sbin/nginx -t
/usr/local/lnmp/openresty/nginx/sbin/nginx
5.在物理机测试openstry的性能,可以发现性能大大提高,处理量大致是缓存未前移的三倍….

猜你喜欢

转载自blog.csdn.net/jay_youth/article/details/81454357