In the following section, we will try to build an example using NGINX-Lua support.
Let's say we have a website, the pages of which have moved from location /previous-feature/1 , ... , /previous-feature/n
to /new-feature-1 , ... , /new-feature-n
. Now, if the site is being indexed by Google (or any other search engine), it might be required to perform URL redirects when the Googlebot hits /previous-location/X
. The redirect may require a proper HTTP redirect code (301
, 302
) too.
In order to solve the previously stated problem, we need to do some validation before processing a request. We will configure NGINX to do the following:
We will save the location information in MySQL in the following format; then, we can do lookups from NGINX to determine whether the location requires a redirect or not:
ID |
Org_Location |
Redirect_Loc |
Redirect_Code |
---|---|---|---|
1 |
|
|
|
2 |
|
|
|
The following MySQL statements will create the required table and insert data into it:
CREATE TABLE seo_redirect_location ( id int(11) NOT NULL AUTO_INCREMENT, Org_Location varchar(45) DEFAULT NULL, Redirect_Location varchar(45) DEFAULT NULL, Redirect_Code int(11) NOT NULL, PRIMARY KEY (id) ); INSERT INTO seo_redirect_location (Org_Location, Redirect_Location, Redirect_Code) VALUES ('/previous-feature/1','/new-feature-1',301); INSERT INTO seo_redirect_location (Org_Location,Redirect_Location, Redirect_Code) VALUES ('/previous-feature/2','/new-feature-2',302);
Now, we need to build the NGINX script that can look up the MySQL database. We will add the lua-resty-Mysql
library to do this. The following NGINX script sums up all the required changes:
# add library to path lua_package_path "/home/ubuntu/lua-resty-mysql-master/lib/?.lua;;"; # import mysql package init_by_lua 'mysql = require "resty.mysql" '; server { location / { default_type 'text/plain'; content_by_lua ' local db, err = mysql:new() db:set_timeout(1000) #connect to databas local ok, err, errno, sqlstate = db:connect{ host = "192.168.2.111", port = 3306, database = "test", user = "newuser", password = "newuser", max_packet_size = 1024 * 1024 } if not ok then ngx.exec("@inline_concat") end local res, err, errno, sqlstate = db:query("select * from seo_redirect_location where org_Location='"..ngx.var.uri.."' order by id asc", 10) if next(res) then return ngx.redirect(res[1].Redirect_location,res[1].Redirect_Code) end ngx.exec("@inline_concat") '; } location @inline_concat { # MIME type determined by default_type: default_type 'text/plain'; set $a "hello"; set $b "world"; # Lua script to generate Hello world set_by_lua $res "return ngx.arg[1]..ngx.arg[2]..ngx.var.uri" $a $b; return 200 $res; } }
The preceding script does the following things:
lua_package_path
directive adds the lua-resty-Mysql
scripts to the NGINX Lua pathinit_by_lua
directive imports the resty.mysql
packagecontent_by_lua
directive tries to build a response using Luadb.connect
statement connects to the MySQL instance@inline_concat
locationint
in the seo_redirect_location
table using db:query()
methodnext()
method call gives back any result found by the queryngx.redirect()
method@inline_concat
location@inline_concat
location block@inline_concat
location, set_by_lua
executes the Lua code to generate a responsereturn
statement. The optional HTTP code (200
) is also passed to the call3.145.37.126