openresty 重定向,



openresty 重定向

        

             

                                  

重定向

      

重定向:避免网站路径、域名修改后,网站中原有链接失效

        

命令格式:rewrite old_url new_url  [flag]

old_url:旧的url路径,可为正则表达式
new_url:新的跳转的url,匹配正则表达式后,跳转到该地址

# flag:进一步处理的标识
last:终止rewrite,服务端跳转到指定路径
break:终止rewrite,不再继续匹配
redirect:临时重定向,返回响应码302
permanent:永久重定向,返回响应码301

           

             

                                  

示例

      

default.conf

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    #location 1
    location /break {
        root /usr/share/nginx/html2;
 
        if ( !-e $request_filename ){    #root目录下查找文件/default/info
             rewrite ^/break/(.*)  /default/info  break;
        }
    }
 
    #location 2
    location /break2 {
        root /usr/share/nginx/html2;
 
        if ( !-e $request_filename ){    #root目录下查找文件/default/info,
                                         #随后继续执行下面语句,echo "break"
             rewrite ^/break2/(.*)  /default/info  break;
             echo "break";
        }
    }

    #location 3
    location /last {
        if ( !-e $request_filename ){    #匹配后,跳转到/test/$1路径
             rewrite ^/last/(.*)  /test/$1  last;
             echo "last";
        }
    }

    #location 4
    location /test {
        echo "test";
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

          

本地文件

huli@hudeMacBook-Pro redirect % pwd
/Users/huli/lua/openresty/redirect

huli@hudeMacBook-Pro redirect % ls html     
default
huli@hudeMacBook-Pro redirect % ls html/default
info
huli@hudeMacBook-Pro redirect % cat html/default/info
break info

       

创建容器

docker run -it -d -p 1000:80 \
-v /Users/huli/lua/openresty/redirect/html:/usr/local/openresty/nginx/html2 \
-v /Users/huli/lua/openresty/redirect/default.conf:/etc/nginx/conf.d/default.conf \
--name open2 lihu12344/openresty

         

使用测试

huli@hudeMacBook-Pro redirect % curl localhost:1000/break/1
break info

huli@hudeMacBook-Pro redirect % curl localhost:1000/break2/1
break

huli@hudeMacBook-Pro redirect % curl localhost:1000/last    
last
huli@hudeMacBook-Pro redirect % curl localhost:1000/last/1
test

huli@hudeMacBook-Pro redirect % curl localhost:1000/test
test

         

             

                                  

示例 2

      

default.conf

server {
    listen       80;
    server_name  localhost;
 
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
 
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
 
    location /redirect {
        if ( !-e $request_filename ) {
             rewrite ^/redirect/(.*)  /test/$1  redirect;
        }
    }
 
    location /permanent {
        if ( !-e $request_filename ) {
             rewrite ^/permanent/(.*)  /test/$1 permanent;
        }
    }
 
    location /test {
        echo "test";
    }
 
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
 
}

        

创建容器

docker run -it -d -p 2000:80 \
-v /Users/huli/lua/openresty/redirect/default2.conf:/etc/nginx/conf.d/default.conf \
--name open3 lihu12344/openresty

       

使用测试

huli@hudeMacBook-Pro redirect % curl -I localhost:2000/redirect/1
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:08 GMT
Content-Type: text/html
Content-Length: 151
Location: http://localhost/test/1
Connection: keep-alive

huli@hudeMacBook-Pro redirect % curl -I localhost:2000/permanent/1
HTTP/1.1 301 Moved Permanently
Server: openresty/1.21.4.1
Date: Mon, 04 Jul 2022 04:50:17 GMT
Content-Type: text/html
Content-Length: 175
Location: http://localhost/test/1
Connection: keep-alive

         

              

相关内容