微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

day15.2

Nginx实现rewrite重写

# 什么是rewrite
Rewrite主要实现URL地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。
做伪静态,将动态页面url转换成静态的页面url

rewrite使用场景

1.地址跳转
www.taobao.com跳转成 main.m.taobao.com

2.协议跳转
http://blog.driverzeng.com 跳转成:https://blog.driverzeng.com

3.伪静态
	将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
	搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入

伪静态的配置

# 语法
句法:Syntax: rewrite regex replacement [flag]
认:Default: --
语境:Context: server,location,if

rewrite:模块
regex:正则表达式
replacement:要替换的URL

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.zls.com;
        root /code;
        index index.html;

        location /test {
                rewrite ^(.*)$  https://www.baidu.com redirect;
        }
}

# 重新加载Nginx
[root@web01 ~]# systemctl reload Nginx

# 域名解析
10.0.0.7 rewrite.zls.com

# permanent临时重定向配置
[root@web01 ~]# vim /etc/Nginx/conf.d/rewrite.conf

server {
        listen 80;
        server_name rewrite.zls.com;
        root /code;
        index index.html;

        location /test {
                rewrite ^(.*)$  https://www.baidu.com permanent;
        }
}

# 重新加载Nginx
[root@web01 ~]# systemctl reload Nginx

# 域名解析
10.0.0.7 rewrite.zls.com

# last与break区别对比
[root@web01 ~]# !vim
vim /etc/Nginx/conf.d/rewrite.conf

server {
        listen 80;
        server_name rewrite.zls.com;
        root /code;
        index index.html;

        location ~^/break {
                rewrite ^/break /test/ break;
        }

        location ~^/last {
                rewrite ^/last /test/ last;
        }
        location /test {
                rewrite ^(.*)$  https://www.baidu.com permanent;
        }
}

# last和break的区别
break只要匹配到规则,则会取本地配置路径的目录寻找的请求的文件;
而last只要匹配到规则,会对其所在的server层的location继续访问


rewrite实践

开启rewrite日志

# 开启rewrite日志,错误日志的级别要改成 notice,在http加上rewrite_log on;
[root@web01 ~]# vim /etc/Nginx/Nginx.conf 
        rewrite_log on;

# 重启Nginx
[root@web01 ~]# systemctl reload Nginx

案例一

# 用户访问/abc/1.HTML实际上真实访问的是/ccc/bbb/2.html
[root@web01 bbb]# vim /etc/Nginx/conf.d/al.conf

server {
        listen 80;
        server_name rew.wc.com;
        root /code;
        index index.html;

        location /abc/1.html {
                rewrite ^(.*)$ /ccc/bbb/2.html redirect;
        }
}

# 创建以下站点目录
[root@web01 bbb]# mkdir /code/ccc/bbb -p
[root@web01 bbb]# vim /2.html

#  重启Nginx
[root@web01 Nginx]# systemctl restart Nginx

案例二

# 用户访问/2018/ccc/2.html 实际上真实访问的是 /2014/ccc/bbb2.html
[root@web01 bbb]# vim /etc/Nginx/conf.d/al.conf

server {
        listen 80;
        server_name rew.wc.com;
        root /code;
        index index.html;

        location /2018 {
                rewrite ^/2018/(.*) /2014/$1 redirect;
        }
}

# 创建以下站点目录
[root@web01 bbb]# mkdir /code/2014/ccc/bbb -p
[root@web01 bbb]# vim /2.html

#  重启Nginx
[root@web01 Nginx]# systemctl restart Nginx

案例三

# 用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html
[root@web01 33]# vim /etc/Nginx/conf.d/al.conf 

server {
        listen 80;
        server_name rew.wc.com;
        root /code;
        index index.html;

        location /course {
                rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
        }
}

# 创建站点目录
[root@web01 code]# mkdir /code/course/11/22/33 -p
[root@web01 code]# cd /code/course/11/22/33/
[root@web01 33]# vim course_33.html

# 重启Nginx
[root@web01 Nginx]# systemctl restart Nginx

案例四

# 80端口强制跳转443端口
[root@web01 33]# vim /etc/Nginx/conf.d/al.conf 

server {
        listen 80;
        server_name rew.wc.com;
        rewrite ^(.*) https://$server_name redirect;
}

wordpress使用rewrite做伪静态

# 在装好的wordpress上将配置文件修改
[root@web01 conf.d]# vim blog.wc.com.conf 

server{
        listen 80;
        server_name blog.wc.com;
        root /wc/wordpress;
        index index.PHP index.html;
        location ~ \.PHP$ {
        fastcgi_pass 127.0.0.1:9000;
#fastcgi_pass unix:/opt/PHP.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}
        location / {
                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;
        }

        }
}

# 重启Nginx
[root@web01 Nginx]# systemctl restart Nginx

discuz做伪静态

# 1.安装discuz
[root@web03 code]# wget http://test.driverzeng.com/Nginx_Code/discuz_X3.3_SC_GBK.zip

(详细操作见wordpress,照葫芦画瓢)


全绿即可







版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐