g87616758 发表于 2018-11-11 08:38:57

nginx+set-misc-nginx-module模块说明

  set-misc-nginx-module模块是标准的HttpRewriteModule指令的扩展,提供更多的功能,如URI转义与非转义、JSON引述、Hexadecimal/MD5/SHA1/Base32/Base64编码与解码、随机数等等。在后面的应用中,都将会接触使用到这个模块的。该模块是由章亦春先生开发的,他开发的其他模块应用也会使用到这个模块的。充分使用nginx非阻塞模式,对性能上有极大的提高,我个人认为很有必要去弄懂弄透nginx,通过自己去动手动脑用实例来加以验证,不断的对模块参数命令加以理解,在实际环境中应用自如。某朋友说“招运维人员,不懂nginx的一律不要。”呵呵,有点道理的。
  1. set-misc-nginx-module模块指令说明:
  set_if_empty
  语法:set_if_empty $dst ;
  默认值:no
  配置段:location, location if
  如果参数$dst是空的,则赋值为。
  set $a 32;
  set_if_empty $a 56;
  $a的值为32.
  set $a ”;
  set $value “hello, world”
  set_if_empty $a $value;
  $a的值为”hello, world”。
  set_quote_sql_str
  语法:set_quote_sql_str $dst/ set_quote_sql_str $dst
  默认值:no
  配置段:location, location if
  当两个参数时,该指令将引用第二个参数值。该指令通常用于防止SQL注入。mysql字符串值引用规则和分配第一个参数结果。
  location /test {
  set $value “hello\n\r’\”\\”;
  set_quote_sql_str $quoted $value;
  echo $quoted;
  }
  结果为’hello\n\r\’\”\\’。
  当是当个参数时,该指令将修改参数变量。如:
  location /test {
  set $value “hello\n\r’\”\\”;
  set_quote_sql_str $value;
  echo $value;
  }
  结果为’hello\n\r\’\”\\’。
  set_quote_pgsql_str
  语法:set_quote_pgsql_str $dst/ set_quote_pgsql_str $dst
  默认值:no
  配置段: location, location if
  与set_quote_sql_str相似,但是要符合PostgreSQL的SQL字符串常量的引用规则。
  set_quote_json_str
  语法:set_quote_json_str $dst/ set_quote_json_str $dst
  默认值:no
  配置段:location, location if
  当两个参数时,该指令将引用第二个参数。JSON字符串值引用规则和分配第一个参数结果。
  location /test {
  set $value “hello\n\r’\”\\”;
  set_quote_json_str $quoted $value;
  echo $quoted;
  }
  结果为:”hello\n\r’\”\\”。
  当当个参数时,该指令将修改参数变量。如:
  location /test {
  set $value “hello\n\r’\”\\”;
  set_quote_json_str $value;
  echo $value;
  }
  结果为:”hello\n\r’\”\\”。
  set_unescape_uri
  语法: set_unescape_uri $dst/ set_unescape_uri $dst
  默认值: no
  配置段:location, location if
  当两个参数时,该指令将非转义第二个参数的值作为URI一部分,并分配第一个参数变量$dst分配结果。如:
  location /test {
  set_unescape_uri $key $arg_key;
  echo $key;
  }
  当请求GET /test?key=hello+world%21时,得到:hello world!。
  注意: nginx标准的变量$arg_PARAMETER保存原始的URI参数(转义过的),因此需要set_unescape_uri指令来非转义先。
  当单个参数时,该指令将修改参数变量位置,如:
  location /test {
  set $key $arg_key;
  set_unescape_uri $key;
  echo $key;
  }
  当请求GET /test?key=hello+world%21时,得到:hello world!。
  set_escape_uri
  语法:set_escape_uri $dst/set_escape_uri $dst
  默认值:no
  配置段:location, location if
  与set_unescape_uri相似。
  set_hashed_upstream
  语法:set_hashed_upstream $dst
  默认值:no
  配置段:location, location if
  参数 hash后的值,对应于中的某个upstream名称。
  upstream moon { … }
  upstream sun { … }
  upstream earth { … }
  upstream_list universe moon sun earth;
  location /test {
  set_unescape_uri $key $arg_key;
  set $list_name universe;
  set_hashed_upstream $backend $list_name $key;
  echo $backend;
  }
  当请求/test?key=blah时,得到的值是 “moon”, “sun”, “earth”其中一个。取决于参数key。
  set_encode_base32
  语法:set_encode_base32 $dst/ set_encode_base32 $dst
  默认值:no
  配置段: location, location if
  当两个参数时,该指令将对第二个参数进行base32(hex)编码,并将结果赋值给第一个变量参数$dst。 如:
  location /test {
  set $raw “abcde”;
  set_encode_base32 $digest $raw;
  echo $digest;
  }
  当请求/test时,得到:c5h66p35。
  默认情况下,字符=用来左填充字节对齐。可以通过 set_misc_base32_padding off来禁止填充。
  当单个参数时,该指令将修改参数变量位置。如:
  location /test {
  set $value “abcde”;
  set_encode_base32 $value;
  echo $value;
  }
  当请求/test时,得到:c5h66p35。
  set_misc_base32_padding
  语法:set_misc_base32_padding on|off
  默认值:on
  配置段:http, server, server if, location, location if
  当set_encode_base32指令以base32进行编码时,该指令控制是否以字符=来填充。
  set_decode_base32
  语法:set_decode_base32 $dst| set_decode_base32 $dst
  默认值:no
  配置段: location, location if
  与set_encode_base32相似,只不过是反过程。
  set_encode_base64
  语法: set_encode_base64 $dst|set_encode_base64 $dst
  默认值:no
  配置段;location, location if
  当两个参数时,该指令将对第二个参数进行base64编码,并将结果赋值给第一个变量参数$dst。 如:
  location /test {
  set $raw “abcde”;
  set_encode_base64 $digest $raw;
  echo $digest;
  }
  当请求/test时,得到YWJjZGU=。
  当单个参数时,该指令将修改参数变量位置。如:
  location /test {
  set $value “abcde”;
  set_encode_base64 $value;
  echo $value;
  }
  当请求/test时,得到YWJjZGU=。
  set_decode_base64
  语法:set_decode_base64 $dst| set_decode_base64 $dst
  默认值:no
  配置段:location, location if
  与set_encode_base64相似,只不过是反过程。
  set_encode_hex
  语法:set_encode_hex $dst| set_encode_hex $dst
  默认值:no
  配置段: location, location if
  当两个参数时,该指令将对第二个参数进行hexadecimal编码,并将结果赋值给第一个变量参数$dst。 如:
  location /test {
  set $raw “章亦春”;
  set_encode_hex $digest $raw;
  echo $digest;
  }
  当请求/test时,得到:e7aba0e4baa6e698a5。
  当单个参数时,该指令将修改参数变量位置。如:
  location /test {
  set $value “章亦春”;
  set_encode_hex $value;
  echo $value;
  }
  当请求/test时,得到:e7aba0e4baa6e698a5。
  set_decode_hex
  语法:set_decode_hex $dst| set_decode_hex $dst
  默认值:no
  配置段: location, location if
  与set_encode_hex相似,只不过是反过程。
  set_sha1
  语法:set_sha1 $dst| set_sha1 $dst
  默认值:no
  配置段:location, location if
  当两个参数时,该指令将对第二个参数进行SHA-1编码,并将结果赋值给第一个变量参数$dst。 如:
  location /test {
  set $raw “hello”;
  set_sha1 $digest $raw;
  echo $digest;
  }
  当请求/test,得到:aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d。
  当单个参数时,该指令将修改参数变量位置。如:
  location /test {
  set $value “hello”;
  set_sha1 $value;
  echo $value;
  }
  set_md5
  语法:set_md5 $dst| set_md5 $dst
  默认值:no
  配置段:location, location if
  当两个参数时,该指令将对第二个参数进行MD5编码,并将结果赋值给第一个变量参数$dst。 如:
  location /test {
  set $raw “hello”;
  set_sha1 $digest $raw;
  echo $digest;
  }
  当请求/test,得到:5d41402abc4b2a76b9719d911017c592。
  当单个参数时,该指令将修改参数变量位置。如:
  location /test {
  set $value “hello”;
  set_sha1 $value;
  echo $value;
  }
  set_hmac_sha1
  语法:set_hmac_sha1 $dst   | set_hmac_sha1 $dst
  默认值:no
  配置段:location, location if
  为参数计算HMAC-SHA1值,将结果赋值给参数变量$dst并带上密钥。如:
  location /test {
  set $secret ‘thisisverysecretstuff';
  set $string_to_sign ‘some string we want to sign';
  set_hmac_sha1 $signature $secret $string_to_sign;
  set_encode_base64 $signature $signature;
  echo $signature;
  }
  请求/test,得到:R/pvxzHC4NLtj7S+kXFg/NePTmk=。
  注意:该指令依赖于OpenSSL库,因此编译nginx时,需要–with-http_ssl_module。
  set_random
  语法: set_random $res
  默认值:no
  配置段:location, location if
  生成从与之间的非负数的随机数。含 。
  set_secure_random_alphanum
  语法:set_secure_random_alphanum $res
  默认值:no
  配置段:location, location if
  生成长度为的随机字符串。字符有。
  set_secure_random_lcalpha
  语法:set_secure_random_lcalpha $res
  默认值:no
  配置段:location, location if
  生成长度为的随机字符串。字符有。
  set_rotate
  语法:set_rotate $value
  默认值:no
  配置段:location, location if
  set_local_today
  语法:set_local_today $dst
  默认值:no
  配置段:location, location if
  将本地的今天日期以”yyyy-mm-dd”格式赋值给参数变量$dst。

页: [1]
查看完整版本: nginx+set-misc-nginx-module模块说明