Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate lua and operate mysql with nginx

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how nginx integrates lua operation mysql", the content is detailed, the steps are clear, and the details are handled properly. I hope this "nginx how to integrate lua operation mysql" article can help you solve your doubts.

Realization idea

Make the configuration blacklist directly in nginx, which can be realized by writing logic blocks.

Write a filter in the server (Java) and intercept it uniformly in the filter

Write an interceptor in the server (Java) and intercept it uniformly in the interceptor

Here are three implementation ideas, as for the implementation, there may be more, but let's think about it. Writing logic blocks in nginx does not seem to be something that many people are good at; it is not impossible to do it at the code level, but in this way, in the peak period of business involving high concurrency, this is bound to cause great pressure on the back-end services, so is there any other better way to deal with it?

This is to say that lua, that is, nginx as a gateway is still used as a proxy server. Because nginx can integrate lua, lua is used to cooperate to complete the design of the above business implementation.

Ngx_lua module concept

The ngx_lua module was developed by the Taobao technical team by integrating the lua interpreter into Nginx

Lua script can be used to implement business logic. Because of the compactness, speed and built-in collaboration of lua, the cost of business logic implementation is greatly reduced while ensuring high concurrent service capability.

OpenRestry

OpenResty is a high-performance Web platform based on Nginx and Lua, which integrates a large number of excellent Lua libraries, third-party modules and most dependencies, and is used to easily build dynamic Web applications, Web services and dynamic gateways that can handle ultra-high concurrency and high scalability.

Nginx and Lua have been integrated inside OpenResty, so it will be more convenient to use.

To put it simply, if you install and use OpenRestry directly, you can achieve the effect of using both Nginx and Lua. At the same time, based on OpenRestry, you can also operate other middleware, such as mysql,redis,kafka, internally, which makes the business architecture more flexible in design.

Step 1 of OpenRestry installation, download OpenRestrywget https://openresty.org/download/openresty-1.15.8.2.tar.gz2, extract the file tar-zxf openresty-1.15.8.2.tar.gz3, and enter the OpenResty directory to perform configuration

This step is somewhat similar to the source code installation of nginx, and the relevant environment variables are configured. Just use the default one here.

. / configure

4. Execute the command: make & & make install

5. Enter the directory of OpenResty to configure nginx

When you enter the nginx directory, you can see that the directory inside is almost the same as the configuration after the installation of nginx itself.

Enter conf, locate the nginx.conf configuration file, and add the following:

Location / lua {default_type 'text/html'; content_by_lua' ngx.say ("hello,openRestry lua")';}

6. Start nginx and test

Go to the sbin directory of nginx to start nginx

After the startup is completed, the browser can access the server, and you can see that the nginx service itself has been started.

Then visit the lua address configured above, and you can see that it can also be accessed normally, indicating that the module of openrestry has been installed.

Ngx_lua common instructions

The basic building blocks for writing Nginx scripts using Lua are instructions that specify when to run user Lua code and how to use the results. Here are some simple instructions for some commonly used instructions

1 、 init_by_lua*

This instruction is executed each time the Nginx reloads the configuration to complete some time-consuming operation module loading or initialize some global configuration

2 、 init_worker_by_lua*

This instruction is used to start some scheduled tasks, such as heartbeat check, timing pull server configuration, etc.

3 、 set_by_lua*

As long as this instruction is used to assign a value to a variable, it can only return one value at a time and assign the result value to the specified variable in Nginx.

4 、 rewrite_by_lua*

Used to perform internal URL rewriting or external redirection, such as pseudo-static URL rewriting, which is performed by default at the end of the rewrite processing phase (similar to nginx's own rewrite function)

5 、 access_by_lua*

This directive is used for access control, for example, only intranet IP access is allowed

6 、 content_by_lua*

This instruction is the most frequently used instruction, most of the tasks are completed at this stage, other processes often prepare data for this stage, and formal processing is often carried out at this stage.

7 、 header_filter_by_lua*

Used to set the header information of the reply message

8 、 body_filter_by_lua*

This instruction filters the response data, such as truncation and replacement

9 、 log_by_lua*

This directive is used in the log request processing phase and uses Lua code to process the log, but does not replace the original log processing

10 、 balancer_by_lua*

The main function of this instruction is to implement the load balancer algorithm of the upstream server.

11 、 ssl_certificate_by_*

This directive will allow the Lua code of this configuration item when Nginx and downstream services start a SSL handshake operation

A need to use instructions

Next, let's make a simple requirement for the various instructions mentioned above.

After nginx receives the request, according to the value passed by gender in the parameter, "sir" is displayed on the page if gender passes in 1, and "madam" is displayed on the page if gender passes in 0.

Code implementation

Note: the basic step of using instructions is to write lua code in a custom localtion block in the nginx.conf module.

Location / getByGender {default_type 'text/html' Set_by_lua $param "local uri_args = ngx.req.get_uri_args () local gender= uri_args ['gender'] local name = uri_args [' name'] if gender==' 1 'Mr. then return name..':' elseif gender==' 0' Ms. then return name..': 'else return name end " Charset utf-8; return 200$ param;}

Then start nginx to do a test.

1) access the service without any parameters

There is no return message at this time.

2) access the service with name parameters

3) access the service with name and gender parameters

More instructions can be written in this way, but you need to master a little basic syntax of lua.

Lua operation redis

Redis is often used as data cache and in-memory database in the system, and plays a very important role in all kinds of Internet projects.

Lua-resty-redis library is an interface library provided by OpenResty to operate Redis. It can do some logic processing according to its own business situation, which is suitable for complex business logic. So the following will be explained by Lua-resty-redis.

Lua-resty-redis environment preparation

1. Install redis in advance and start the service

2. Test the redis client

Lua-resty-redis provides detailed API for accessing Redis, including creation of docking, connection, operation, data processing, and so on. These API basically correspond to the operation of Redis.

API commonly used in lua-resty-redis

1. Import redis dependencies into lua

Redis = require "resty.redis"

2. New, create a Redis object

Redis,err = redis:new ()

3. Create a redis connection

Ok: return 1 for successful connection and nil for failed connection

Err: returns the corresponding error message

Ok,err=redis:connect (host,port [, options_table])

4. Set the timeout for the request operation Redis.

Redis:set_timeout (time)

5. Close, close the connection

Close the current connection and return 1 successfully

Nil and error message are returned for failure.

Ok,err = redis:close ()

Supplementary note:

In lua-resty-redis, all Redis commands have their own methods; the method name is the same as the command name, but all are lowercase

Concrete realization effect display

Under the nginx.conf module, add the following location content

Location / redis {default_type "text/html" Content_by_lua_block {local redis = require "resty.redis"-- introduce Redis local redisObj = redis:new ()-- create Redis object redisObj:set_timeout (3000)-- set timeout data to 3s local ok,err = redisObj:connect ("IP") 6379)-- set redis connection information if not ok then-- determine whether the connection is successful ngx.say ("failed to connection redis", err) return end ok,err = redisObj:set ("username") "TOM")-- storing data if not ok then-- determining whether to store a successful ngx.say ("failed to set username", err) return end local res Err = redisObj:get ("username")-- get data from redis ngx.say (res)-- write data to redisObj:close ()} in the message body

Restart nginx and test it. Visit the following address directly in the browser, and you can see that the data is successfully written to redis.

Ngx_lua operation Mysql

MySQL is a widely used relational database. In ngx_lua, MySQL has two access modes, namely

Use ngx_lua module and lua-resty-mysql module: these two modules are installed by default when you install OpenResty

Use the drizzle_nginx_module (HttpDrizzleModule) module: separate is required

Installation, this library is not currently in OpenResty

Lua-resty-mysql

Lua-resty-mysql is a module developed by OpenResty, which is flexible, powerful, suitable for complex business scenarios, and supports stored procedure access.

Database query based on lua-resty-mysql

1. Prepare the mysql service

2. Create a table in advance

CREATE TABLE `users` (`id` int (10) NOT NULL AUTO_INCREMENT, `username` varchar (255) DEFAULT NULL, `day` date DEFAULT NULL, `salary` double (10Magne2) DEFAULT NULL, PRIMARY KEY (`id`) ENGINE=InnoDB DEFAULT CHARSET=utf8

And prepare several pieces of data in advance.

INSERT INTO `mydb`.`users` (`id`, `username`, `roomday`, `salary`) VALUES ('1levels,' xiaowang', '1991-03-15levels,' 9000.00'); INSERT INTO `mydb`.`users` (`id`, `username`, `dayday`, `salary`) VALUES ('2levels,' xiaoma', '1992-07-15levels,' 8000.00'); lua-resty-mysql API description

1. Introduce "resty.mysql" module

Local mysql = require "resty.mysql"

2. Create a MySQL connection object

When an error is encountered, db describes the information for nil,err and error description

Db,err = mysql:new ()

3. Create a connection object

Ok,err=db:connect (options)

Options is a parameter Lua table structure that contains information about database connections

Host: server hostname or IP address

Port: server listening port. Default is 3306.

User: user name of the login

Password: login password

Database: the name of the database used

4. Set the timeout for sub-requests (ms)

Including connect method

Db:set_timeout (time)

5. Close the current MySQL connection and return to status

If successful, 1 is returned; if any error occurs, nil and error description are returned

Db:close ()

6. Asynchronously send a query to the remote MySQL

Returns the number of bytes successfully sent if successful, or nil and error description if error occurs.

Bytes,err=db:send_query (sql)

7. Read a row of data from the result returned by the MySQL server

Res returns a Lua table that describes the OK package or result set package

Rows specifies the maximum value of the returned result set, which defaults to 4

In the case of a query, an array containing multiple rows is returned. Each row is a key-value pair of data columns

Res, err, errcode, sqlstate = db:read_result () res, err, errcode, sqlstate = db:read_result (rows)

The result is similar to the following

{id=1,username= "TOM", birthday= "1988-11-11", salary=10000.0}, {id=2,username= "JERRY", birthday= "1989-11-11", salary=20000.0}}

If it is an addition, deletion or modification, data similar to the following will be returned

{insert_id = 0, server_status=2, warning_count=1, affected_rows=2, message=nil}

Return value description:

Res: result set of the operation

Err: error messa

Error code of errcode:MySQL, such as 1064

Sqlstate: returns a standard SQL error code of 5 characters, such as 42000

Specific operation case

Add the following to the server block, and then restart nginx

Location / mysql {content_by_lua_block {default_type "text/html" Local mysql = require "resty.mysql" local db = mysql:new () local ok,err = db:connect {host= "127.0.0.1", port=3306, user= "root", password= "123456" Database= "mydb"} db:set_timeout (3000) db:send_query ("select * from users where id = 1") local res,err,errcode,sqlstate = db:read_result () ngx.say (res [1] .id.. "," .res [1] .username.. "," .res [1]. Birthday.. ",".. res [1] .minutes) db:close ()}}

You can see that the data with ID 1 in the database is successfully queried by accessing the path of mysql.

Use cjson to format query results

Judging from the returned results above, this form of returned data is actually not very friendly when parsing, so you can use lua-cjson to process the query results.

Use steps

Step 1: introduce cjson

Local cjson = require "cjson"

Step 2: call the encode method of cjson for type conversion

Cjson.encode (res)

Let's make a simple modification to the above program module.

Location / mysql-cjson {default_type "text/html" Content_by_lua_block {local cjson = require "cjson" local mysql = require "resty.mysql" local db = mysql:new () local ok,err = db:connect {host= "127.0.0.1", port=3306, user= "root", password= "123456" Database= "mydb"} db:set_timeout (3000) db:send_query ("select * from users") local res,err,errcode,sqlstate = db:read_result () ngx.say (cjson.encode (res)) for iMagne v in ipairs (res) do ngx.say (v.id.. "," .username.. " "v.daylight", ".. V.salary) end db:close ()}}

Then it was tested again, and the data was presented in json format.

Add, delete and modify operation

Location / mysql-cjson {default_type "text/html" Content_by_lua_block {local cjson = require "cjson" local mysql = require "resty.mysql" local db = mysql:new () local ok,err = db:connect {host= "127.0.0.1", port=3306, user= "root", password= "123456" Database= "mydb"} db:set_timeout (3000)-- query operation db:send_query ("select * from users where id=1")-- insert data-- local res,err,errcode,sqlstate = db:query ("insert into users (id,username,birthday,salary) values '1995-10-17)-- modify data-- local res,err,errcode,sqlstate = db:query ("update users set username='lisi' where id = 1")-- Delete data-- local res,err,errcode Sqlstate = db:query ("delete from users where id = 2") db:close ()}} so far This article "nginx how to integrate lua operation mysql" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report