commit de45adaadcb2026c5d2c215c03ce7919ce864ed0 Author: Your Name Date: Sat Dec 24 11:38:07 2022 +0800 提交nginx地址重写案例 diff --git a/rewrite_test1.conf.bak b/rewrite_test1.conf.bak new file mode 100644 index 0000000..9217332 --- /dev/null +++ b/rewrite_test1.conf.bak @@ -0,0 +1,9 @@ +location / { + root /usr/share/nginx/html; + if ( -f $request_filename ){ + return 301 http://www.baidu.com; + } + if ( -d $request_filename ){ + return 301 http://www.jd.com; + } +} diff --git a/rewrite_test10.conf.bak b/rewrite_test10.conf.bak new file mode 100644 index 0000000..f2d4d69 --- /dev/null +++ b/rewrite_test10.conf.bak @@ -0,0 +1,17 @@ +#http://alice.testpm.com ==> http://www.testpm.com/alice +#http://jack.testpm.com ==> http://www.testpm.com/jack + +location / { + if ( $host ~ www.testpm.com ){ + break; + } + if ( $host ~ ^(.*)\.testpm.com$){ + set $user $1; + rewrite .* http://www.testpm.com/$user permanent; + } +} + +location /alice { + root /usr/share/nginx/html; + index a.html; +} diff --git a/rewrite_test2.conf.bak b/rewrite_test2.conf.bak new file mode 100644 index 0000000..97c89bb --- /dev/null +++ b/rewrite_test2.conf.bak @@ -0,0 +1,5 @@ +location / { + if ( $request_uri ~* abc ){ + return 402; + } +} diff --git a/rewrite_test3.conf.bak b/rewrite_test3.conf.bak new file mode 100644 index 0000000..49d3c4a --- /dev/null +++ b/rewrite_test3.conf.bak @@ -0,0 +1,9 @@ +# http://www.testpm.com/a/1.html ==> http://www.testpm.com/b/2.html +location /a/1.html { + # http://ip/a/1.html => http://ip/b/2.html + rewrite .* /b/2.html last; # 如果后面的路径没有加http开头,则表示本站的路径, flag 没有写默认是last +} + +location /b { + root /usr/share/nginx/html; +} diff --git a/rewrite_test4.conf.bak b/rewrite_test4.conf.bak new file mode 100644 index 0000000..9ad4133 --- /dev/null +++ b/rewrite_test4.conf.bak @@ -0,0 +1,8 @@ +# http://www.testpm.com/2019/a/1.html ==> http://www.testpm.com/2018/a/1.html +location /2019 { + rewrite ^/[0-9]+(/.*)$ /2018$1 permanent; +} + +location /2018 { + root /usr/share/nginx/html; +} diff --git a/rewrite_test5.conf.bak b/rewrite_test5.conf.bak new file mode 100644 index 0000000..d766c9d --- /dev/null +++ b/rewrite_test5.conf.bak @@ -0,0 +1,4 @@ +# http://www.qf.com/a/1.html ==> http://jd.com +location /a/1.html { + rewrite .* https://www.jd.com; +} diff --git a/rewrite_test6.conf.bak b/rewrite_test6.conf.bak new file mode 100644 index 0000000..b17fef9 --- /dev/null +++ b/rewrite_test6.conf.bak @@ -0,0 +1,4 @@ +# http://www.qf.com/a/1.html ==> http://jd.com/a/1.html +location / { + rewrite .* http://www.baidu.com$request_uri permanent; +} diff --git a/rewrite_test7.conf.bak b/rewrite_test7.conf.bak new file mode 100644 index 0000000..3e3d7e2 --- /dev/null +++ b/rewrite_test7.conf.bak @@ -0,0 +1,11 @@ +# http://www.tianyun.com/a/b/c +# $1: /a/b +# $2: c +# http://$host$1$2$3/ + +location /a/b/c { + root /usr/share/nginx/html; + if ( -d $request_filename ) { + rewrite ^(.*)([^/])$ $1$2/ permanent; + } +} diff --git a/rewrite_test8.conf.bak b/rewrite_test8.conf.bak new file mode 100644 index 0000000..c523576 --- /dev/null +++ b/rewrite_test8.conf.bak @@ -0,0 +1,10 @@ +# 伪静态化 +# http://www.tianyun.com/login/tianyun.html ==> http://www.tianyun.com/reg/login.html?user=tianyun + +location /login { + rewrite /(.*)/(.*)\.html$ /reg/$1.html?user=$2 permanent; +} + +location /reg { + root /usr/share/nginx/html; +} diff --git a/rewrite_test9.conf.bak b/rewrite_test9.conf.bak new file mode 100644 index 0000000..8540d7f --- /dev/null +++ b/rewrite_test9.conf.bak @@ -0,0 +1,12 @@ +#http://www.tianyun.com/qf/11-22-33/1.html ==> http://www.tianyun.com/qf/11/22/33/1.html +#http://ip/2019-10-23/hello.html ==> http://ip/2019/10/23/hello.html + +# /qf/11-22 +location ~ qf/[0-9]+- { + rewrite /qf/([0-9]+)-([0-9]+)-([0-9]+)/(.*)$ /qf/$1/$2/$3/$4 permanent; +} + +# /qf/11/22 +location ~ ^/qf/[0-9]+/.*$ { + root /usr/share/nginx/html; +}