zoukankan      html  css  js  c++  java
  • ngx_http_rewrite_module(重定向)

    1:指定rewrite规则
    rewrite regex replacement [flag];
     
    什么是rewrite规则:If the specified regular expression matches a request URI, URI is changed as specified in the replacement string. The rewrite directives are executed sequentially in order of their appearance in the configuration file. It is possible to terminate further processing of the directives using flags. If a replacement string starts with “http://”, “https://”, or “$scheme”, the processing stops and the redirect is returned to a client.
     
    欢哥人工翻译(可以打赏哦!!!):如果regex匹配到用户访问的URI,那么用户访问的URI就会被替换为replacement所指定的string。这个replacement所指定的string会继续向后执行,但是执行到哪里结束会给flags这个参数所决定。如果这个replacement是“http://”, “https://”, 或者是“$scheme”开头的话,那么rewrite将不会向后执行,会直接终止并返回给客户端
     
    rewrite的工作法则:
    1:rewrite实现重写操作,匹配URI可以使用正则表达式匹配,替换可以使用正则表达式的后向引用
    2:如果在location中存在多个rewrite规则会自上而下逐个被匹配检查,可以使用flag参数控制
    3:如果replacement string是以“http://”, “https://”, 或者是“$scheme”开头的话,则替换结果会直接以重定向方式返回给用户
     
    flag参数详解:
    1:last:表示结束本轮rewrite规则从上之下进行检查,而是从最开始从新循环检查,且请求结果不会返回给客户端(状态码为:200)
    2:break:重写完成后不会对将前URI进行后续的匹配检查,直接将URI请求的资源返回给客户端(状态码为:200)
    3:redirect:重定向完成后以临时重定向的方式直接返回新的URL给客户端,客户端再对新的URL发起请求(状态码为:302)
    4:permanent:重定向成后以永久重定向的方式直接返回新的URL给客户端,客户端在对新的URL发起请求(状态码为:301)
     
    避免死循环匹配:(加一个break)
    location / {
    root /app;
    index index.html index.htm;
    rewrite (.*).htm$ $1.html break;
    }
     
    location ~* .html$ {
    root /app;
    index index.html index.htm;
    rewrite (.*).html$ $1.htm break;
    }
     
     
    2:指定rewrite是否记录到日志,如果启用,这些rewrite日志会记录到错误日志中
    rewrite_log on | off;
     
    3:设置条件判断,如果条件满足,执行指定的指令
    判断表达式:
    ==:精确等于
    !=:不等于
    ~:模式匹配,区分字母大小写
    ~*:模式匹配,不区分字母大小写
    !~:模式不匹配,区分字母大小写
    !~*:模式不匹配,不区分字母大小写
     
    检查file或directory的属性:
    -f, !-f:如果文件存在
    -d, !-d: 如果目录存在
    -e, !-e:如果文件或目录或链接存在
    -x , !-x:如果文件可执行
     
    例如:
    if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
    }
     
    if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
    }
     
    if ($request_method = POST) {
    return 405;
    }
     
    if ($slow) {
    limit_rate 10k;
    }
     
    if ($invalid_referer) {
    return 403;
    }
     
    4:自定义变量
    set $variable value
     
    例如:
    set $hello "ni hao"
     
    5:指定返回的状态码或URL,停止处理URI匹配过程
    return code
     
    例如:
    if ($invalid_referer) {
    return 403 "<p> wrong page </p>"
    }
    • ngx_http_gzip_module(压缩)
    功用:用户压缩文本资源
     
    1:启用gzip压缩功能
    gzip on | off;
     
    2:指定压缩比例,1-9,默认为1
    gzip_comp_level level;
     
    3:开启压缩功能的最小的长度
    gzip_min_length length;
     
    4:设定协议的最小版本以上都可以压缩
    gzip_http_version 1.0 | 1.1;
     
    5:设定压缩的资源内容类型,默认为text/html
    gzip_types mime-type …;
     
     
    6:如果Nginx是方向代理如果进行压缩
    gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any …;
     
    例如:
    gzip on
    gzip_http_version 1.1
    gzip_disable msle6
    gzip_min_lengh 1000
    gzip_types text/plain text/css text/js application/xml
    gzip_proxied expired no-cache no-store private auth;
    • ngx_http_fastcgi_module(fastcgi功能)
    对于LAMP来说:实现fastcgi协议的模块为proxy_fastcgi_module,
    对于LNMP来说:实现fastcgi协议的模块为ngx_http_fastcgi_module,
     
    fastcgi模块的指令说明:
     
     
    1:在Nginx中指定php-fpm服务器监听的地址和端口
    fastcgi_pass address;
    例如:fastcgi_pass 127.0.0.1:9000;
     
    2:定义fastcgi应用的主页名称
    fastcgi_index name;
    例如:fastcgi_index index.php;
     
    3:传递给php-fpm服务器参数及其值
    fastcgi_param parameter value [if_not_empty];
    例如:fastcgi_param SCRIPT_FILENAME /apps/php/$fastcgi_script_name;
    需要导入fastcgi_param参数文件,用include fastcgi_params; 这个 fastcgi_param文件里面有各种的变量,其中就有$fastcgi_script_name。
     
     
    4:fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
     
    path:指明缓存文件系统路径,用于缓存文件数据,在这个路径下会有n级结构,实现对文件数据的快速查找,使用的hash算法
     
    max_size=size:此路径下指定多大的空间用于缓存数据
     
    levels=#:指定缓存目录的层级结构
    例如:levels=1:2 表示:一级目录的名称为1个字符,二级目录的名称为2个字符
     
    keys_zone=name:size:指定磁盘上用于存储value的存储空间名称、大小
     
    inactive=time:指定非活动时间
     
    max_size=size:指定存储的空间大小
     
    注意:这个只能用于http上下文(/var/cache/nginx_fastcgi目录需要先创建好)
    例如:在http的上下文中指定:fastcgi_cache_path /var/cache/nginx_fastcgi levels=1:2:3 keys_zone=fcgi_cache:10m inactive=3h;
    在一个fastcgi的location中调用这个fastcgi_cache存储空间名
    例如:
    location ~ .php$ {
    fastcgi_cache fcgi_cache;
    }
     
     
    5:是否启用cache功能,如何启用,数据缓存在哪个cache中
    fastcgi_cache zone | off;
    例如:在使用fastcgi的location中添加:fastcgi_cache fcgicache; 指明调用的缓存的名字
     
    6:指明在缓存中使用什么作为k/v键值对中的key
    fastcgi_cache_key string;
    例如:在使用fastcgi的location中添加:fastcgi_cache_key $request_uri; 指明以用户请求的uri为key
     
     
    7:指明缓存哪些请求方法的数据
    fastcgi_cache_methods GET | HEAD | POST …;
    例如:fastcgi_cache_methods GET
     
    8:指定请求匹配的最小次数,大于最小次数才会缓存
    fastcgi_cache_min_uses number;
    例如:fastcgi_cache_min_uses 5;
     
    9:指明对应的响应码缓存的时间,没有定义是不会缓存的
    fastcgi_cache_valid [code …] time;
    例如:
    fastcgi_cache_valid 200 30s;
    fastcgi_cache_valid 301 302 1m;
    fastcgi_cache_valid any 10s;
     
    注意:用使用缓存机制,需要在location中指定如下三个指令
    fastcgi_cache
    fastcgi_cache_key
    fastcgi_cache_valid
     
     
     
     
    在Nginx中使用ngx_http_fastcgi_module示例
    location ~ .php$ {
    # 将客户端请求的URI从$fastcgi_script_name变量中取出来,且通过fastcgi发送给php服务器,fastcgi://192.168.23.201:9000/apps/php/$fastcgi_script_name。那么PHP服务器就会在本地的文件系统去找/apps/php/$fastcgi_script_name资源。
    fastcgi_pass 192.168.23.201:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /apps/php/$fastcgi_script_name;
    include fastcgi_params;
    # 指明缓存的键,这里存储的是key值
    fastcgi_cache_key $request_uri;
    # 调用缓存的磁盘存储空间,这里存储的是value值
    fastcgi_cache fcgi_cache;
    # 指定可以缓存的状态码
    fastcgi_cache_valid 200 30s;
    fastcgi_cache_valid 301 302 1m;
    fastcgi_cache_valid any 10s;
    }
  • 相关阅读:
    C++调用C#类库函数
    C# DataSet转JSON
    抽象工厂模式
    工厂方法模式
    简单工厂模式
    jsp页面中的EL表达式不被解析org.apache.jasper.JasperException: Unable to convert string [${item.createtime}]
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory'
    mybatis第二天
    MyBatis入门
    js自动访问数据库
  • 原文地址:https://www.cnblogs.com/liu1026/p/7537955.html
Copyright © 2011-2022 走看看