理论+实验 详解Nginx Rewrite

目录

  • 一 Nginx Rewrite
    • 1.1 Rewrite跳转场景
    • 1.2 Rewrite跳转实现
    • 1.3 Rewrite实际场景
    • 1.4 Nginx正则表达式
    • 1.5 Rewrite命令
  • 二 Nginx Rewrite基本操作
    • 2.1 location分类
    • 2.2 location优先级
    • 2.3 比较rewrite和location
    • 2.4 Location优先级的示例
    • 2.5 location优先级规则
  • 三 实验
    • 3.1 基于域名的跳转
    • 3.2 基于客户端IP访问跳转
    • 3.3 基于旧域名跳转到新域名后面加目录
    • 3.4 基于参数匹配的跳转
    • 3.5 基于目录下所有php结尾的文件跳转
    • 3.6 基于最普通一条url请求的跳转

一 Nginx Rewrite

1.1 Rewrite跳转场景

理论+实验 详解Nginx Rewrite_第1张图片

1.2 Rewrite跳转实现

理论+实验 详解Nginx Rewrite_第2张图片

1.3 Rewrite实际场景

理论+实验 详解Nginx Rewrite_第3张图片

1.4 Nginx正则表达式

理论+实验 详解Nginx Rewrite_第4张图片

1.5 Rewrite命令

理论+实验 详解Nginx Rewrite_第5张图片
理论+实验 详解Nginx Rewrite_第6张图片

二 Nginx Rewrite基本操作

2.1 location分类

理论+实验 详解Nginx Rewrite_第7张图片

2.2 location优先级

理论+实验 详解Nginx Rewrite_第8张图片

2.3 比较rewrite和location

理论+实验 详解Nginx Rewrite_第9张图片

2.4 Location优先级的示例

理论+实验 详解Nginx Rewrite_第10张图片
理论+实验 详解Nginx Rewrite_第11张图片

2.5 location优先级规则

理论+实验 详解Nginx Rewrite_第12张图片

三 实验

3.1 基于域名的跳转

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
      location / {
        #    root   html;
        #   index  index.html index.htm;
                if ($host = 'www.51xit.top')
                    {
                    rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
}
        }

浏览器输入:http://www.51xit.top,结果跳转到www.52xit.top

3.2 基于客户端IP访问跳转

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;         
        access_log 
        /usr/local/nginx/logs/www.51xit.top.access.log;
        #改日志存储路径 去掉main
        set $rewrite true;
       
        if ($remote_addr = "192.168.100.1"){ 
                set $rewrite false;
        }
        if ($rewrite = true){
                rewrite (.*) /wh.html;
        }
        
        location = /wh.html {
                root /usr/local/nginx/html; 
        }
        
        location / {
           root html;
           index index.html index.htm;
       }
[root@www ~]# killall -s HUP nginx
[root@www ~]# echo "网页维护中" >/usr/local/nginx/html/wh.html

浏览器测试,输入www.51xit.top,正常访问;
登录客户机20.0.0.11,浏览器输入www.51xit.top,提示"网页维护中"

3.3 基于旧域名跳转到新域名后面加目录

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;    
        access_log 
        /usr/local/nginx/logs/www.51xit.top.access.log;
         localtion /post {
                rewrite (.+) http://www.52xit.top/bbs$1 permanent;
        }
[root@www ~]# killall -s HUP nginx

浏览器测试,输入www.51xit.top/post,显示www.52xit.top/bbs

3.4 基于参数匹配的跳转

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;            
        access_log 
        /usr/local/nginx/logs/www.51xit.top.access.log;
   if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
                rewrite (.*) http://www.51xit.top permanent;
        } 
[root@www ~]# killall -s HUP nginx   

浏览器输入http://www.51xit.top/100-100-100.html ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/100-200-10.html ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/100-100-100dddd.html ,页面不能跳转

3.5 基于目录下所有php结尾的文件跳转

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
        listen       80;
        server_name  www.51xit.top;    
        charset utf-8;             
        access_log /usr/local/ng
        inx/logs/www.51xit.top.access.log;
  location ~* /upload/.*\.php$ {
                rewrite (.+) http://www.51xit.top permanent;
        }
 [root@www ~]# killall -s HUP nginx
 

浏览器输入http://www.51xit.top/upload/1.php ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/upload/bbs/1.php ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/upload/index.html ,页面不跳转

3.6 基于最普通一条url请求的跳转

[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
    server {
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;
        access_log 
        /usr/local/nginx/logs/www.51xit.top.access.log;
  location ~* ^/1/test.html {
                rewrite (.+) http://www.51xit.top permanent;
        } 
 [root@www ~]# killall -s HUP nginx    

浏览器输入http://www.51xit.top/1/test.html ,页面显示http://www.51xit.top

你可能感兴趣的:(实验,理论)