架构(day19)
原创
©著作权归作者所有:来自51CTO博客作者佳豪哥哥的原创作品,请联系作者获取转载授权,否则将追究法律责任
Nginx实现Rewrite重写
什么是rewrite
Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。
做伪静态,将动态页面url转换成静态的页面url
Rewrite使用场景
- 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
- 搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入
伪静态的配置
句法:Syntax: rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if
rewrite:模块
regex:正则表达式(匹配当前的url)
replacement:要替换成的url
rewrite http://blackgoatking.com http://www.blackgoatking.com;
#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;
如果懂shell脚本的,这两个就类似于脚本中的,break和continue
rewrite的flag
概述
| flag
|
匹配到last的规则后可以继续匹配后面的location
| last
|
匹配到break的规则后,无法再匹配后面的location
| break
|
302临时重定向
| redirect
|
301永久重定向
| permanent
|
# redirect临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /test {
rewrite ^(.*)$ https://www.baidu.com redirect;
}
}
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /test {
#rewrite ^(.*)$ https://www.baidu.com redirect;
return 302 http://baidu.com;
}
}
# 重新加载nginx
[root@web01 ~]# systemctl reload nginx
# 域名解析
10.0.0.7 rewrite.wu.com
# permanent临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /test {
rewrite ^(.*)$ https://www.baidu.com permanent;
}
}
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /test {
#rewrite ^(.*)$ https://www.baidu.com permanent;
return 301 https://www.baidu.com;
}
}
rewrite实践
开启rewrite日志
# 开启rewrite日志,错误日志的级别要改成 notice,在http层加上rewrite_log on;
events {
worker_connections 1024;
}
http {
rewrite_log on;
include /etc/nginx/mime.types;
default_type application/octet-stream;
...........
}
# 重启nginx
[root@web01 nginx]# systemctl restart nginx
案例一
用户访问 /abc/1.html 实际上真实访问的是 /ccc/bbb/2.html
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
}
}
# 要在code目录下创建ccc/bbb/2.html文件
案例二
用户访问 /2018/ccc/2.html 实际上真实访问的是 /2014/ccc/bbb/2.html
### rewrite写死。。。
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /2018/ccc/2.html {
rewrite ^(.*)$ /2014/ccc/bbb/2.html redirect;
}
}
### 正则后向引用匹配
[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /2018 {
rewrite ^/2018/(.*) /2014/$1 redirect;
}
}
# code目录下要有/2014/ccc/bbb/2.html文件
案例三
用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html
[root@web01 nginx]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wu.com;
root /code;
index index.html;
location /course {
rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
}
}
案例四
80端口强制跳转443端口
server {
listen 80;
server_name www.dirverzeng.com;
rewrite ^(.*) https://$server_name redirect;
#return 302 https://$server_name$request_uri;
}
rewrite做伪静态
if ( -f $request_filename/index.html ){
rewrite (.*) $1/index.html break;
}
if ( -f $request_filename/index.php ){
rewrite (.*) $1/index.php;
}
if ( !-f $request_filename ){
rewrite (.*) /index.php;
}