openresty redis和tengine 哪个好

Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。它添加了很多实用优化及功能,如负载均衡、性能优化、监控管理、日志管理等,补充了Nginx在日常生产环境中的不足。由于Tengine目前还没有RPM包,而且官方也未给出最优化安装、配置清单,我们只能自己来确定Tengine编译选项的作用。
测试用的Tengine版本下载地址为:http://tengine.taobao.org/download/tengine-1.5.1.tar.gz
./configure –help得到编译选项如下:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
&&--prefix=PATH&&&&&&&&&&&&&&&&&&&&&&set installation prefix&&--sbin-path=PATH&&&&&&&&&&&&&&&&&& set nginx binary pathname&&--conf-path=PATH&&&&&&&&&&&&&&&&&& set nginx.conf pathname&&--error-log-path=PATH&&&&&&&&&&&&&&set error log pathname&&--pid-path=PATH&&&&&&&&&&&&&&&&&&&&set nginx.pid pathname&&--lock-path=PATH&&&&&&&&&&&&&&&&&& set nginx.lock pathname&&&--user=USER&&&&&&&&&&&&&&&&&&&&&&&&set non-privileged user for&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& worker processes&&--group=GROUP&&&&&&&&&&&&&&&&&&&&&&set non-privileged group for&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& worker processes&&&--builddir=DIR&&&&&&&&&&&&&&&&&&&& set build directory&&&--enable-mods-shared=all&&&&&&&&&& enable all the modules to be shared&&--enable-mods-static=all&&&&&&&&&& enable all the modules to be static&&&--dso-path=*)&&&&&&&&&&&&&&&&&&&&&&set dso default load path&&--dso-tool-path=*)&&&&&&&&&&&&&&&& set dso_tool pathname&&&--with-rtsig_module&&&&&&&&&&&&&&&&enable rtsig module&&--with-select_module&&&&&&&&&&&&&& enable select module&&--without-select_module&&&&&&&&&&&&disable select module&&--with-poll_module&&&&&&&&&&&&&&&& enable poll module&&--without-poll_module&&&&&&&&&&&&&&disable poll module&&&--without-procs&&&&&&&&&&&&&&&&&&&&disable procs module&&&--with-file-aio&&&&&&&&&&&&&&&&&&&&enable file AIO support&&--with-ipv6&&&&&&&&&&&&&&&&&&&&&&&&enable IPv6 support&&&--without-syslog&&&&&&&&&&&&&&&&&& disable syslog logging&&&--without-dso&&&&&&&&&&&&&&&&&&&&&&disable dso module load&&&--with-http_realip_module&&&&&&&&&&enable ngx_http_realip_module&&--with-http_addition_module&&&&&&&&enable ngx_http_addition_filter_module&&--with-http_xslt_module&&&&&&&&&&&&enable ngx_http_xslt_filter_module&&--with-http_image_filter_module&&&&enable ngx_http_image_filter_module&&--with-http_geoip_module&&&&&&&&&& enable ngx_http_geoip_module&&--with-http_sub_module&&&&&&&&&&&& enable ngx_http_sub_filter_module&&--with-http_dav_module&&&&&&&&&&&& enable ngx_http_dav_module&&--with-http_flv_module&&&&&&&&&&&& enable ngx_http_flv_module&&--with-http_slice_module&&&&&&&&&& enable ngx_http_slice_module&&--with-http_mp4_module&&&&&&&&&&&& enable ngx_http_mp4_module&&--with-http_gzip_static_module&&&& enable ngx_http_gzip_static_module&&--with-http_concat_module&&&&&&&&&&enable ngx_http_concat_module&&--with-http_random_index_module&&&&enable ngx_http_random_index_module&&--with-http_secure_link_module&&&& enable ngx_http_secure_link_module&&--with-http_degradation_module&&&& enable ngx_http_degradation_module&&--with-http_sysguard_module&&&&&&&&enable ngx_http_sysguard_module&&&--with-http_addition_module=shared enable ngx_http_addition_filter_module (shared)&&--with-http_xslt_module=shared&&&& enable ngx_http_xslt_filter_module (shared)&&--with-http_image_filter_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_image_filter_module (shared)&&--with-http_geoip_module=shared&&&&enable ngx_http_geoip_module&&--with-http_sub_module=shared&&&&&&enable ngx_http_sub_filter_module (shared)&&--with-http_flv_module=shared&&&&&&enable ngx_http_flv_module (shared)&&--with-http_slice_module=shared&&&&enable ngx_http_slice_module (shared)&&--with-http_mp4_module=shared&&&&&&enable ngx_http_mp4_module (shared)&&--with-http_concat_module=shared&& enable ngx_http_concat_module (shared)&&--with-http_random_index_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_random_index_module (shared)&&--with-http_secure_link_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_secure_link_module (shared)&&--with-http_sysguard_module=shared enable ngx_http_sysguard_module (shared)&&--with-http_charset_filter_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_charset_filter_module (shared)&&--with-http_userid_filter_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_userid_filter_module (shared)&&--with-http_footer_filter_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_footer_filter_module (shared)&&--with-http_trim_filter_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_trim_filter_module (shared)&&--with-http_access_module=shared&& enable ngx_http_access_module (shared)&&--with-http_autoindex_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_autoindex_module (shared)&&--with-http_map_module=shared&&&&&&enable ngx_http_map_module (shared)&&--with-http_split_clients_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_split_clients_module (shared)&&--with-http_referer_module=shared&&enable ngx_http_referer_module (shared)&&--with-http_rewrite_module=shared&&enable ngx_http_rewrite_module (shared)&&--with-http_fastcgi_module=shared&&enable ngx_http_fastcgi_module (shared)&&--with-http_uwsgi_module=shared&&&&enable ngx_http_uwsgi_module (shared)&&--with-http_scgi_module=shared&&&& enable ngx_http_scgi_module (shared)&&--with-http_memcached_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_memcached_module (shared)&&--with-http_limit_conn_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_limit_conn_module (shared)&&--with-http_limit_req_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_limit_req_module (shared)&&--with-http_empty_gif_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_empty_gif_module (shared)&&--with-http_browser_module=shared&&enable ngx_http_browser_module (shared)&&--with-http_user_agent_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_user_agent_module (shared)&&--with-http_upstream_ip_hash_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_upstream_ip_hash_module (shared)&&--with-http_upstream_least_conn_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_upstream_least_conn_module (shared)&&--with-http_upstream_session_sticky_module=shared&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& enable ngx_http_upstream_session_sticky_module (shared)&&&--without-http_charset_module&&&&&&disable ngx_http_charset_filter_module&&--without-http_gzip_module&&&&&&&& disable ngx_http_gzip_filter_module&&--without-http_ssi_module&&&&&&&&&&disable ngx_http_ssi_module&&--without-http_ssl_module&&&&&&&&&&disable ngx_http_ssl_module&&--without-http_userid_module&&&&&& disable ngx_http_userid_filter_module&&--without-http_footer_filter_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_footer_filter_module&&--without-http_trim_filter_module&&disable ngx_http_trim_filter_module&&--without-http_access_module&&&&&& disable ngx_http_access_module&&--without-http_auth_basic_module&& disable ngx_http_auth_basic_module&&--without-http_autoindex_module&&&&disable ngx_http_autoindex_module&&--without-http_geo_module&&&&&&&&&&disable ngx_http_geo_module&&--without-http_map_module&&&&&&&&&&disable ngx_http_map_module&&--without-http_split_clients_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_split_clients_module&&--without-http_referer_module&&&&&&disable ngx_http_referer_module&&--without-http_rewrite_module&&&&&&disable ngx_http_rewrite_module&&--without-http_proxy_module&&&&&&&&disable ngx_http_proxy_module&&--without-http_fastcgi_module&&&&&&disable ngx_http_fastcgi_module&&--without-http_uwsgi_module&&&&&&&&disable ngx_http_uwsgi_module&&--without-http_scgi_module&&&&&&&& disable ngx_http_scgi_module&&--without-http_memcached_module&&&&disable ngx_http_memcached_module&&--without-http_limit_conn_module&& disable ngx_http_limit_conn_module&&--without-http_limit_req_module&&&&disable ngx_http_limit_req_module&&--without-http_empty_gif_module&&&&disable ngx_http_empty_gif_module&&--without-http_browser_module&&&&&&disable ngx_http_browser_module&&--without-http_upstream_check_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_check_module&&--without-http_upstream_least_conn_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_least_conn_module&&--without-http_upstream_session_sticky_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_session_sticky_module&&--without-http_upstream_keepalive_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_keepalive_module&&--without-http_upstream_ip_hash_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_ip_hash_module&&--without-http_upstream_consistent_hash_module&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& disable ngx_http_upstream_consistent_hash_module&&--without-http_user_agent_module&& disable ngx_http_user_agent_module&&--without-http_stub_status_module&&disable ngx_http_stub_status_module&&&--with-http_perl_module&&&&&&&&&&&&enable ngx_http_perl_module&&--with-perl_modules_path=PATH&&&&&&set Perl modules path&&--with-perl=PATH&&&&&&&&&&&&&&&&&& set perl binary pathname&&&--with-http_lua_module&&&&&&&&&&&& enable ngx_http_lua_module (will also enable --with-md5 and --with-sha1)&&--with-http_lua_module=shared&&&&&&enable ngx_http_lua_module (shared) (will also enable --with-md5 and --with-sha1)&&--with-luajit-inc=PATH&&&&&&&&&&&& set LuaJIT headers path (where lua.h/lauxlib.h/... are located)&&--with-luajit-lib=PATH&&&&&&&&&&&& set LuaJIT library path (where libluajit-5.1.{a,so} are located)&&--with-lua-inc=PATH&&&&&&&&&&&&&&&&set Lua headers path (where lua.h/lauxlib.h/... are located)&&--with-lua-lib=PATH&&&&&&&&&&&&&&&&set Lua library path (where liblua.{a,so} are located, only support Lua-5.1.x)&&&--with-http_tfs_module&&&&&&&&&&&& enable ngx_http_tfs_module (will also enable --with-md5)&&--with-http_tfs_module=shared&&&&&&enable ngx_http_tfs_module (shared) (will also enable --with-md5)&&--with-libyajl-inc=PATH&&&&&&&&&&&&set libyajl headers path (where yajl.h is located)&&--with-libyajl-lib=PATH&&&&&&&&&&&&set libyajl library path (where libyajl.{a,so} is located)&&&--http-log-path=PATH&&&&&&&&&&&&&& set http access log pathname&&--http-client-body-temp-path=PATH&&set path to store&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& http client request body temporary files&&--http-proxy-temp-path=PATH&&&&&&&&set path to store&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& http proxy temporary files&&--http-fastcgi-temp-path=PATH&&&&&&set path to store&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& http fastcgi temporary files&&--http-uwsgi-temp-path=PATH&&&&&&&&set path to store&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& http uwsgi temporary files&&--http-scgi-temp-path=PATH&&&&&&&& set path to store&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& http scgi temporary files&&&--without-http&&&&&&&&&&&&&&&&&&&& disable HTTP server&&--without-http-cache&&&&&&&&&&&&&& disable HTTP cache&&&--with-mail&&&&&&&&&&&&&&&&&&&&&&&&enable POP3/IMAP4/SMTP proxy module&&--with-mail_ssl_module&&&&&&&&&&&& enable ngx_mail_ssl_module&&--without-mail_pop3_module&&&&&&&& disable ngx_mail_pop3_module&&--without-mail_imap_module&&&&&&&& disable ngx_mail_imap_module&&--without-mail_smtp_module&&&&&&&& disable ngx_mail_smtp_module&&&--with-google_perftools_module&&&& enable ngx_google_perftools_module&&--with-cpp_test_module&&&&&&&&&&&& enable ngx_cpp_test_module&&--with-backtrace_module&&&&&&&&&&&&enable ngx_backtrace_module&&&--add-module=PATH&&&&&&&&&&&&&&&&&&enable an external module&&&--with-cc=PATH&&&&&&&&&&&&&&&&&&&& set C compiler pathname&&--with-cpp=PATH&&&&&&&&&&&&&&&&&&&&set C preprocessor pathname&&--with-cc-opt=OPTIONS&&&&&&&&&&&&&&set additional C compiler options&&--with-ld-opt=OPTIONS&&&&&&&&&&&&&&set additional linker options&&--with-cpu-opt=CPU&&&&&&&&&&&&&&&& build for the specified CPU, valid values:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& pentium, pentiumpro, pentium3, pentium4,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& athlon, opteron, sparc32, sparc64, ppc64&&&--without-pcre&&&&&&&&&&&&&&&&&&&& disable PCRE library usage&&--with-pcre&&&&&&&&&&&&&&&&&&&&&&&&force PCRE library usage&&--with-pcre=DIR&&&&&&&&&&&&&&&&&&&&set path to PCRE library sources&&--with-pcre-opt=OPTIONS&&&&&&&&&&&&set additional build options for PCRE&&--with-pcre-jit&&&&&&&&&&&&&&&&&&&&build PCRE with JIT compilation support&&&--with-md5=DIR&&&&&&&&&&&&&&&&&&&& set path to md5 library sources&&--with-md5-opt=OPTIONS&&&&&&&&&&&& set additional build options for md5&&--with-md5-asm&&&&&&&&&&&&&&&&&&&& use md5 assembler sources&&&--with-sha1=DIR&&&&&&&&&&&&&&&&&&&&set path to sha1 library sources&&--with-sha1-opt=OPTIONS&&&&&&&&&&&&set additional build options for sha1&&--with-sha1-asm&&&&&&&&&&&&&&&&&&&&use sha1 assembler sources&&&--with-zlib=DIR&&&&&&&&&&&&&&&&&&&&set path to zlib library sources&&--with-zlib-opt=OPTIONS&&&&&&&&&&&&set additional build options for zlib&&--with-zlib-asm=CPU&&&&&&&&&&&&&&&&use zlib assembler sources optimized&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& for the specified CPU, valid values:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& pentium, pentiumpro&&&--with-libatomic&&&&&&&&&&&&&&&&&& force libatomic_ops library usage&&--with-libatomic=DIR&&&&&&&&&&&&&& set path to libatomic_ops library sources&&&--with-jemalloc&&&&&&&&&&&&&&&&&&&&force jemalloc library usage&&--with-jemalloc=DIR&&&&&&&&&&&&&&&&set path to jemalloc library files&&&&&--with-openssl=DIR&&&&&&&&&&&&&&&& set path to OpenSSL library sources&&--with-openssl-opt=OPTIONS&&&&&&&& set additional build options for OpenSSL&&&--with-debug&&&&&&&&&&&&&&&&&&&&&& enable debug logging
注:高亮显示为Tengine与Nginx不同的编译选项,Nginx编译选项详见
与本文相关的文章taoCMS是基于php+sqlite/mysql的国内最小(100Kb左右)的功能完善的CMS管理系统
火云邪神语录:天下武功,无坚不破,唯快不破!Nginx的看家本领就是速度,Lua的拿手好戏亦是速度,这两者的结合在速度上无疑有基因上的优势。最先将Nginx,Lua组合到一起的是,它有一个模块,将Lua嵌入到了Nginx里面;随后也包含了ngx_lua模块。至于二者的区别:OpenResty是Nginx的Bundle;而Tengine则是Nginx的Fork。值得一提的是,OpenResty和Tengine均是国人自己创建的项目,前者主要由和开发,后者主要由淘宝打理。至于OpenResty和Tengine孰优孰劣,留给大家自己判断,如下资料可供参考:推荐看看春哥在上关于『』的演讲实录,有料!安装需要最新版的,,,等安装文件。安装Lua或者LuaJIT都是可以的,但是出于效率的考虑,推荐安装LuaJIT。shell& wget http://luajit.org/download/LuaJIT-&VERSION&.tar.gz
shell& tar zxvf LuaJIT-&VERSION&.tar.gz
shell& cd LuaJIT-&VERSION&
shell& make
shell& make install因为安装在缺省路径,所以LuaJIT对应的lib,include均在/usr/local目录里。shell& export LUAJIT_LIB=/usr/local/lib
shell& export LUAJIT_INC=/usr/local/include/luajit-&VERSION&下面就可以编译Nginx了:shell& wget http://nginx.org/download/nginx-&VERSION&.tar.gz
shell& tar zxvf nginx-&VERSION&.tar.gz
shell& cd nginx-&VERSION&
shell& ./configure
--add-module=/path/to/ngx_lua
--add-module=/path/to/ngx_devel_kit
shell& make
shell& make install试着启动一下Nginx看看,如果你运气不好的话,可能会遇到如下错误:cannot open shared object file: No such file or directory这是神马情况?可以用ldd命令来看看:shell& ldd /path/to/nginx
libluajit-&VERSION&.so =& not found此类问题通常使用ldconfig命令就能解决:shell& echo &/usr/local/lib& & /etc/ld.so.conf.d/usr_local_lib.conf
shell& ldconfig再试着启动Nginx看看,应该就OK了。应用我们先用一个简单的程序来暖暖场:把下面的代码加入到Nginx的配置文件nginx.conf,并重启Nginx,然后浏览,就能看到效果了。location /lua {
set $test &hello, world.&;
content_by_lua '
ngx.header.content_type = &text/plain&;
ngx.say(ngx.var.test);
}在深入学习ngx_lua之前,建议大家仔细阅读一遍春哥写的。这里我就说关键的:Nginx配置文件所使用的语言本质上是『声明性的』,而非『过程性的』。Nginx处理请求的时候,指令的执行并不是由定义指令时的物理顺序来决定的,而是取决于指令所属的阶段,Nginx常用的阶段按先后顺序有:rewrite阶段,access阶段,content阶段等等。演示代码中的set指令属于rewrite阶段,content_by_lua指令属于content阶段,如果试着把两条指令的顺序交换一下,会发现程序依然能够正常运行。下面我们尝试结合Redis写个更实战一点的例子。首先,我们需要创建一个Redis配置文件config.json,内容如下:{
&host&: &&HOST&&,
&port&: &&PORT&&
}然后,我们创建一个解析配置文件的脚本init.lua,其中用到了模块:local cjson = require &cjson&;
local config = ngx.shared.
local file = io.open(&config.json&, &r&);
local content = cjson.decode(file:read(&*all&));
file:close();
for name, value in pairs(content) do
config:set(name, value);
end说明:代码里用到了共享内存,这样就不必每次请求都解析一遍配置文件了。接着,我们创建一个渲染内容的脚本content.lua,用到了模块:ngx.header.content_type = &text/plain&;
local redis = require &resty.redis&;
local config = ngx.shared.
local instance = redis:new();
local host = config:get(&host&);
local port = config:get(&port&);
local ok, err = instance:connect(host, port);
if not ok then
ngx.log(ngx.ERR, err);
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
instance:set(&name&, &laowang&);
local name = instance:get(&name&)
instance:close();
ngx.say(&name: &, name);说明:建议把Resty Redis模块放到vendor目录下,稍后在Nginx中统一设置。最后,我们需要在Nginx配置文件里设置一下:lua_shared_dict config 1m;
lua_package_path &/path/to/vendor/?.;&;
init_by_lua_file /path/to/init.
location /lua {
content_by_lua_file /path/to/content.
}说明:为了方便调试,我关闭了lua_code_cache,如果是生产环境,应该开启它。另外,安装CJSON的时候,需要注意Makefile文件里头文件的路径,缺省是:PREFIX = /usr/local
LUA_INCLUDE_DIR = $(PREFIX)/include如果安装的是LuaJIT的话,最好把头文件拷贝到相应目录:cp /usr/local/include/luajit-&VERSION&/* /usr/local/include/…我最近参与的一个项目,提供了一些用于Web轮询的接口,都是用Nginx+Lua实现的,虽然总共只有十几台服务器,但是每天可以提供几十亿次的请求量,贼拉拉的强。最后,让我引用某位屌丝的语录做结束语吧:Lua,未婚男性程序员的最爱。
类别: | 阅读:16914 | 评论:0 |
想收藏或者和大家分享这篇好文章→
taoCMS发布taoCMS2.5Beta5(最后更新13年07月26日),请大家速速升级,欢迎大家试用和提出您宝贵的意见建议。
?请使用新浪微博联系我?
?在github上follow我?为 Nginx 的 JSON API 输出也打开 gzip 压缩 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
已注册用户请 &
在线学习资源
NGINX Modules
为 Nginx 的 JSON API 输出也打开 gzip 压缩
14:01:46 +08:00 · 6253 次点击
Nginx 的 gzip 压缩默认只针对 text/html,如果你用 Nginx 输出 JSON(这应该是目前绝大部分移动应用消耗流量的地方),那么最好是为 JSON 格式也打开 gzip 输出:
gzip_min_length
gzip_buffers
gzip_types
text/plain application/x-javascript text/css application/json application/xml text/
在你的 gzip_types 里加入 application/json 即可,并且 API Server 源在设定 Content-Type 时也要注意。
对于一些内容复杂的 JSON,gzip 还是非常有效的压缩手段。
2 回复 &| &直到
08:00:00 +08:00
& & 21:40:38 +08:00
这也是nginx的好处之一,支持对fastcgi输出进行On the fly压缩
lighttpd就只能压缩静态文件了
& & 13:19:12 +08:00
@ lighttpd不能压缩json吗? 那如果我要用lighttpd gzip传输json,该怎么做呢?
自己实现gzip算法,在response的时候直接output 压缩过的json? 这样的话浏览器能自动解压缩吗?多谢
& · & 1674 人在线 & 最高记录 1946 & · &
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.7.3 · 43ms · UTC 06:46 · PVG 14:46 · LAX 23:46 · JFK 02:46? Do have faith in what you're doing.openresty(nginx)、lua、drizzle调研 - 一片相思林 - 推酷
openresty(nginx)、lua、drizzle调研 - 一片相思林
一、概述:
1.研究目标:nginx中使用lua脚本,及nginx直接访问mysql,redis
2.需要安装的内容:
openresty,mysql,redis
3.OpenResty (也称为 ngx_openresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。
二、安装说明
0.环境准备
$yum install -y gcc gcc-c++ readline-devel pcre-devel openssl-devel tcl perl
1、安装drizzle
cd /usr/local/src/
tar xzvf drizzle-.tar.gz
cd drizzle-/
./configure --without-server
make libdrizzle-1.0
make install-libdrizzle-1.0
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
2、安装openresty
tar xzvf ngx_openresty-1.7.2.1.tar.gz
cd ngx_openresty-1.7.2.1/
./configure --with-http_drizzle_module
gmake install
三、nginx配置nginx.conf
/usr/local/openresty/nginx/conf/nginx.conf
# 添加MySQL配置(drizzle)
upstream backend {
drizzle_server 127.0.0.1:3306 dbname=test user=root password=123456 protocol=
drizzle_keepalive max=200 overflow=ignore mode=
listen&&&&&& 80;
server_name&
#charset koi8-r;
#access_log& logs/host.access.log&
location / {
index& index.html index.
location /lua {
default_type text/
content_by_lua 'ngx.say(&hello, lua&)';
location /lua_redis {
default_type text/
content_by_lua_file /usr/local/lua_test/redis_test.
location /lua_mysql {
default_type text/
content_by_lua_file /usr/local/lua_test/mysql_test.
location @cats-by-name {
set_unescape_uri $name $arg_
set_quote_sql_str $
drizzle_query 'select * from cats where name=$name';
location @cats-by-id {
set_quote_sql_str $id $arg_
drizzle_query 'select * from cats where id=$id';
location = /cats {
access_by_lua '
if ngx.var.arg_name then
return ngx.exec(&@cats-by-name&)
if ngx.var.arg_id then
return ngx.exec(&@cats-by-id&)
rds_json_ret 400 &expecting \&name\& or \&id\& query arguments&;
# 通过url匹配出name,并编码防止注入,最后以json格式输出结果
location ~ '^/mysql/(.*)' {
set $name $1;
set_quote_sql_str $quote_name $
set $sql &SELECT * FROM cats WHERE name=$quote_name&;
drizzle_query $
# 查看MySQL服务状态
location /mysql-status {
四、lua测试脚本
/usr/local/lua_test/redis_test.lua
local redis = require &resty.redis&
local cache = redis.new()
cache.connect(cache, '<span style="color: #7.0.0.1', '<span style="color: #79')
local res = cache:get(&foo&)
if res==ngx.null then
ngx.say(&This is Null&)
ngx.say(res)
/usr/local/lua_test/mysql_test.lua
local mysql = require &resty.mysql&
local db, err = mysql:new()
if not db then
ngx.say(&failed to instantiate mysql: &, err)
db:set_timeout(<span style="color: #00) -- 1 sec
-- or connect to a unix domain socket file listened
-- by a mysql server:
local ok, err, errno, sqlstate =
db:connect{
path = &/path/to/mysql.sock&,
database = &ngx_test&,
user = &ngx_test&,
password = &ngx_test& }
local ok, err, errno, sqlstate = db:connect{
host = &<span style="color: #7.0.0.1&,
port = <span style="color: #06,
database = &test&,
user = &root&,
password = &<span style="color: #3456&,
max_packet_size = <span style="color: #24 * <span style="color: #24 }
if not ok then
ngx.say(&failed to connect: &, err, &: &, errno, & &, sqlstate)
ngx.say(&connected to mysql.&)
local res, err, errno, sqlstate =
db:query(&drop table if exists cats&)
if not res then
ngx.say(&bad result: &, err, &: &, errno, &: &, sqlstate, &.&)
res, err, errno, sqlstate =
db:query(&create table cats &
.. &(id serial primary key, &
.. &name varchar(5))&)
if not res then
ngx.say(&bad result: &, err, &: &, errno, &: &, sqlstate, &.&)
ngx.say(&table cats created.&)
res, err, errno, sqlstate =
db:query(&insert into cats (name) &
.. &values (\'Bob\'),(\'\'),(null)&)
if not res then
ngx.say(&bad result: &, err, &: &, errno, &: &, sqlstate, &.&)
ngx.say(res.affected_rows, & rows inserted into table cats &,
&(last insert id: &, res.insert_id, &)&)
-- run a select query, expected about 10 rows in
-- the result set:
res, err, errno, sqlstate =
db:query(&select * from cats order by id asc&, <span style="color: #)
if not res then
ngx.say(&bad result: &, err, &: &, errno, &: &, sqlstate, &.&)
local cjson = require &cjson&
ngx.say(&result: &, cjson.encode(res))
-- put it into the connection pool of size 100,
-- with 10 seconds max idle timeout
local ok, err = db:set_keepalive(<span style="color: #000, <span style="color: #0)
if not ok then
ngx.say(&failed to set keepalive: &, err)
-- or just close the connection right away:
-- local ok, err = db:close()
-- if not ok then
ngx.say(&failed to close: &, err)
五、验证结果
hello, lua
$ redis-cli set foo 'hello,lua-redis'
hello,lua-redis
connected to mysql.
table cats created.
3 rows inserted into table cats (last insert id: 1)
result: [{&name&:&Bob&,&id&:&1&},{&name&:&&,&id&:&2&},{&name&:null,&id&:&3&}]
{&errcode&:400,&errstr&:&expecting \&name\& or \&id\& query arguments&}
[{&id&:1,&name&:&Bob&}]
[{&id&:2,&name&:&&}]
[{&id&:1,&name&:&Bob&}]
worker process: 32261
upstream backend
active connections: 0
connection pool capacity: 0
servers: 1
六、参考资料
1.openresty
如何安装nginx_lua_module模块
nginx+lua 项目使用记(二)
nginx_lua模块基于mysql数据库动态修改网页内容
突破log_by_lua中限制Cosocket API的使用
17173 Ngx_Lua使用分享
关于 OPENRESTY 的两三事
Nginx 第三方模块-漫谈缘起
CentOS6.4 安装OpenResty和Redis 并在Nginx中利用lua简单读取Redis数据
Nginx与Lua
由Lua 粘合的Nginx生态环境
Nginx 第三方模块试用记
agentzh 的 Nginx 教程(版本 )
CentOS下Redis 2.2.14安装配置详解
http://blog.csdn.net/gaojinshan/article/details/
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致

我要回帖

更多关于 ngx openresty 的文章

 

随机推荐