openresty - nginx+lua+redis反向代理應用服務器

1.安裝配置php

http://openresty.org/ 上這個網址下載並安裝。html

 

2.nginx反向代理應用服務器mysql

問題2.1:什麼是應用服務器?nginx

實際上就是讓nginx擁有處理業務的能力。git

例如:mysql, redis 讀寫數據;發送socket請求;對http請求的篩選處理或者是業務層的用戶驗證等,這裏不一一舉例了。github

問題2.2:爲何要選擇openresty?redis

openresty是由nginx+lua結合的框架,據稱可達到10k+的併發能力,這裏我並無實際驗證過。但做爲併發處理能力最優的nginx和簡單高效的lua,再搭配上redis;所以不須要質疑他的處理能力,相信沒有更好的組合了。sql

 

3.固定URL反向代理服務器

Example:併發

 1 location /proxy2 {
 2     default_type text/html;
 3 
 4     set $flag 0;
 5 
 6     set_by_lua $flag '
 7         local args = ngx.req.get_uri_args()
 8         if args["a"] == "1" then
 9             return 1
10         end
11         return 0
12     ';
13 
14     if ($flag ~ 0) {
15         return 0;
16     }
17     
18     proxy_pass http://127.0.0.1/example.php;
19     
20     proxy_redirect off;
21 
22     proxy_set_header   Host             $host; 
23     proxy_set_header   X-Real-IP        $remote_addr;
24     proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
25     proxy_max_temp_file_size 0;
26     proxy_connect_timeout      90;
27     proxy_send_timeout         90;
28     proxy_read_timeout         90;
29     proxy_buffer_size          4k;
30     proxy_buffers              4 32k;
31     proxy_busy_buffers_size    64k;
32     proxy_temp_file_write_size 64k;
33 }

 

4.變量URL反向代理 

問題4.1:capture method.http_post 不發送代理post參數.

根據文檔的描述

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.

此上的請求僅僅是發送了body的內容, post args並無發送出去,此時十分費解難道這個還不支持post的請求參數代理。

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

去掉body也依舊無效,而且訪問一直處於running的狀態,而且的鏈接超時後打印結果卡在ngx.location.capture;難道是沒有讀取到post的內容一直輪詢,通過一番調試後,關鍵的一行代碼出現了。

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

原來openresty並不會在capture的時候自動判斷body是否已經存在或再次讀取body內容,並且一直阻塞訪問,直到超時。因此在capture前先讀取body內容就解決了前面的全部疑惑。

Example:

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

 

問題4.2:變量URL 反向代理比較特殊,通過驗證nginx是不支持get參數代理.

很遺憾nginx不支持變量URL的get參數代理,緣由至關費解也無從查明,最後決定繞過這個坎實現個人需求。

 1 location /proxy {
 2     set $url '';
 3     set_by_lua $args "
 4         local request_uri = ngx.var.request_uri
 5         local poi = 0
 6         for i = 1, #request_uri, 1 do
 7             if string.sub(request_uri, i, i) == '?' then
 8                 poi = i
 9                 break
10             end
11         end
12         return string.sub(request_uri, poi)
13     ";
14 
15     proxy_pass http://$url$args;
16 
17     proxy_redirect off;
18     proxy_set_header   Host             $host; 
19     proxy_set_header   X-Real-IP        $remote_addr;
20     proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
21     proxy_max_temp_file_size 0;
22     proxy_connect_timeout      90;
23     proxy_send_timeout         90;
24     proxy_read_timeout         90;
25     proxy_buffer_size          4k;
26     proxy_buffers              4 32k;
27     proxy_busy_buffers_size    64k;
28     proxy_temp_file_write_size 64k;
29 }

 

相關文檔:

https://github.com/chaoslawful/lua-nginx-module (nginx的lua庫文檔)

相關文章
相關標籤/搜索