Setting up the server

In the following section, we will try to build an example using NGINX-Lua support.

The problem statement

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.

Statement

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:

  • Determine whether the request URI is one of the redirected locations
  • If no, then serve the request
  • If yes, then determine the new location and HTTP redirect code
  • Perform an NGINX redirect to the new location using the redirect code

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

/previous-feature/1

/new-feature-1

301

2

/previous-feature/2

/new-feature-2

302

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:

  • The lua_package_path directive adds the lua-resty-Mysql scripts to the NGINX Lua path
  • The init_by_lua directive imports the resty.mysql package
  • NGINX does all validations in the default location block
  • The content_by_lua directive tries to build a response using Lua
  • The db.connect statement connects to the MySQL instance
  • If an error occurs while connecting to MySQL, then the content is served from the @inline_concat location
  • If no error occurs, then check the location int in the seo_redirect_location table using db:query() method
  • The next() method call gives back any result found by the query
  • If one exists, perform a redirect using the ngx.redirect() method
  • If one does not exist, the server serves content from the @inline_concat location
  • If the NGINX configuration does not have a default content type, it needs to be set in the @inline_concat location block
  • In the @inline_concat location, set_by_lua executes the Lua code to generate a response
  • NGINX sends back the generated response using the return statement. The optional HTTP code (200) is also passed to the call
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.37.126