• openresty


    1.安装配置

    http://openresty.org/ 上这个网址下载并安装。

    2.nginx反向代理应用服务器

    问题2.1:什么是应用服务器?

    实际上就是让nginx拥有处理业务的能力。

    例如:mysql, redis 读写数据;发送socket请求;对http请求的筛选处理或者是业务层的用户验证等,这里不一一举例了。

    问题2.2:为什么要选择openresty?

    openresty是由nginx+lua结合的框架,据称可达到10k+的并发能力,这里我并没有实际验证过。但作为并发处理能力最优的nginx和简单高效的lua,再搭配上redis;因此不需要质疑他的处理能力,相信没有更好的组合了。

    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库文档)

  • 相关阅读:
    idea设置tomcat虚拟路径的两种方法
    改变javahome但是java version并没有修改的解决方案
    1. 两数之和
    88. 合并两个有序数组
    53. 最大子数组和(欠分治法)
    【记录】orcle安装
    191. 位1的个数
    【记录】plsql安装
    IP2region离线解析IP地址
    flink 常用命令
  • 原文地址:https://www.cnblogs.com/onizuk/p/3296363.html
Copyright © 2020-2023  润新知