构建nginx高速缓存

本文介绍了如何利用OpenResty这一基于Nginx和Lua的高性能Web平台来构建高速缓存。OpenResty集成了丰富的Lua库和模块,便于构建高并发的Web应用。文章中详细说明了OpenResty的下载、安装和配置过程,并通过测试展示了开启缓存后Nginx性能的显著提升。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

OpenResty通过汇聚各种设计精良的 Nginx 模块(主要由 OpenResty 团队自主开发),从而将 Nginx 有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。

软件下载: https://openresty.org/cn/ 

软件安装:

[root@server1 ~]# tar zxf openresty-1.19.9.1.tar.gz
[root@server1 ~]# cd openresty-1.19.9.1/
[root@server1 openresty-1.19.9.1]# ./configure --with-http_ssl_module 
[root@server1 openresty-1.19.9.1]# make && make install

软件配置;

[root@server1 openresty-1.19.9.1]# cd /usr/local/openresty/nginx/conf/
[root@server1 conf]# systemctl start memcached.service  ##开启11211端口
[root@server1 conf]# netstat -antlp  ##查看端口
[root@server1 conf]# vim nginx.conf

user  nginx;
worker_processes  auto;

#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 127.0.0.1:11211;
        keepalive 512;    ##保持 512 个不立即关闭的连接用于提升性能
        }



        location /memc {
        internal;    ##表示只接受内部访问
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;   ##使用内置的$query_string来
        set $memc_exptime 300;    ##表示缓存失效时间
        memc_pass memcache;
        }

      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.conf;
        }
当所请求的u r i 以 “ .php ” 结尾时 , 首先到 memcache 中查询有没有以 $uri$args 为 key 的数据 , 如果有则直接返回;否则,执行 location 的逻辑,如果返回的 http 状态码为 200 ,则在输出前以$uri$args 为 key , 将输入结果存入 memcache 。

[root@server1 html]# systemctl start php-fpm.service 

 

开启nginx

[root@server1 conf]# nginx

在真机中测试:

[kiosk@foundation6 Desktop]$ ab -c10 -n5000 http://172.25.6.1/example.php

可以看到缓存速度为1690.92

关闭之前的nginx

[root@server1 conf]# nginx -s stop

开启高速缓存nginx

[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx -t
[root@server1 conf]# /usr/local/openresty/nginx/sbin/nginx

再次在真机中测试

[kiosk@foundation6 Desktop]$ ab -c10 -n5000 http://172.25.6.1/example.php

可以看到缓存速度增加到8053.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值