窝窝插件 发表于 2018-11-15 13:15:37

Nginx_rewrite小结

  一.正则表达式匹配,其中:
  * ~ 为区分大小写匹配
  * ~* 为不区分大小写匹配
  * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
  nginx正则表达式:nginx正则表达式,有时间自己在写篇正则表达式的文章(先借鉴下)
  二.文件及目录匹配,其中:
  * -f和!-f用来判断是否存在文件
  * -d和!-d用来判断是否存在目录
  * -e和!-e用来判断是否存在文件或目录
  * -x和!-x用来判断文件是否可执行
  三.rewrite指令的最后一项参数为flag标记,flag标记有:
  1.last    相当于apache里面的标记,表示rewrite。
  2.break本条规则匹配完成后,终止匹配,不再匹配后面的规则。
  3.redirect返回302临时重定向,浏览器地址会显示跳转后的URL地址。
  4.permanent返回301永久重定向,浏览器地址会显示跳转后的URL地址
  四.nginx全局变量
  arg_PARAMETER    #这个变量包含GET请求中,如果有变量PARAMETER时的值。
  args                  #这个变量等于请求行中(GET请求)的参数,如:foo=123&bar=blahblah;
  binary_remote_addr #二进制的客户地址。
  body_bytes_sent    #响应时送出的body字节数数量。即使连接中断,这个数据也是精确的。
  content_length    #请求头中的Content-length字段。
  content_type      #请求头中的Content-Type字段。
  cookie_COOKIE    #cookie COOKIE变量的值
  document_root    #当前请求在root指令中指定的值。
  document_uri      #与uri相同。
  host                #请求主机头字段,否则为服务器名称。
  hostname          #Set to themachine’s hostname as returned by gethostname
  http_HEADER
  is_args            #如果有args参数,这个变量等于”?”,否则等于”",空值。
  http_user_agent    #客户端agent信息
  http_cookie          #客户端cookie信息
  limit_rate            #这个变量可以限制连接速率。
  query_string          #与args相同。
  request_body_file#客户端请求主体信息的临时文件名。
  request_method    #客户端请求的动作,通常为GET或POST。
  remote_addr          #客户端的IP地址。
  remote_port          #客户端的端口。
  remote_user          #已经经过Auth Basic Module验证的用户名。
  request_completion #如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty)。
  request_method    #GET或POST
  request_filename#当前请求的文件路径,由root或alias指令与URI请求生成。
  request_uri          #包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。不能修改。
  scheme                #HTTP方法(如http,https)。
  server_protocol      #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
  server_addr          #服务器地址,在完成一次系统调用后可以确定这个值。
  server_name      #服务器名称。
  server_port          #请求到达服务器的端口号。
    log_formatmain'$remote_addr - $remote_user [$time_local] "$request" \n'  
                      '$status $body_bytes_sent \n "$http_referer" \n'
  
                      '"$http_user_agent" \n "$http_x_forwarded_for" \n' 'arg_PARAMETER $arg_PARAMETER \n'
  
                      'args $args \n' 'binary_remote_addr $binary_remote_addr \n' 'body_bytes_sent $body_bytes_sent \n' 'content_length $content_length \n' 'content_t
  
ype $content_type \n' 'cookie_COOKIE $cookie_COOKIE \n' 'document_root $document_root \n' 'document_uri $document_uri \n' 'host $host \n' 'hostname $hostname \n' 'htt
  
p_HEADER $http_HEADER \n' 'is_args $is_args \n' 'http_user_agent $http_user_agent \n' 'http_cookie $http_cookie \n' 'limit_rate $limit_rate \n' 'query_string $query_s
  
tring \n' 'request_body_file $request_body_file \n' 'request_method $request_method \n' 'remote_addr $remote_addr \n' 'remote_port $remote_port \n' 'remote_user $remo
  
te_user \n' 'request_completion $request_completion \n' 'request_method $request_method \n' 'request_filename $request_filename \n' 'request_uri $request_uri \n' 'sch
  
eme $scheme \n' 'server_protocol $server_protocol \n' 'server_addr $server_addr \n' 'server_name $server_name \n' 'server_port $server_port\n\n\n';
  

  
    access_loglogs/access.logmain;
  备:我对参数不熟的都通过日志打印查看具体保存值
  五、nginx的rewrite规则编写实例
  文件和目录不存在时,重定向到某个文件上:例
    if(!-e $request_filename) {  
      rewrite ^/(.*)$ /index.php last;
  
    }
  多目录转成参数:例 abc.domiam.com/sort/2 => abc.domian.com/index.php?act=sort$name=abc&id=2;
    if($host ~* (.*)\.domian\.com) {  
      set $sub_name $1;
  
      rewrite ^/sort\/(\d+)\/?$ /index.php?act=sort$cid=$sub_name$id=$1 last;
  
    }
  注:部分正则表达式可以在圆括号“()”内,其值可以通过后面的变量$1到$9访问
  目录对换/123456/xxxx -> /xxxx?id=123456 例:
  rewrite ^/(\d+)/(.+)//$2?id=$1 last;
  如果客户端使用IE游览器,则重定向到/nginx-ie目录下:
    if($http_user_agent ~ MSIE) {  
      rewrite ^(.*)$ /nginx-ie/$1 break;
  
    }
  注:last和break 标记的实现功能类似,但二者之间有细微的差别,使用alias指令时必须使用last标记,使用proxy_pass指令时要使用break标记。last标记在本条rewrite规则执行完毕后,会对其所在的server{....}标签重新发送请求,而break标记则在本条规则匹配完成后,终止匹配,不再匹配后面的规则。
  禁止访问多个目录:
    location ~ ^/(cron|templates)/ {  
       deny all;
  
       break;
  
    }
  禁止访问以/data开头的文件:
    location ~ ^/data {  
      deny all;
  
    }
  设置某些类型文件的游览器缓存时间:
    location ~ .*\.(gif|jpg|jep|png|bmp|swf)$ {  
      expires 30d;
  
    }
  
    location ~ .*\.(js|css)?$ {
  
      expires 1h;
  
    }


页: [1]
查看完整版本: Nginx_rewrite小结