Configure Lua to forward Nginx request Replication

Source: Internet
Author: User

Configure Lua to forward Nginx request Replication

Configure Nginx to copy requests and forward them to other applications. The following are the steps you have set up and your understanding to facilitate future use.

1. Establish the environment

The actual build environment is as follows: Linux CenterOS 6.5, Nginx1.9.0, headers-more-nginx-module-0.31, LuaJIT-2.1.0-beta2, lua-nginx-module-0.10.2.

The above is the corresponding build version. if the version does not match, nginx compilation may fail. The plug-in downloaded by github should be renamed as much as possible for ease of use.

Follow the reference link to compile Nginx.

2. Nginx + Lua file configuration

A. Compile a copy request lua script copy_req.lua.

Local res1, res2, action
Action = ngx. var. request_method
If action = "POST" then
Arry = {method = ngx. HTTP_POST, body = ngx. req. read_body ()}
Else
Arry = {method = ngx. HTTP_GET}
End

If ngx. var. svr = "on" then
Res1, res2 = ngx. location. capture_multi {
{"/Product"... ngx. var. request_uri, arry },
{"/Test"... ngx. var. request_uri, arry },
}
Else
Res1, res2 = ngx. location. capture_multi {
{"/Product"... ngx. var. request_uri, arry },
}
End

If res1.status = ngx. HTTP_ OK then
Local header_list = {"Content-Length", "Content-Type", "Content-Encoding", "Accept-Ranges "}
For _, I in ipairs (header_list) do
If res1.header [I] then
Ngx. header [I] = res1.header [I]
End
End
Ngx. say (res1.body) # return results in the production environment only
Else
Ngx. status = ngx. HTTP_NOT_FOUND
End

Here, the file address reference can be written as an address. The relative address is relative to the nginx directory.
B. Configure the corresponding Nginx configuration file. The address in this article is conf/vhost/fenliu. conf. Add include vhost/*. conf at the bottom of nginx. conf;

The fenliu. conf file is configured as follows:

Upstream product {
Server 127.0.0.1: 80;
}
Upstream test {
Server 192.168.1.1: 88;
}
Server {
Listen 8000;
# Lua_code_cache off;

Location ~ * ^/Product {
Log_subrequest on;
Rewrite ^/product (. *) $1 break;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_pass http: // product;
Access_log log/product-upstream.log;
}

Location ~ * ^/Test {
Log_subrequest on;
Rewrite ^/test (. *) $1 break;
Proxy_set_header Host $ host;
Proxy_set_header X-Real-IP $ remote_addr;
Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
Proxy_pass http: // test;
Access_log log/test-upstream.log;
}

Location ~ * ^/(. *) $ {
Client_body_buffer_size 2 m;
Set $ svr "on"; # enable or disable the copy Function
Content_by_lua_file conf/vhost/copy_req.lua;
}
}

This file is very important. The remarks here are my own understanding. ^/product, ^/test is mainly used to forward the URLs accessed by these two paths, and one is forwarded to production, one rewrite is added to the test to rewrite the request address,
^/(. *) $ Is the focus. It is to copy and forward all non-product and test requests.

The above configuration is used as an example. The actual process is as follows:
1. Request address: http: // ip: 8000/hello/req. do
2. If nginx does not match the product and test, the last one will be taken. The Lua configuration will change to two requests:/product/hello/req. do and/test/hello/req. do.
3. The product and test of nginx are intercepted and forwarded to the production and test environments. The address is incorrect at this time. Therefore, you can use rewrite to rewrite the url,
Rewrite ^/product (. *) $1 break; match/product/hello/req. do changes to/product (/hello/req. do), $1 indicates/hello/req. do, the address after rewriting will become the address we want, and the address after forwarding will become http: // product/hello/req. do.

This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151577.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.