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 等都进行一致的高性能响应。
前几年,在看老罗的T1发布会时,听说了OpenResty。最近,看到很多大公司关于OpenResty的使用,便尝试一下。
1、安装 创建不登录用户
1 useradd -s /sbin/nologin -M nginx
安装依赖包
1 2 apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
如果执行失败,提示找不到镜像资源,可以先更新一下系统环境,再重新执行上一步。
1 2 apt-get update apt-get upgrade
下载OpenResty
1 2 3 4 5 cd /usr/local /srcwget https://openresty.org/download/openresty-1.11.2.2.tar.gz tar -zxvf openresty-1.11.2.2.tar.gz
下载第三方模块包
1 2 3 4 5 6 7 8 9 cd /usr/local /src/openresty-1.11.2.2/bundlewget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.tar.gz wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/v0.3.0.tar.gz wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip tar -zxvf 2.3.tar.gz tar -zxvf v0.3.0.tar.gz unzip nginx-http-concat-master.zip
安装LuaJIT
1 2 3 cd /usr/local /src/openresty-1.11.2.2/bundle/LuaJIT-2.1-20161104/make clean && make && make install
修改nginx-http-concat
mod_concat
模块由淘宝开发,目前已经包含在tengine
中,并且淘宝已经在使用这个nginx模块。不过塔暂时没有包含在nginx中。这个模块类似于apache中的modconcat。如果需要使用它,需要使用两个”?”问号。
1 http://example.com/??style1.css,style2.css,foo/style3.css
以上将原先3个请求合并为1个请求。
如果你担心文件被用户的浏览器缓存而没有及时更新,你依旧可以带上一个版本号的参数,如下:
1 http://example.com/??style1.css,style2.css,foo/style3.css?v=102234
由于nginx-http-concat
中js的MIME-TYPE
用的是application/x-javascript
,属于比较老的类型,需要修改成application/javascript
。否则,在合并js时会报400错误。
1 2 3 4 5 6 cd /usr/local /src/openresty-1.11.2.2/bundle/nginx-http-concat-mastervim ngx_http_concat_module.c static ngx_str_t ngx_http_concat_default_types[] = { ngx_string("application/x-javascript" ), ngx_string("text/css" ), ngx_null_string };
修改成
1 2 3 static ngx_str_t ngx_http_concat_default_types[] = { ngx_string("application/javascript" ), ngx_string("text/css" ), ngx_null_string };
安装openResty:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 cd /usr/local /src/openresty-1.11.2.2./configure \ --prefix=/usr/local /openresty \ --user=nginx \--group=nginx \ --with-http_realip_module \ --with-pcre \ --with-luajit \ --with-http_postgres_module \ --with-http_iconv_module \ --with-stream \ --add-module=./bundle/ngx_cache_purge-2.3/ \ --add-module=./bundle/nginx_upstream_check_module-0.3.0/ \ --add-module=./bundle/nginx-http-concat-master make && make install
如果安装过程中出现类似以下的错误提示:
1 checking for libpq library version 9.1 ... not foundngx_postgres addon was unable to detect version of the libpq library.
说明需要安装postgresql:
1 2 apt-get install -f postgresql-server-dev-9.xapt-get install -f postgresql-9.x
查看已安装模块
1 cd /usr/local/openresty/nginx./sbin/nginx -V | tr ' ' '\n'
修改nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 user nginx;worker_processes auto;worker_cpu_affinity auto;error_log logs/error.log error ;pid logs/nginx.pid;worker_rlimit_nofile 65536 ;events { use epoll ; worker_connections 65536 ; multi_accept on ; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr\t$http_x_forwarded_for\t[$time_local]\t"$request"\t"-"\t$status\t"$http_user_agent"\t"$http_referer"\t$request_time' ; access_log logs/access.log main; open_file_cache max=65536 inactive=120s ; open_file_cache_min_uses 1 ; open_file_cache_valid 30s ; sendfile on ; tcp_nopush on ; tcp_nodelay on ; server_tokens off ; keepalive_timeout 120 ; keepalive_requests 10000 ; client_header_timeout 10 ; client_body_timeout 10 ; reset_timedout_connection on ; send_timeout 10 ; server_names_hash_bucket_size 128 ; client_header_buffer_size 32k ; client_max_body_size 300m ; large_client_header_buffers 4 32k ; gzip on ; gzip_min_length 1k ; gzip_buffers 4 16k ; gzip_http_version 1 .0 ; gzip_comp_level 3 ; gzip_types text/plain application/x-javascript application/javascript text/javascript text/css application/xml image/jpeg image/gif image/png; gzip_vary on ; upstream my_service { server 192.168.1.2:80 ; keepalive 100 ; } }
对应的/work/app/nginx-app/data/config/nginx-test.conf
配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 server { listen 80 ; server_name www.test.com; access_log /work/app/nginx-app/test/logs/access_data.log main; location /lua { default_type 'text/html' ; content_by_lua 'ngx.say("hello world")' ; } location /test { proxy_pass http://my_service; proxy_redirect off ; proxy_http_version 1 .1 ; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true ; proxy_connect_timeout 10 ; proxy_read_timeout 10 ; proxy_send_timeout 10 ; proxy_intercept_errors on ; } error_page 404 /work/app/nginx-app/data/assets/html/404 .html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /work/app/nginx-app/data/assets/html; } } }
验证
1 cd /usr/local /openresty/nginx/./sbin/nginx -t
1 nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
2、第三方模板 Html模板解析:resty.template
1 cd /usr/local /openresty/lualib/restywget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template.luamkdir htmlcd htmlwget https://raw.githubusercontent.com/bungle/lua-resty-template/master/lib/resty/template/html.lua
Lua包管理器:Luarocks
1 2 3 4 5 6 7 8 9 cd /opt/apps/wget http://luarocks.github.io/luarocks/releases/luarocks-2.4.2.tar.gztar -xzvf luarocks-2.4.2.tar.gz cd luarocks-2.4.2/./configure --prefix=/usr/local /luarocks --with-lua=/usr/local --lua-suffix=jit --with-lua-include=/usr/local /include/luajit-2.1make build make install
通过 LuaRocks安装 Lua MD5 库。
1 cd /usr/local /luarocksbin/luarocks install md5
Kafka模块:lua-resty-kafka
请看nginx+lua+kafka实现日志统一收集汇总 。
参考