rewrite的作用

  • 将用户请求的URL重写修改为其他URL的过程
  • 主要用于实现URL地址重写、地址跳转
  • 协议跳转:将用户的http请求跳转为https
  • URL静态化:将动态URL地址显示为伪静态UR0L地址,减少动态URL对外暴露过多的参数信息

rewrite的使用操作

  • set :设置变量
  • if :判断
  • return :返回返回值或URL
  • rewrite :重定向URL

 if指令【server、location】

  • ~ :模糊匹配
  • ~* :不区分大小写
  • ~! :不匹配
  • = :精确匹配

【实验】过滤nginx请求中包含a1=3526的http请求到192.168.200.121:8080端口处理

[root@nginx conf.d]# vim test.conf
server {
  listen 80;
  server_name www.test.org;
  root /code;
  location / {
    index index.html;
    if ( $request_uri ~* 'a1=\d{4,8}' ) {

      # \d 表示数字,{4,8}表示出现的数字有4-8个

      proxy_pass http://192.168.200.121:8080;
    }
  }
}

rewrite-set

【实验】将用户请求url.test.org.zh跳转到url.test.org

[root@nginx code]# vim /etc/nginx/conf.d/test.conf 
server {
  listen 80;
  server_name www.test.org.zh www.test.org.jp;
  location / {
    if ( $http_host ~* "zh" ) {
        set $diy zh;
    }
    if ( $http_host ~* "jp" ) {
        set $diy jp;
    }
    rewrite ^/$ http://www.test.org/$diy/ permanent;
  }
}
server {
  listen 80;
  server_name www.test.org;
  root /code;
  location / {
    index index.html;
  }
}

rewrite-return【返回:状态码、状态码+OK、url地址】

【实验1】如果用户使用IE|firefox浏览器访问www.test.org,则返回200状态码+提示更换浏览器

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    index index.html;
    if ($http_user_agent ~* "MSIE|firefox") {
      return 200 '请更换浏览器';
    }
  }
}

【实验2】如果用户使用IE或者firefox浏览器访问www.test.org,则返回500错误

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    index index.html;
    if ($http_user_agent ~* "MSIE|firefox") {
      return 500;
    }
  }
}

【实验3】如果用户使用IE或者firefox浏览器访问www.test.org,则直接跳转京东网站

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    index index.html;
    if ($http_user_agent ~* "MSIE|firefox") {
      return 301 https://jd.com;
    }
  }
}

rewrite-break-last

  • 用于重写url或者跳转url的指令
  • last :规则匹配后,将匹配的uri重新发起请求
  • break :规则匹配后,不在匹配
  • redirect :302临时重定向,地址会显示跳转后的地址
  • permanent :301永久重定向,地址会显示跳转后的地址

 【实验1】使用last和break的查看区别

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    rewrite /1.html /2.html break;
    rewrite /2.html /3.html;
  }
  location /2.html {
    rewrite /2.html /a.html;
  }
  location /3.html {
    rewrite /3.html /b.html;
  }
}

[root@nginx code]# echo "1.html" >> /code/1.html
[root@nginx code]# echo "2.html" >> /code/2.html
[root@nginx code]# echo "3.html" >> /code/3.html
[root@nginx code]# echo "a.html" >> /code/a.html
[root@nginx code]# echo "b.html" >> /code/b.html

redirect和permanent的区别

  • redirect:302临时跳转,浏览器不会记录结果
  • permanent【推荐】:301永久跳转,请求结果会被浏览器保存,即使nginx没启动,也能访问到内容
server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    rewrite /1.html /2.html redirect;
    rewrite /2.html /3.html;
  }
  location /2.html {
    rewrite /2.html /a.html;
  }
  location /3.html {
    rewrite /3.html /b.html;
  }
}
server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
  root /code;
  location / {
    rewrite /1.html /2.html permanent;
    rewrite /2.html /3.html;
  }
  location /2.html {
    rewrite /2.html /a.html;
  }
  location /3.html {
    rewrite /3.html /b.html;
  }
}

【语言】根据用户请求头中携带的语言调度到不同的页面

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code;
    location / {
      index index.html;

      if ($http_accept_language ~* "zh-CN | zh") {
        set $language zh;
      }
      if ($http_accept_language ~* "en") {
        set $language en;
      }

    rewrite ^/$ /$language redirect;

  }
}

用户使用手机设备访问,从www.test.org跳转到www.test.org/m

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code;
    location / {
      index index.html;
      
      if ($http_user_agent ~* "android|phone|iphone") {
        rewrite ^/$ /m break;
      }
  }
}

用户使用手机设备访问www.test.org跳转到mmm.test.org

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code;
    location / {
      index index.html;

      if ($http_user_agent ~* "android|phone|iphone") {
        rewrite ^(.*)$ http://mmm.test.org redirect;
      }
  }
}

用户通过http协议请求,自动跳转到https(两种方式)

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
    location / {
      rewrite ^(.*)$ https://$http_host$1;
      #return 302 https://$http_host$request_uri;
  }
}

网站维护,用户访问重定向到维护页面

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code/;

    rewrite ^(.*)$ /wh.html break;

    location / {
      index index.html;
  }
}

当用户访问404、403、401、502时,自动跳转到维护页面

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code/;
    location / {
      index index.html;
  }
    error_page 403 404 500 502 @error_tmp;
    location @error_tmp {
      rewrite ^(.*)$ /404.html break;
  }
}

 当网站维护时,指定IP能够正常访问,其他的IP跳转到维护页面

server {
  listen 80;
  server_name www.test.org;
    charset utf-8;
    default_type text/html;
      root /code/;
    location / {
      index index.html;
      set $ip 0;
      if ($remote_addr = "192.168.200.10") {
        set $ip 1;
    }
      if ($ip = 0) {
        rewrite ^(.*)$ /wh.html break;
    }
  }
}