当前位置:网站首页>Distributed -- openresty+lua+redis
Distributed -- openresty+lua+redis
2022-06-30 14:48:00 【aruba】
We have used it before nginx, It has a wealth of modules for us to use , because nginx By c Written in language , So before writing modules, you must use c/c++, later , Someone will lua The interpreter inherits nginx in , built-in ngx_lua modular , thus ,nginx Support lua
One 、OpenResty
OpenResty Is based on nginx An extension of the open source version , Integrated a lot of lua library
1. add to repo
cd /etc/yum.repos.d/
wget https://openresty.org/package/centos/openresty.repo2. install openresty
yum install openresty3. start-up openresty
openresty The default installation path is /usr/local/openresty/, One of the nginx Catalog , About openresty The startup and configuration of are the same as before nginx identical
cd /usr/local/openresty/nginx/conf modify openresty Of http modular To configure , newly added server modular , Use content_by_lua To perform a lua Code :
server{
listen 8080;
location /{
default_type text/html;
content_by_lua 'ngx.say("hello openresty")';
}
}start-up openresty:
cd /usr/local/openresty/nginx/sbin/
./nginx -p /usr/local/openresty/nginx/ Browser access 8080 port :
Two 、http visit Redis
It's done nginx perform lua sentence , Next let's look at nginx How to access the Redis Redis Environment construction can be seen in the previous article : Distributed --Redis Installation and use of data types
1. obtain Redis data
Here is the passage nginx obtain Redis in key Corresponding value
1.1 start-up Redis
Start a default 6379 Port of Redis that will do , Here's what I built before Redis colony :
1.2 modify nginx To configure
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
default_type text/plain;
# Set get redis in key by m Value
set $redis_key "m";
redis_pass 127.0.0.1:6379;
# In case of 404, Just leave it to @fetch Handle
error_page 404 = @fetch;
}
location @fetch {
root html;
}
}
}nginx Reload configuration :
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http.conf -s reload1.3 adopt http visit
curl http://localhost/1.htmlresult :
redis Client side Settings m The key/value pair :
Revisit :
2. Set up Redis data
The way above 1, Just got Redis The data of , So how do you set that Redis The key value is right ? If nginx Support redis Just follow your instructions , actually nginx Is to support the
2.1 modify nginx To configure
have access to redis2_query Keep up redis Instructions , To set the value , It can also be used redis Instruction get value :
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location /get {
# Set a variable
set_unescape_uri $key "n";
redis2_query get $key;
redis_pass 127.0.0.1:6379;
# In case of 404, Just leave it to @fetch Handle
error_page 404 = @fetch;
}
location /set {
# Set a variable
set_unescape_uri $key "n";
redis2_query set $key "hello2";
redis2_pass 127.0.0.1:6379;
}
location @fetch {
root html;
}
}
}nginx restart :
./nginx -s stop
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-http2.conf2.2 Access test
curl http://localhost/get
curl http://localhost/setresult :
3、 ... and 、lua combination Nginx、Redis
The official introduction :https://github.com/openresty/lua-resty-redis First ,OpenResty Integrate lua after , There are three kinds of calls lua The way :
Way grammar | describe |
|---|---|
content_by_lua | At first we have used , It supports the execution of a lua Code |
content_by_lua_file | This statement supports the execution of a lua Script files , It's also the most used |
content_by_lua_block | This statement supports the execution of a lua Code block , Use... For some simple scenarios |
Let's use lua And Redis Interact
1. lua operation Redis data
lua operation Redis, The step is to introduce Redis modular , Connect Redis, And then operate
1.1 To write lua Script
Create a directory to store lua Script :
mkdir lua
vi control_redis.luaThe content is :
-- introduce redis modular
local redis = require("resty.redis")
-- Create a redis object
local red = redis:new()
-- 1. Connect redis
-- Multi parameter return
local ok,err = red:connect("127.0.0.1",6379)
if not ok then
ngx.say("connect failed:",err)
return
end
-- 2. Set up redis The key/value pair
ok,err = red:set("luaKey","luaValue")
if not ok then
ngx.say("set faild:",err)
return
end
-- 3. Read redis The key/value pair
ret = red:get("luaKey")
ngx.say("read luaKey value:",ret)
return1.2 modify nginx To configure
Use content_by_lua_file Appoint lua The absolute path of the script :
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8090;
server_name localhost;
location / {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/lua/control_redis.lua;
}
}
}restart nginx:
./nginx -s stop
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf1.3 test
2. lua obtain nginx in http Of get Request parameters
2.1 To write lua Script
vi http_get.lua Use ngx.req.get_uri_args() obtain , The content is :
-- Back to a table type
local args = ngx.req.get_uri_args()
for k,v in pairs(args) do
ngx.say("key:"..k.."value:"..v)
end2.2 modify nginx To configure
New port listening :
server {
listen 8091;
server_name localhost;
location /get {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/lua/http_get.lua;
}
}Reload the configuration file :
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload2.3 test
http://192.168.42.4:8091/get?name= Zhang San &age=193. lua obtain nginx in http Of post Request parameters
post There are two kinds of requests :body Key value pairs and body Request body . The latter corresponds to the popular json Format
3.1 To write lua Script
post To obtain the request parameters, you need to call ngx.req.read_body() Method
Key value pair :
vi http_post_kv.lua Use ngx.req.get_post_args() obtain , The content is :
-- Read first
ngx.req.read_body()
-- Get more
local params = ngx.req.get_post_args()
for k,v in pairs(params) do
ngx.say("key:"..k.." value:"..v)
endRequest body :
vi http_post_body.lua Use ngx.req.get_body_data() obtain , The content is :
-- Read first
ngx.req.read_body()
-- Then get the request body
local body = ngx.req.get_body_data();
ngx.say(body)3.2 modify nginx To configure
server {
listen 8092;
server_name localhost;
location /post_kv {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/lua/http_post_kv.lua;
}
location /post_body {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/lua/http_post_body.lua;
}
}Reload nginx To configure :
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload3.3 test
Key value pair :
Request body :
4. lua obtain nginx in http Request header for
4.1 To write lua Script
vi http_headers.lua Request header passed ngx.req.get_headers() obtain , The content is :
local headers = ngx.req.get_headers()
for k,v in pairs(headers) do
ngx.say("key:"..k.." value:"..v)
end4.2 modify nginx To configure
server {
listen 8093;
server_name localhost;
location /headers {
default_type text/html;
content_by_lua_file /usr/local/openresty/nginx/lua/http_headers.lua;
}
}Reload the configuration file :
./nginx -p /usr/local/openresty/nginx/ -c /usr/local/openresty/nginx/conf/nginx-lua.conf -s reload4.3 test
That's all openresty+lua+redis Basic use of
边栏推荐
- How does hbuilder display in columns?
- K high frequency elements before sorting
- CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
- [extensive reading of papers] analyzing connections between user attributes, images, and text
- In situ merging of two arrays with two pointers
- On simple code crawling Youdao translation_ 0's problem (to be solved)
- Laravel upload error
- One dimensional and two dimensional array addresses
- Effect of shadow around the block after mouse over
- Sum of squares of two pointers
猜你喜欢

Introduction to the construction and development of composer private warehouse

CCF window (Full Score code + problem solving idea) March 2, 2014

day02
![[geek challenge 2019] PHP problem solving record](/img/bf/038082e8ee1c91eaf6e35add39f760.jpg)
[geek challenge 2019] PHP problem solving record

KnightCTF WEB

Querywrapper in mybaits plus

【BUUCTF】 Have Fun

PS dynamic drawing

Shangpinhui knowledge points of large e-commerce projects

Detailed explanation of the first three passes of upload Labs
随机推荐
The first three passes of sqli Labs
JS to realize simple lottery function
Detailed explanation of the first three passes of upload Labs
數據恢複軟件EasyRecovery15下載
Notepad regular delete the line of the keyword
After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
PHP common authentication / third-party methods
PS cutting height 1px, Y-axis tiling background image problem
The JSON data returned from the control layer to JS has a "\" translator. How to remove it
The first dark spring cup dnuictf
机械工程师面试的几个问题,你能答上来几个?
KnightCTF WEB
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
The kth largest element in the sorted array
ES6 notes
NoViableAltException([email protected][])
ThinkPHP v3.2 comment annotation injection write shell
CCF window (Full Score code + problem solving idea) March 2, 2014
[buuctf] [geek challenge 2019] secret file