Nginx第二步

记录一些nginx常用的命令和要点。

[命令]set 设置变量

<Nginx 变量名的可见范围是整个配置, 但每个请求都有所有变量的独立副本>

<Nginx 变量值容器的生命期是与当前正在处理的请求绑定的>

server {
    listen 8080;
    location /test {
        set $foo hello;
        echo "foo: $foo";
    }
}

[命令]echo_exec 重定向

server {
    listen 8080;
    location /foo {
        set $a hello;
        echo_exec /bar;
} 
    location /bar {
        echo "a = [$a]";
    }
}
------------------------------------------
$ curl localhost:8080/foo
a = [hello]

[内置变量]uri request_uri 请求中的uri

location /test {
    echo "uri = $uri";
    echo "request_uri = $request_uri";
}
---------------------------------------
$ curl 'http://localhost:8080/test/hello%20world?a=3&b=4'
uri = /test/hello world
request_uri = /test/hello%20world?a=3&b=4

[内置变量]arg_xxx query中匹配到的value

<不区分大小写>

location /test {
    echo "name: $arg_name";
    echo "class: $arg_class";
}
---------------------------------------
$ curl 'http://localhost:8080/test?name=hello%20world&class=9'
name: hello%20world
class: 9

[命令]set_unescape_uri 为%XX解码

location /test {
    set_unescape_uri $name $arg_name;
    set_unescape_uri $class $arg_class;
    echo "name: $name";
    echo "class: $class";
}
--------------------------------------------
$ curl 'http://localhost:8080/test?name=hello%20world&class=9'
name: hello world
class: 9

[内置变量] args url?之后的部分,可改写

server {
    listen 8080;
    location /test {
        set $args "foo=1&bar=2";
        proxy_pass http://127.0.0.1:8081/args;
    }
} 

server {
    listen 8081;
    location /args {
        echo "args: $args";
    }
}
-------------------------------------------------
$ curl 'http://localhost:8080/test?blah=7'
args: foo=1&bar=2

[命令] map 定义映射规则

<这里有个疑问,就是在第一次输入实,set args为dubug后,foo的值仍为0.这是因为在set orig_foo时候,读取过一次foo的值,且将foo的值放到了缓存中,第二次读取仍会从缓存中拿数据。如果没有第一步的set orig_foo,那么最后输出foo的值应为1>

map $args $foo {
    default 0; #foo默认值为0
    debug 1; #args匹配到debug时,foo设置为1
} 
server {
    listen 8080;
    location /test {
        set $orig_foo $foo;
        set $args debug;
        echo "original foo: $orig_foo";
        echo "foo: $foo";
    }
}
-----------------------------------------------
$ curl 'http://localhost:8080/test'
original foo: 0
foo: 0
-----------------------------------------------
$ curl 'http://localhost:8080/test?debug'
original foo: 1
foo: 1

[命令] echo_location 级联请求(子请求)

[内置变量] request_method & echo_request_method

<请求会依次执行父请求和子请求的部分>

<在子请求中通通过request_method获取请求类型,获取到的是父请求的类型,此时需要通过echo_request_method获取子请求类型>

location /main {
    echo_location /sub;
    echo "main method: $request_method";
} 
location /sub {
    echo "sub method: $request_method";
}
-----------------------------------------------
$ curl --data hello 'http://localhost:8080/main'
sub method: POST
main method: POST
location /main {
    echo "main method: $echo_request_method";
    echo_location /sub;
} 
location /sub {
    echo "sub method: $echo_request_method";
}
-----------------------------------------------
$ curl --data hello 'http://localhost:8080/main'
main method: POST
sub method: GET

<nginx开启调试日志>

1、configure阶段添加选项 --with-debug 重新make && make install

2、在配置文件中通过error_log [log file path] debug;来写日志。

附:Nginx wiki : https://www.nginx.com/resources/wiki/modules/lua/#access-by-lua

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值