利用OpenResty为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.获取OpenResty压缩包,并解压

tar zxf openresty-1.13.6.1.tar.gz

2.配置

cd openresty-1.13.6.1
./configure


3.编译安装

gmake && gmake install

4.复制网页文件

cd /usr/local/openresty/nginx/html/
cp /usr/local/lnmp/nginx/html/index.php .
cp /usr/local/lnmp/nginx/html/example.php .

5.更改配置文件

cd /usr/local/openresty/nginx/conf
vim nginx.conf
===============
  2 user  nginx nginx;
  3 worker_processes  auto;

 18     upstream memcache {
 19         server localhost:11211;
 20         keepalive 512;
 21         }

 47         location / {
 48             root   html;
 49             index index.php index.html index.htm;
 50         }

 69         location /memc {
 70                 internal;
 71                 memc_connect_timeout 100ms;
 72                 memc_send_timeout 100ms;
 73                 memc_read_timeout 100ms;
 74                 set $memc_key $query_string;
 75                 set $memc_exptime 300;
 76                 memc_pass memcache;
 77         }
 78         location ~ \.php$ {
 79             set $key $uri$args;
 80             srcache_fetch GET /memc $key;
 81             srcache_store PUT /memc $key;
 82 		root html;
 83 		fastcgi_pass 127.0.0.1:9000;
 84 		fastcgi_index index.php;
 85	# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 86 		include fastcgi.conf;
 87 	}




6.查看

cd /usr/local/openresty/nginx/sbin/

./nginx -t 查看配置文件语法是否有错误

./nginx -V 查看当前版本是否正确

7.启动服务

./nginx

ps ax

8.测试(物理机)

ab -c 10 -n 5000 http://172.25.70.1/index.php

此时是OpenResty的Nginx生效

接下来,我们将OpenResty的Nginx关闭,将Nginx打开

[root@server1 sbin]# pwd
/usr/local/openresty/nginx/sbin
[root@server1 sbin]# ./nginx -s stop
[root@server1 sbin]# nginx
=============================================
ab -c 10 -n 5000 http://172.25.70.1/index.php



OpenResty明显提升了Nginx的访问PHP网页的速度。

相关内容

    暂无相关文章