zoukankan      html  css  js  c++  java
  • nginx map初探

    1. map指令由ngx_http_map_module模块提供,并且默认加载。
    2. map指令用来创建变量,仅在变量被接受的时候执行视图映射操作。
    3. map指令配置段位于http段内。
    4. map指令有三个参数:
      default:默认值,当没有设置 default,将会用一个空的字符串作为默认的结果。
      hostnames:允许用前缀或者后缀掩码指定域名作为源变量值。(这个参数必须写在值映射列表的最前面)
      include:包含一个或多个含有映射值的文件。
    5. 可以使用正则表达式:
      ~ 开头,表示这个正则表达式对大小写敏感。
      ~*开头,表示这个正则表达式对大小写不敏感。
    6. 使用示例
      http {
          map $http_user_agent $agent {
              ~curl curl;
              ~*chrome chrome;
          }
          server {
              listen       8080;
              server_name  www.test.com;
      
              location /hello {
                  default_type text/plain;
                  echo http_user_agent: $http_user_agent;
                  echo agent: agent:$agent;
              }
          }
      }
      
      执行curl 127.0.0.1:8080/hello得到如下信息
      http_user_agent: curl/7.15.5 (x86_64-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
      agent: curl
    7. 使用实例
      维护一个ip.conf的配置文件,实现根据ip自动切换目录并且不用频繁重启nginx
      ip.conf实例内容如下
      192.168.1.1 dir1;
      192.168.1.2 dir2;
      
      nginx配置如下
      http {
          map $ip $dir {
              default test;
              include ip.conf;
          }
          server {
              listen       8080;
              server_name  www.test.com;
              root /data/$dir/www;
              rewrite ^/(js|images|css)/(.*) http://static.test.com/$1/$2 permanent;
              location / {
                   try_files $uri $uri/ /index.php?$query_string;
              }
          }
      }
      
  • 相关阅读:
    PHP中防止SQL注入的方法
    SQLmap超详细文档和实例演示
    Spring Cloud 个人心得 理论
    Spring注解说明
    git各角色权限描述
    jenkins调用tomcat重启命令 && tomcat重启脚本
    nginx1.5 升级到1.14
    发布项目
    spring boot 多线程
    Jmeter测试http请求
  • 原文地址:https://www.cnblogs.com/junly/p/6992859.html
Copyright © 2011-2022 走看看