Total Pageviews

Friday, 2 December 2011

OpenResty is a full-fledged web application server

http://openresty.org/ (http://agentzh.org)

OpenResty (aka. ngx_openresty) is a full-fledged web application server by bundling the standard Nginx core, lots of 3rd-party Nginx modules, as well as most of their external dependencies.

By taking adantage of various well-designed Nginx modules, OpenResty effectively turns the nginx server into a powerful web app server, in which the web developers can use the Lua programming language to script various existing nginx C modules and Lua modules and construct extremely high-performance web applications that is capable to handle 10K+ connections.

OpenResty aims to run your server-side web app completely in the Nginx server, leveraging Nginx's event model to do non-blocking I/O not only with the HTTP clients, but also with remote backends like MySQL, PostgreSQL, Memcached, and Redis.

See Components for the complete list of software bundled in OpenResty.

See GettingStarted on how to quickly setup an OpenResty server that can say hello world over HTTP. Or you can go to the Download section to grab OpenResty's source code tarball directly.
---------------------------------

OpenResty实现防cc攻击




我们知道防cc攻击主要包括两部分,一是限制请求速度,二是给用户发送js跳转代码进行验证请求是否合法。
一、安装依赖 centos:

yum install readline-devel pcre-devel openssl-devel

ubuntu:

apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

二、luajit安装 

cd /tmp/
git clone http://luajit.org/git/luajit-2.0.git
cd luajit-2.0/
make && make install
ln -sf luajit-2.0.0-beta10 /usr/local/bin/luajit
ln -sf /usr/local/lib/libluajit-5.1.so.2 /usr/lib/

三、openresty安装

cd /tmp
wget http://agentzh.org/misc/nginx/ngx_openresty-1.2.4.13.tar.gz
tar xzf ngx_openresty-1.2.4.13.tar.gz
cd ngx_openresty-1.2.4.13/
./configure --prefix=/usr/local/openresty --with-luajit
make && make install

四、nginx配置nginx.conf:

http{
[......]
lua_shared_dict limit 10m;
lua_shared_dict jsjump 10m;

    server {
#lua_code_cache off;
        listen       80;
        server_name  www.centos.bz;

        location / {
default_type  text/html;
content_by_lua_file "/usr/local/openresty/nginx/conf/lua";
        }
        location @cc {
            internal;
            root   html;
            index  index.html index.htm;
        }
    }
}

/usr/local/openresty/nginx/conf/lua文件:

local ip = ngx.var.binary_remote_addr
local limit = ngx.shared.limit
local req,_=limit:get(ip)
if req then
    if req > 20 then
        ngx.exit(503)
    else
        limit:incr(ip,1)
    end
else
    limit:set(ip,1,10)
end

local jsjump = ngx.shared.jsjump
local uri = ngx.var.request_uri
local jspara,flags=jsjump:get(ip)
local args = ngx.req.get_uri_args()
if jspara then
    if flags then
        ngx.exec("@cc")
    else
        if args["jskey"] and args["jskey"]==tostring(jspara) then
            jsjump:set(ip,jspara,3600,1)
            ngx.exec("@cc")
        else
            local url=''
            if ngx.var.args then
                url=ngx.var.scheme.."://"..ngx.var.host..ngx.var.request_uri.."&jskey="..jspara
            else
                url=ngx.var.scheme.."://"..ngx.var.host..ngx.var.request_uri.."?jskey="..jspara
            end
            local jscode="<script>window.location.href='"..url.."';</script>"   
            ngx.say(jscode)
        end
    end
else
    local random=math.random(100000,999999)
    jsjump:set(ip,random,60)
    local url=''
    if ngx.var.args then
        url=ngx.var.scheme.."://"..ngx.var.host..ngx.var.request_uri.."&jskey="..random
    else
        url=ngx.var.scheme.."://"..ngx.var.host..ngx.var.request_uri.."?jskey="..random
    end
    local jscode="<script>window.location.href='"..url.."';</script>"   
    ngx.say(jscode)
end

lua代码部分解释:
1、1-12行是限速功能实现,第5和第10行表示10秒钟内容最多只能请求20次。
2、14-48行是验证部分,24行中的3600表示验证通过后,白名单时间为3600秒,即1小时.
------------
相关帖子:http://briteming.blogspot.com/2014/05/ngxopenresty-turning-nginx-into-full.html
----------
https://github.com/openresty/lua-nginx-module
https://github.com/laozi2/nginx-tcp-lua-module
------------------------
 

High Performance Web Platform Based on Nginx and LuaJIT

openresty.org  
 

Name

OpenResty - Turning Nginx into a Full-Fledged Scriptable Web Platform

Table of Contents

Description

OpenResty is a full-fledged web application server by bundling the standard nginx core, lots of 3rd-party nginx modules, as well as most of their external dependencies.

This bundle is maintained by Yichun Zhang (agentzh).

Because most of the nginx modules are developed by the bundle maintainers, it can ensure that all these modules are played well together.

The bundled software components are copyrighted by the respective copyright holders.

The homepage for this project is on openresty.org.

For Users

Visit the download page on the openresty.org web site to download the latest bundle tarball, and follow the installation instructions in the installation page.

For Bundle Maintainers

The bundle's source is at the following git repository:

https://github.com/openresty/openresty

To reproduce the bundle tarball, just do

make

at the top of the bundle source tree.

Please note that you may need to install some extra dependencies, like perl, dos2unix, and mercurial. On Fedora 22, for example, installing the dependencies is as simple as running the following commands:

sudo dnf install perl dos2unix mercurial

Back to TOC

Additional Features

In additional to the standard nginx core features, this bundle also supports the following:

Back to TOC

resolv.conf parsing

syntax: resolver address ... [valid=time] [ipv6=on|off] [local=on|off|path]

default: -

context: http, stream, server, location

Similar to the resolver directive in standard nginx core with additional support for parsing additional resolvers from the resolv.conf file format.

When local=on, the standard path of /etc/resolv.conf will be used. You may also specify arbitrary path to be used for parsing, for example: local=/tmp/test.conf.

When local=off, parsing will be disabled (this is the default).

This feature is not available on Windows platforms.

Back to TOC

Mailing List

You're very welcome to join the English OpenResty mailing list hosted on Google Groups:

https://groups.google.com/group/openresty-en

The Chinese mailing list is here:

https://groups.google.com/group/openresty

Back to TOC

Report Bugs

You're very welcome to report issues on GitHub:

https://github.com/openresty/openresty/issues

from https://github.com/openresty/openresty 

-----------------------------------------------------

Web服务器OpenResty

OpenResty( 又名 ngx_openresty)是打包了标准 Nginx 核心的开源Web服务器。基于 Lua 编程语言对 Nginx 核心以及现有的各种 Nginx C 模块进行脚本编程,构建出可以处理一万以上并发请求的极端高性能的 Web 应用。借助OpenResty,开发人员不需要了解Nginx核心和写复杂的C/C++模块,只需使用Lua语言既可进行Web应用开发。主要由章亦春(agentzh)维护。在 2011 年之前曾由淘宝网赞助,在后来的 2012 ~ 2016 年间主要由美国的 CloudFlare 公司 提供支持。目前,OpenResty® 主要由 OpenResty 软件基金会和 OpenResty Inc. 公司提供支持。

OpenResty 将服务器端应用完全运行于 Nginx 服务器中,充分利用 Nginx 的事件模型来进行非阻塞 I/O 通信。不仅仅是和 HTTP 客户端间的网络通信是非阻塞的,与MySQL、PostgreSQL、Memcached、以及 Redis 等众多远方后端之间的网络通信也是非阻塞的。

[repo owner=”openresty” name=”openresty.org”]