1024programmer Nginx openresty-Nginx+lua+redis reverse proxy application server

openresty-Nginx+lua+redis reverse proxy application server

1. Installation configuration

Download and install from this URL at http://openresty.org/.

2. nginxReverse proxy application server

Question 2.1: What is an application server?

In fact, it is to let nginx have the ability to handle business.

For example: mysql, redis
Reading and writing data; sending socket requests; screening processing of http requests or user authentication at the business layer, etc., are not listed here.

Question 2.2: Why choose openresty?

openresty is a framework combined with nginx+lua, which is said to have a concurrency capability of 10k+, but I have not actually verified it here. However, nginx with the best concurrent processing capabilities and simple and efficient lua are paired with redis; therefore, there is no need to question its processing capabilities. I believe there is no better combination.

3. Fixed URL reverse proxy

Example:

location /proxy2 {
default_type text/html;
set $flag ;
set_by_lua $flag ‘
local args = ngx.req.get_uri_args()
If args[“a”] == “” then
return
end
return
‘;
if ($flag ~ ) {
return ;
}
proxy_pass http://127.0.0.1/example.php;
proxy_redirect off;
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_max_temp_file_size ;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 4k;
proxy_buffers 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

4. Variable URL reverse proxy

Issue 4.1: capture method.http_post does not send proxy post parameters.

According to the description of the document

Issuing a POST subrequest, for example, can be done as follows
res = ngx.location.capture(
‘/foo/bar’,
{ method = ngx.HTTP_POST, body = ‘hello, world’ }
)
See HTTP method constants methods other than POST. The method option is ngx.HTTP_GET by default.

The request on this is just to send the content of the body, post
The args are not sent out, and it is very puzzling at this time whether this does not support the request parameter proxy of post.

When the body option is not specified, the POST and PUT subrequests will inherit the request bodies of the parent request (if any).

Removing the body is still invalid, and the access has been in the running state, and the print result is stuck at ngx.location.capture after the connection timeout; could it be that the content of the post has not been read and has been polling, after some debugging, the key A line of code appeared.

— Reads the client request body synchronously without blocking the Nginx event loop.
ngx.req.read_body()

It turns out that openresty does not automatically judge whether the body already exists or read the body content again when capturing, and blocks access until it times out. So reading the body content before capturing will solve all the previous doubts.

Example:

ngx.req.read_body()
res = ngx.location.capture(‘/proxy’, {method = ngx.HTTP_POST, args = args, vars = {url = url}})

Question 4.2: The variable URL reverse proxy is special. After verification, nginx does not support get parameter proxy.

It’s a pity that nginx doesn’t support the get parameter proxy of variable URLs. The reason is quite puzzling and impossible to find out. Finally, I decided to bypass this hurdle to meet my needs.

location /proxy {
set $url ”;
set_by_lua $args “
local request_uri = ngx.var.request_uri
local poi =
  for i = , #request_uri, do
If string.sub(request_uri, i, i) == ‘?’ then
Poi = i
break
end
end
Return string.sub(request_uri, poi)
“;
proxy_pass http://$url$args;
proxy_redirect off;
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_max_temp_file_size ;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 4k;
proxy_buffers 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

Related documents:

https://github.com/chaoslawful/lua-nginx-module
(nginx lua library documentation)

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/openresty-nginxluaredis-reverse-proxy-application-server/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索