rewrite跳转

https://www.cnblogs.com/fengff/p/12557440.html
https://www.jianshu.com/p/74bf196b63cc
https://blog.csdn.net/qq_41475058/article/details/89516051
https://www.cnblogs.com/brianzhu/p/8624703.html
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

和Apache等Web服务软件一样,Nginx rewrite的主要功能也是实现URL地址重写。Nginx的rewrite规则需要PCRE软件的支持,即通过Perl兼容正则表达式语法进行规则匹配。默认参数编译时,Nginx就会安装支持rewrite的模块,但是,也必须要有PCRE软件的支持。
rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分的内容,重定向到replacement部分,结尾是flag标记。

指令语法:rewrite regex replacement 【flag】;
应用位置:server,location,if

regex常用正则表达式说明
image.png

rewrite指令的最后一项参数flag标记的说明
image.png
说明:

  • last和break用来实现URL重写,浏览器地址栏的URL地址不变,但在服务器端访问的程序及路径发生了变化。
  • redirect和permanent用来实现URL跳转,浏览器地址栏会显示跳转后的URL地址。

last和break标记的实现功能类似,但二者之间有细微的差别

  • 使用alias指令时必须用last标记,使用proxy_pass指令时要使用break标记。
  • last标记在本条rewrite规则执行完毕后,会对其所在的server{…}标签重新发起请求。
  • break标记则会在本条规则匹配完成后,终止匹配,不再匹配后面的规则。

注意:
在server中使用rewrite ,我们使用的flag是last;但是在location中,我们却只能用break
—因为,如果在location的rewrite也使用last,便会再次以新的URI重新发起内部重定向,再次进行location匹配,而新的URI中极有可能和旧的URI一样再次匹配到相同location中,这样死循环发生了。当循环到第10次时, Nginx会终止这样无意义的循环,并返回500错误。这点需要特别的注意

URL跳转

字面意思,我输入了一个地址,跳转到其他地址上的页面,这叫跳转
比如当前的网址是www.92test.com,后续发展起来后,访问的人也多了,有时候懒得打www,而是直接输入url 92test.com就可以跳转到官网地址上;
又或者是当前网址是www.92test.com,原本我有一个地址是xyz.92test.com,但是各种原因这个xyz的url不用了,但是又有很多人知道这个地址,怎么能让其他人访问页面不会以为是不用了的错误页面呢?可以跳转到一个默认地址上去。是的,就是www上去
当然,我们可以用server_name重新指向一个url地址,实际上还可以用rewrite 301这种临时的跳转方式来实现

本次测试居然发现92test居然也有这样一个地址,所以临时修改了server_name

nginx的配置如下
[root@nginx-web02 nginx]# cat conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.yjs.com;
access_log logs/host.access.log main;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name yjs.com;
rewrite ^/(.*) http://www.yjs.com/$1 permanent;
}
}
[root@nginx-web02 nginx]#
测试使其跳转,也可以在浏览器里测试
image.png
image.png
无论地址栏输入的是www.yjs.com还是yjs.com,(又或者是www.yjs.com还是ops.yjs.com),最终都会到www.yjs.com地址下的页面

URL重写

重写,比如当前有两个server
www.yjs.com和ops.yjs.com
image.png
这里比如ops的地址已经作废,但是页面还是不能让客户访问的时候返回错误页面,这时候可以让他跳转到主页上去
查看nginx的配置文件
[root@nginx-web02 nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx-web02 nginx]# nginx -s reload
[root@nginx-web02 nginx]# cat conf/nginx.conf
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘“$http_user_agent” “$http_x_forwarded_for”‘;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.yjs.com;
access_log logs/host.access.log main;
location / {
root html/www;
index www.html www.htm;
}
}
server {
listen 80;
server_name ops.yjs.com;
access_log logs/host.access.log main;
location / {
root html/ops;
index ops.html ops.htm;
}
if ($http_host ~ “^(.).yjs.com$”) {
set $domain $1;
rewrite ^(.*) http://www.yjs.com/$domain/ops.html break;
}
}
}
[root@nginx-web02 nginx]#
查看网页的目录文件
[root@nginx-web02 nginx]# tree html/
html/
├── 50x.html
├── index.html
├── ops.html
└── www
├── ops
│ └── ops.html
└── www.html
2 directories, 5 files
[root@nginx-web02 nginx]#
注意:
这里我设置的跳转是跳转到www的之后的
if ($http_host ~ “^(.).yjs.com$”) {
set $domain $1;
rewrite ^(.) http://www.yjs.com/$domain/ops.html break;
}
这里设置(.
)为$1,这个地方类似awk的使用一样的(其实有点忘记了,后续恶补一下)
然后nginx又set设置$domain为$1,也就是ops
所以这个地方需要注意,查看上面www的网页根目录是html/www,那么跳转后的页面需要在此基础上开始展示的
也就是如tree html/展示的那样,跳转后的目录要在html/www/之下成立的ops/ops.html路径才对
image.png