Ngixn实现Rewrite重写
1.rewrite概述
rewrite主要功能是实现url地址重写,以及重定向,就是将传入的web请求重定向到其他url的过程。也可以做伪静态。将动态页面url转换成静态页面的url
2.rewrite使用场景
1.地址跳转:
用户输入该域名的一部分跳转到该完整域名的界面
2.协议跳转:
用户输入http协议时跳转到https协议
3.伪静态:
将动态页面显示静态页面方式的一种技术,便于搜素引擎(SEO优化依赖于url路径,好记的url便于支持搜素引擎的录入)的录入,同时也防止动态页面url地址对外暴露过多的参数,提高安全性
3.伪静态的基础配置语法
# rewrite模块 正则表达式(匹配当前的url) 需要替换成的url
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
4.rewrite的flag
| flag |
概述 |
| last |
匹配到last的规则后可以继续匹配后面的location |
| break |
匹配到break的规则后不能继续匹配后面的location |
| redirect |
302临时重定向 |
| permanent |
301永久重定向 |
4.1.redirect临时重定向配置
1.编写nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/redirect.conf
server {
listen 80;
server_name redirect.jl.com;
root /code;
index index.html;
location /test{
rewrite ^(.*)$ http://zhangweishihundan.com redirect;
# 或 return 302 http://zhangweishihundan.com;
}
}
2.重启nginx
systemctl reload nginx
3.域名解析
10.0.0.7 redirect.jl.com
4.浏览器访问
![]()
4.2.permanent永久重定向
1.编写nginx配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/permanent.conf
server {
listen 80;
server_name permanent.jl.com;
root /code;
index index.html;
location /test {
rewrite ^(.*)$ http://baidu.com permanent;
# 或 return 301 http://baidu.com;
}
}
2.重启nginx
systemctl reload nginx
3.域名解析
10.0.0.7 permanent.jl.com
4.浏览器访问
![]()
5.redirect和permanent的区别
当当前的服务器的nginx停掉后,临时重定向会跳转不了网站,但永久重定向还会跳转网站
![]()
![]()
rewrite实践
开启rewrite日志
# 开启rewrite日志,错误日志的级别要改成notice,在http层加上rewrite_log on
error_log /var/log/nginx/error.log notice;
http {
...
rewrite_log on;
}
重启nginx
systemctl reload nginx
案例1
## 用户访问 /abc/1.html 实际上真实访问的是 /ccc/bbb/2.html
1.编写配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/abc.conf
server {
listen 80;
server_name rewrite.jl.com;
root /code;
index index.html;
location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
}
}
2.域名解析
10.0.0.7 rewrite.jl.com
3.重启nginx
systemctl reload nginx
4.创建站点目录
[root@web01 ~]# mkdir -p /code/ccc/bbb
5.部署index页面
[root@web01 ~]# echo '2.html' > /code/ccc/bbb/2.html
6.浏览器访问/abc/1.html
![]()
案例2
## 用户访问 /2018/ccc/2.html 实际上真实访问的是 /2014/ccc/2.html
1.编写配置文件
server {
listen 80;
server_name rewrite.jl.com;
root /code;
index index.html;
location /2018 {
rewrite ^/2018/(.*) /2014/$1 redirect;
}
}
2.创建站点目录
[root@web01 ~]# mkdir /code/2014/ccc -p
3.重启nginx
systemctl reload nginx
4.部署index页面
[root@web01 ~]# echo '2014/ccc' > /code/2014/ccc/2.html
5.浏览器访问
![]()
案例3
## 用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html
1.编写配置文件
server {
listen 80;
server_name rewrite.jl.com;
root /code;
index index.html;
location /course {
rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html;
}
}
2.创建站点目录
[root@web01 ~]# mkdir /code/course/11/22/33/ -p
3.部署index页面
[root@web01 ~]# echo 'course_33' > /code/course/11/22/33/course_33.html
4.重启nginx
systemctl reload nginx
5.浏览器访问
![]()
案例四
## 80端口强制跳转443端口
1.编写配置文件
server {
listen 80;
server_name rewrite.jl.com;
rewrite ^(.*) https://$server_name redirect;
# 或 return 302 https://$sever_name$request_uri;
}
2.重启nginx
3.浏览器访问
![]()
rewrite做伪静态
wordpress博客网站的伪静态代码
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;
}
# 在location层添加该代码
使用论坛网站做伪静态
1.访问论坛网站
![]()
2.点击管理中心
![]()
3.点击全局的seo设置
![]()
4.将url静态化全部点上
![]()
5.点击提交,点查看当前的rewrite规则
![]()
6.找到nginx的伪静态代码复制
![]()
7.复制代码到nginx配置文件中
[root@web02 /code]# vim /etc/nginx/conf.d/luntan.conf
server {
listen 80;
server_name luntan.jl.com;
root /code/upload;
index index.php index.html;
location /{
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
return 404;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
8.此时访问网站为动态页面
![]()
9.重启nginx
systemctl reload nginx
10.查看页面的url,此时显示为静态界面
![]()