Nginx+Lua: различия между версиями

Материал из Artem Aleksashkin's Wiki
Перейти к навигации Перейти к поиску
Нет описания правки
Нет описания правки
 
Строка 34: Строка 34:
local expires = 3600 * 24  -- 1 day
local expires = 3600 * 24  -- 1 day
ngx.header["Set-Cookie"] = "session=blah; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expires)
ngx.header["Set-Cookie"] = "session=blah; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expires)
</pre>
<pre>
local curl = require "curl"
local request = curl.easy_init()
local request_url = 'https://httpbin.org/post'
local request_post_data = 'test=1&test2=2'
local request_headers = {
    "Connection: close",
    "Accept: application/json",
    "Cache-Control: no-cache",
    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
    "Content-Length: " .. string.len(request_post_data),
}
local responce_headers_chunks = {}
local responce_data_chunks = {}
local responce_headers = ""
local responce_data = ""
-- ------------------------------------------------------------------
request:setopt(curl.OPT_URL, request_url)
request:setopt(curl.OPT_HTTPHEADER, request_headers)
request:setopt(curl.OPT_POST, 1)
request:setopt(curl.OPT_POSTFIELDS, request_post_data);
request:setopt(curl.OPT_CONNECTTIMEOUT, 10)
request:setopt(curl.OPT_FOLLOWLOCATION, 1)
request:setopt(curl.OPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36")
request:setopt(curl.OPT_SSL_VERIFYPEER, 0)
request:setopt(curl.OPT_HEADERFUNCTION, function (param, buf)
    table.insert(responce_headers_chunks, param)
    return buf
end)
request:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
    table.insert(responce_data_chunks, param)
    return buf
end)
assert(request:perform())
responce_headers = table.concat(responce_headers_chunks)
responce_data = table.concat(responce_data_chunks)
-- ------------------------------------------------------------------
local json = require('cjson')
print(responce_headers)
print(responce_data)
print(json.decode(responce_data)['form']['test'])
</pre>
</pre>

Текущая версия от 04:03, 16 декабря 2018

Идея построить сайт/сервис на луа + nginx подогревала меня давно, т.к. должно получиться очень быстро. Понемногу раскрываю тему.

lua_package_path ";;/home/artem/projects/blockbot/?.lua;";
server
{
	listen 80;
	server_name blockbot;
	access_log  /var/log/nginx/blockbot.access.log;
	error_log   /var/log/nginx/blockbot.error.log info;

	client_max_body_size 1024m;

	location /
	{
	    lua_code_cache off;
	    set_by_lua_file         $suspicious     /home/artem/projects/blockbot/suspicious.lua;
	    if ($suspicious) {
	        content_by_lua_file /home/artem/projects/blockbot/captcha.lua;
	    }
		proxy_set_header        Host ecommercemarket;
		proxy_set_header        X-Real-IP       $remote_addr;
		proxy_set_header        X-Forwarded-For $remote_addr;
		proxy_pass              http://127.0.0.1:8080;
	}
}
local expires = 3600 * 24  -- 1 day
ngx.header["Set-Cookie"] = "session=blah; Path=/; Expires=" .. ngx.cookie_time(ngx.time() + expires)
local curl = require "curl"
local request = curl.easy_init()
local request_url = 'https://httpbin.org/post'
local request_post_data = 'test=1&test2=2'
local request_headers = {
    "Connection: close",
    "Accept: application/json",
    "Cache-Control: no-cache",
    "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
    "Content-Length: " .. string.len(request_post_data),
}
local responce_headers_chunks = {}
local responce_data_chunks = {}
local responce_headers = ""
local responce_data = ""


-- ------------------------------------------------------------------
request:setopt(curl.OPT_URL, request_url)
request:setopt(curl.OPT_HTTPHEADER, request_headers)
request:setopt(curl.OPT_POST, 1)
request:setopt(curl.OPT_POSTFIELDS, request_post_data);
request:setopt(curl.OPT_CONNECTTIMEOUT, 10)
request:setopt(curl.OPT_FOLLOWLOCATION, 1)
request:setopt(curl.OPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36")
request:setopt(curl.OPT_SSL_VERIFYPEER, 0)
request:setopt(curl.OPT_HEADERFUNCTION, function (param, buf)
    table.insert(responce_headers_chunks, param)
    return buf
end)
request:setopt(curl.OPT_WRITEFUNCTION, function (param, buf)
    table.insert(responce_data_chunks, param)
    return buf
end)
assert(request:perform())
responce_headers = table.concat(responce_headers_chunks)
responce_data = table.concat(responce_data_chunks)

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

local json = require('cjson')
print(responce_headers)
print(responce_data)
print(json.decode(responce_data)['form']['test'])