nginx 配置文件解析--二

nginx服务器中操作: (默认安装了春哥的ngx_echo 模块:   ./configure --add-module=path_echo-nginx-module)

nginx API 默认以的参数默认以&作为分隔符。eg: http://localhost/Npath?cmd=aa&day=2010-12-12 .

1、添加新的API路径 Npath 

server {

location = ‘/Npath' {  echo "hello" ; }

2、在 API 中附带参数。set_unescape_uri 使 API 支持UTF 编码。eg:  http:// localhost / Npath ? para_1=person 。 

server {

location = ‘/Npath' {  

if( $para_1=' ' ) 

echo "hello, anonymous" ;  

break;

set_unescape_uri $para  $arg_para_1    # API 传入的参数会自动加入arg_  前缀。 使用set_*_uri 则引入多UTF-8 编码的支持。

echo "hello,  $para_1" ;    #  输出 hello person

# 如果安装有 set-misc-nginx-module  ngx-devel-kit 模块,则可以set $para  $para_1 ; set_if_empty  $para "anonymous" 来取代上面的 if 语句。

}

3、在 API 中使用特殊规则。

a 通配符与数组。eg: curl  http://localhost/AA, BB  输出 [AA]--[BB]

location  ~  '^/foo/(.*)‘ {   # 使用正则规则表示路径

set  $list  $1;  array_split ',‘   $list;  array_map '[$array_it ]‘  $list ;  array_join '--'   $list ;  

}

echo_foreach_split  ...    echo_end  是 array_split 的迭代版版。

b 遍历子路径 。 eg: curl  http://localhost/Path_Paraent   回遍历对应子 child1 

location = 'Path_Paraent' {   echo_location_async  /child1  ;  }   # echo_location_async 支持迭代。

location /child1  {  action ...  }   # 注意,子路径中无 = 。

4、在 API 中使用 mysql DB 查询。需要模块  ngx_drizzle  ;  ngx_rds_json .


upstream  mysql_test  { 

drizzle_server  ip:port      dbname=test      password=***              user = aa         protocol=mysql  

#  drizzle_keepalive   max=200   overflow=reject   # 建立mysql查询API池,设置最大DB查询的TCP 连接数

}

location = /cats { drizzle_query  'select * from tab_t' ;  drizzle_pass  my_mysql_backend  ;  rds_json  on;   }  # 提供查询的API 

# 使用API参数构建 sql 查询语句。以 http://localhost/cats/Jerrry, Tom   为例

location ~ '^/cats/(.*)' {   set  $list   $1; array_list  ','  $list ; array_map_op  set_quote_sql_str $list;   # 加入引号

array_map  ' name=$array_it '    $list;  array_join   ' or  '  $list   to=$cond ;  # 使用 OR 连接两个条件

drizzle_query  "select   *  from   cats   where   $cond " ;   #  select * from cats  where  name='Jerry'  or name=’Tom'

drizzle_pass   my_mysql_backend;   rds_json  on;  }


ngx_devel_kit  set-misc-nginx-module set_unescape_uri

array-var-nginx-module  memc-nginx-module

ngx_http_upstream_keepalive 支持memchache

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值