Nginx on steroids-LUAで機能を拡張

すべての外部製品の動作を保証するために、人気のあるnginxを使用しています。 高速で信頼性があります。 それにほとんど問題はありません。 当社の製品も絶えず進化しており、新しいサービスが登場し、新しい機能が追加され、古いものが拡張されています。 視聴者と負荷は増え続けています。 ここで、影響を受けるアプリケーションの可用性とフォールトトレランスを維持しながら、開発を加速し、生産性を大幅に向上させ、サービスにこの新しい機能を簡単に追加できるようにする方法について説明します。 「nginx as web application」の概念についてです。

すなわち、 サードパーティのモジュール(主にLUA)について、完全に魔法のようなことを迅速かつ確実に行うことができます。

画像





問題と解決策


基本的な考え方は非常に簡単です。 次の要因を考慮してください。

-アプリケーションロジックの複雑さ、

-アプリケーションコンポーネントの数、

-オーディエンスのサイズ。

ある時点から、アプリケーションの応答性と高速性、時には機能性を維持することは非常に困難になります。 製品はマルチコンポーネントになり、地理的に分散されます。 そして、ますます多くの人々がそれを使用しています。 同時に、応答性とフォールトトレランスに関するビジネス要件があり、最初にそれを遵守する必要があります。

この問題を解決するにはいくつかの方法があります。 あなたはすべてを壊し、他の技術でそれを作り直すことができます。 もちろん、このオプションは機能しますが、あまり気に入らなかったため、徐々にやり直すことにしました。 基本は、アセンブリのオープンレスト (nginx + LUA)です。 なぜLUA。 cgi、fastcgi、およびその他のcgiの助けがなくても、強力で美しく、高速な機能をnginx構成ファイルに直接スクリプト化できます。 すべてが非同期に動作します。 そして、顧客だけでなく、バ​​ックエンドにも。 同時に、Webサーバーのイベントループに干渉することなく、コールバックなしで、既存のnginx機能を完全に使用します。



現在、次のバックエンドが利用可能です。

-redis

-memcache

-MySQL

-PostgreSQL

さらに、 RabbitMQZeroMQなど、使用するモジュールをさらに接続できます。

非常に高速に動作します。 とにかく、php-fpmより高速))



論理的な質問は、Cですべてをすべて書き換えてみませんか? LUAでの書き込みは、はるかに簡単で高速です。 そして、非同期およびnginxイベントループに関連する問題をすぐに回避できます。



例。 アイデア


通常どおり、完全なコードは提供せず、主要部分のみを提供します。 これらはすべて、以前はphpで行われていました。



1.この部分は、同僚のAotDによって発明され作成されました 。 画像のリポジトリがあります。 ユーザーに表示する必要があり、サイズ変更などのいくつかの操作を実行することをお勧めします。 写真をcephに保存します。これはAmazon S3の類似物です。 ImageMagickは画像処理に使用されます。 リサイザーにキャッシュディレクトリがあり、処理された画像がそこに追加されます。

ユーザーのリクエストを解析し、写真、ユーザーが必要とする解像度を決定してcephに進み、その場で処理して表示します。

serve_image.lua

require "config" local function return_not_found(msg) ngx.status = ngx.HTTP_NOT_FOUND if msg then ngx.header["X-Message"] = msg end ngx.exit(0) end local name, size, ext = ngx.var.name, ngx.var.size, ngx.var.ext if not size or size == '' then return_not_found() end if not image_scales[size] then return_not_found('Unexpected image scale') end local cache_dir = static_storage_path .. '/' .. ngx.var.first .. '/' .. ngx.var.second .. '/' local original_fname = cache_dir .. name .. ext local dest_fname = cache_dir .. name .. size .. ext -- make sure the file exists local file = io.open(original_fname) if not file then -- download file contents from ceph ngx.req.read_body() local data = ngx.location.capture("/ceph_loader", {vars = { name = name .. ext }}) if data.status == ngx.HTTP_OK and data.body:len()>0 then os.execute( "mkdir -p " .. cache_dir ) local original = io.open(original_fname, "w") original:write(data.body) original:close() else return_not_found('Original returned ' .. data.status) end end local magick = require("imagick") magick.thumb(original_fname, image_scales[size], dest_fname) ngx.exec("@after_resize")
      
      







imagic.lua バインディングを接続します。 LuaJITにアクセスできる必要があります。



nginx_partial_resizer.conf.template

 # Old images location ~ ^/(small|small_wide|medium|big|mobile|scaled|original|iphone_(preview|retina_preview|big|retina_big|small|retina_small))_ { rewrite /([^/]+)$ /__CEPH_BUCKET__/$1 break; proxy_pass __UPSTREAM__; } # Try get image from ceph, then from local cache, then from scaled by lua original # If image test.png is original, when user wants test_30x30.png: # 1) Try get it from ceph, if not exists # 2) Try get it from /cache/t/es/test_30x30.ong, if not exists # 3) Resize original test.png and put it in /cache/t/es/test_30x30.ong location ~ ^/(?<name>(?<first>.)(?<second>..)[^_]+)((?<size>_[^.]+)|)(?<ext>\.[a-zA-Z]*)$ { proxy_intercept_errors on; rewrite /([^/]+)$ /__CEPH_BUCKET__/$1 break; proxy_pass __UPSTREAM__; error_page 404 403 = @local; } # Helper failover location for upper command cause you can't write # try_files __UPSTREAM__ /cache/$uri @resizer =404; location @local { try_files /cache/$first/$second/$name$size$ext @resize; } # If scaled file not found in local cache resize it with lua magic! location @resize { # lua_code_cache off; content_by_lua_file "__APP_DIR__/lua/serve_image.lua"; } # serve scaled file, invoked in @resizer serve_image.lua location @after_resize { try_files /cache/$first/$second/$name$size$ext =404; } # used in @resizer serve_image.lua to download original image # $name contains original image file name location =/ceph_loader { internal; rewrite ^(.+)$ /__CEPH_BUCKET__/$name break; proxy_set_header Cache-Control no-cache; proxy_set_header If-Modified-Since ""; proxy_set_header If-None-Match ""; proxy_pass __UPSTREAM__; } location =/favicon.ico { return 404; } location =/robots.txt {}
      
      







2. APIのファイアウォール。 検証、顧客識別、rps制御、および不要なものに対する障壁を要求します。

Firewall.lua

 module(..., package.seeall); local function ban(type, element) CStorage.banPermanent:set(type .. '__' .. element, 1); ngx.location.capture('/postgres_ban', { ['vars'] = { ['type'] = type, ['value'] = element} }); end local function checkBanned(apiKey) -- init search criteria local searchCriteria = {}; searchCriteria['key'] = apiKey; if ngx.var.remote_addr then searchCriteria['ip'] = ngx.var.remote_addr; end; -- search in ban lists for type, item in pairs(searchCriteria) do local storageKey = type .. '__' .. item; if CStorage.banPermanent:get(storageKey) then ngx.exit(444); elseif CStorage.banTmp:get(storageKey) then -- calculate rps and check is our client still bad boy 8-) local rps = CStorage.RPS:incr(storageKey, 1); if not(rps) then CStorage.RPS:set(storageKey, 1, 1); rps=1; end; if rps then if rps > config.app_params['ban_params']['rps_for_ip_to_permanent_ban'] then CStorage.RPS:delete(storageKey); ban(type, item); ngx.exit(444); elseif config.app_params['ban_params']['rps_for_ip_to_tmp_ban'] > 0 and rps == config.app_params['ban_params']['rps_for_ip_to_tmp_ban'] then local attemptsCount = CStorage.banTmp:incr(storageKey, 1) - 1; if attemptsCount > config.app_params['ban_params']['tmp_ban']['max_attempt_to_exceed_rps'] then -- permanent ban CStorage.banTmp:delete(storageKey); ban(type, item); end; end; end; ngx.exit(444); end; end; end; local function checkTemporaryBlocked(apiKey) local blockedData = CStorage.tmpBlockedDemoKeys:get(apiKey); if blockedData then --storage.tmpBlockedDemoKeys:incr(apiKey, 1); -- think about it. return CApiException.throw('tmpDemoBlocked'); end; end; local function checkRPS(apiKey) local rps = nil; -- check rps for IP and ban it if it's needed if ngx.var.remote_addr then local ip = 'ip__' .. tostring(ngx.var.remote_addr); rps = CStorage.RPS:incr(ip, 1); if not(rps) then CStorage.RPS:set(ip, 1, 1); rps = 1; end; if rps > config.app_params['ban_params']['rps_for_ip_to_permanent_ban'] then ban('ip', tostring(ngx.var.remote_addr)); ngx.exit(444); elseif config.app_params['ban_params']['rps_for_ip_to_tmp_ban'] > 0 and rps > config.app_params['ban_params']['rps_for_ip_to_tmp_ban'] then CStorage.banTmp:set(ip, 1, config.app_params['ban_params']['tmp_ban']['time']); ngx.exit(444); end; end; local apiKey_key_storage = 'key_' .. apiKey['key']; -- check rps for key rps = CStorage.RPS:incr(apiKey_key_storage, 1); if not(rps) then CStorage.RPS:set(apiKey_key_storage, 1, 1); rps = 1; end; if apiKey['max_rps'] and rps > tonumber(apiKey['max_rps']) then if apiKey['mode'] == 'demo' then CApiKey.blockTemporary(apiKey['key']); return CApiException.throw('tmpDemoBlocked'); else CApiKey.block(apiKey['key']); return CApiException.throw('blocked'); end; end; -- similar check requests per period (RPP) for key if apiKey['max_request_count_per_period'] and apiKey['period_length'] then local rpp = CStorage.RPP:incr(apiKey_key_storage, 1); if not(rpp) then CStorage.RPP:set(apiKey_key_storage, 1, tonumber(apiKey['period_length'])); rpp = 1; end; if rpp > tonumber(apiKey['max_request_count_per_period']) then if apiKey['mode'] == 'demo' then CApiKey.blockTemporary(apiKey['key']); return CApiException.throw('tmpDemoBlocked'); else CApiKey.block(apiKey['key']); return CApiException.throw('blocked'); end; end; end; end; function run() local apiKey = ngx.ctx.REQUEST['key']; if not(apiKey) then return CApiException.throw('unauthorized'); end; apiKey = tostring(apiKey) -- check permanent and temporary banned checkBanned(apiKey); -- check api key apiKey = CApiKey.getData(apiKey); if not(apiKey) then return CApiException.throw('forbidden'); end; apiKey = JSON:decode(apiKey); if not(apiKey['is_active']) then return CApiException.throw('blocked'); end; apiKey['key'] = tostring(apiKey['key']); -- check is key in tmp blocked list if apiKey['mode'] == 'demo' then checkTemporaryBlocked(apiKey['key']); end; -- check requests count per second and per period checkRPS(apiKey); -- set apiKey's json to global parameter; in index.lua we send it through nginx to php application ngx.ctx.GLOBAL['api_key'] = JSON:encode(apiKey); end;
      
      







Validator.lua

 module(..., package.seeall); local function checkApiVersion() local apiVersion = ''; if not (ngx.ctx.REQUEST['version']) then local nginx_request = tostring(ngx.var.uri); local version = nginx_request:sub(2,4); if tonumber(version:sub(1,1)) and tonumber(version:sub(3,3)) then apiVersion = version; else return CApiException.throw('versionIsRequired'); end; else apiVersion = ngx.ctx.REQUEST['version']; end; local isSupported = false; for i, version in pairs(config.app_params['supported_api_version']) do if apiVersion == version then isSupported = true; end; end; if not (isSupported) then CApiException.throw('unsupportedVersion'); end; ngx.ctx.GLOBAL['api_version'] = apiVersion; end; local function checkKey() if not (ngx.ctx.REQUEST['key']) then CApiException.throw('unauthorized'); end; end; function run() checkApiVersion(); checkKey(); end;
      
      







Apikey.lua

 module ( ..., package.seeall ) function init() if not(ngx.ctx.GLOBAL['CApiKey']) then ngx.ctx.GLOBAL['CApiKey'] = {}; end end; function flush() CStorage.apiKey:flush_all(); CStorage.apiKey:flush_expired(); end; function load() local dbError = nil; local dbData = ngx.location.capture('/postgres_get_keys'); dbData = dbData.body; dbData, dbError = rdsParser.parse(dbData); if dbData ~= nil then local rows = dbData.resultset if rows then for i, row in ipairs(rows) do local cacheKeyData = {}; for col, val in pairs(row) do if val ~= rdsParser.null then cacheKeyData[col] = val; else cacheKeyData[col] = nil; end end CStorage.apiKey:set(tostring(cacheKeyData['key']),JSON:encode(cacheKeyData)); end; end; end; end; function checkNotEmpty() if not(ngx.ctx.GLOBAL['CApiKey']['loaded']) then local cnt = CHelper.tablelength(CStorage.apiKey:get_keys(1)); if cnt == 0 then load(); end; ngx.ctx.GLOBAL['CApiKey']['loaded'] = 1; end; end; function getData(key) checkNotEmpty(); return CStorage.apiKey:get(key); end; function getStatus(key) key = getData(key); local result = ''; if key ~= nil then key = JSON:decode(key); if key['is_active'] ~= nil and key['is_active'] == true then result = 'allowed'; else result = 'blocked'; end; else result = 'forbidden'; end; return result; end; function blockTemporary(apiKey) apiKey = tostring(apiKey); local isset = getData(apiKey); if isset then CStorage.tmpBlockedDemoKeys:set(apiKey, 1, config.app_params['ban_params']['time_demo_apikey_block_tmp']); end; end; function block(apiKey) apiKey = tostring(apiKey); local keyData = getData(apiKey); if keyData then ngx.location.capture('/redis_get', { ['vars'] = { ['key'] = apiKey } }); keyData['is_active'] = false; CStorage.apiKey:set(apiKey,JSON:encode(cacheKeyData)); end; end;
      
      







Storages.lua

 module ( ..., package.seeall ) apiKey = ngx.shared.apiKey; RPS = ngx.shared.RPS; RPP = ngx.shared.RPP; banPermanent = ngx.shared.banPermanent; banTmp = ngx.shared.banTmp; tmpBlockedDemoKeys = ngx.shared.tmpBlockedDemoKeys;
      
      







3. AMQPプロトコルを介したコンポーネント間通信などの追加サービス。 例はこちらです。



4. すでに書いたように 。 バックエンドを介してリクエストを渡すためのルートを「スマートに」制御する機能を備えたアプリケーション自己診断モジュール。 まだ開発中です。



5. API用アダプター。 場合によっては、既存の方法を微調整、補足、または拡張する必要があります。 すべてを書き換えないために、 LUAが役立ちます 。 たとえば、オンザフライでのjson <-> xml変換。



6 ... さらに多くのアイデアがあります。



そのようなベンチマークはありません。 製品は複雑すぎるため、ベンチマーク後のrpは多くの要因に大きく依存しています。 ただし、当社の製品では、影響を受ける機能の生産性が20倍に向上し、場合によってはすべてが最大200倍高速になりました。



長所と短所


有形のプラス。 phpで5メガバイトのコードであったものはすべて、luaで100kbファイルに変わります。

-開発速度、

-アプリケーションの速度、

-信頼性

-イベントループnginxを中断せずに、クライアントとバックエンドとの非同期動作、

-LUAシュガーは気持ちいい! コルーチン、すべてのnginxフォークの共有辞書、サブクエスト、たくさんのバインダー。



目に見えない短所。

-すべてを注意深く行い、非同期とイベントループnginxについて覚えておく必要があります。

-フロントエンドは非常に高速であるため、バックエンドは気に入らないかもしれません。 レイヤーなしで、それらの間には直接接続があります。 たとえば、フロントエンドで1秒あたり10,000リクエストのLUAが簡単に機能すると確信しています。 ただし、同時に基地に行きたい場合は、問題が発生する可能性があります。

-問題が発生した場合、デバッグするのはかなり困難です。



ちなみに、この記事の執筆中に、その瞬間、プログラマーこのすべてについて高負荷について詳しく語っています。



コメントで質問にお答えします。



最後に、 ここでトピックに関する情報の小さな選択を見つけることができます



All Articles