nginx实现rewrite重写
什么是rewrite
rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。<br>做伪静态,将动态页面url转换成静态页面的url
rewrite使用场景
- 地址跳转
- www.taobao.com 跳转成 main.m.taobao.com
- 协议跳转
- http://blog.51cto.com/baicia 跳转成 https://blog.51cto.com/baicia
- 伪静态
- 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态url地址对外暴露过多的参数,提升更高的安全性。
- 搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
伪静态的配置
## 官方语法:
句法:Syntax: rewrite regex replacement [flag]
默认:Default: --
语境:Context: server,location,if
#用于切换维护页面场景
#rewrite ^(.*)$ /page/maintain.html break;
rewrite:模块
regex:正则表达式(匹配当前的url)
replacement:要替换成的url
## 如果懂shell脚本,这两个类似于脚本中的,break和continue
rewrite的flag
flag | 概述 |
---|---|
last | 匹配到last的规则后,可以继续匹配后面的location |
break | 匹配到break的规则后,无法再匹配后面的location |
redirect | 302临时重定向 |
permanent | 301永久重定向 |
## redirect临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /test {
return 301 http://www.taobao.com;
}
}
## 重新加载nginx
[root@web01 ~]# systemctl reload nginx
## 域名解析
10.0.0.7 rewrite.wsh.com
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /test {
rewrite ^(.*)$ http://www.taobao.com redirect;
}
}
## 重新加载nginx
[root@web01 ~]# systemctl reload nginx
## permanent临时重定向配置
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /test {
return 301 http://www.taobao.com;
}
}
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /test {
rewrite ^(.*)$ http://www.taobao.com permanent;
}
}
rewrite实践
开启rewrite日志
## 开启rewrite日志,错误日志的要改成notice,在http层加上rewrite_log on;
[root@web01 ~]# vim /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log notice;
http {
rewrite_log on;
## 重启nginx
[root@web01 ~]# systemctl restart nginx
案例一
用户访问 /abc/1.html 实际上真实访问的是 /ccc/bbb/2.html
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /abc/1.html {
rewrite ^(.*)$ /ccc/bbb/2.html;
}
}
## 创建目录及html文件
[root@web01 ~]# mkdir -p /code/ccc/bbb
[root@web01 ~]# echo '欢迎光临' > /code/ccc/bbb/2.html
## 重启nginx
[root@web01 ~]# systemctl restart nginx
案例二
用户访问 /2018/ccc/2.html 实际上真实访问的是 /2014/ccc/2.html
## 用正则表达式
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /2018 {
rewrite ^/2018/(.*)$ /2014/$1 redirect;
}
}
## 创建目录及html文件
[root@web01 ~]# mkdir -p /code/2014/ccc
[root@web01 ~]# echo 'wwwwwwwwwwww' > /code/2014/ccc/2.html
## 重启nginx
[root@web01 ~]# systemctl restart nginx
案例三
用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html
[root@web01 ~]# vim /etc/nginx/conf.d/rewrite.conf
server {
listen 80;
server_name rewrite.wsh.com;
root /code;
index index.html;
location /course {
rewrite course-(.*)-(.*)-(.*).html /course/$1/$2/$3/coure_$3.html redirect;
}
}
## 创建目录及html文件
[root@web01 ~]# mkdir -p /code/course/11/22/33/
[root@web01 ~]# echo 'xxxxxxxxxx' > /code/course/11/22/33/course_33.html
## 重启nginx
[root@web01 ~]# systemctl restart nginx
案例四
80端口强制跳转443端口
server {
listen 80;
server_name www.dirverzeng.com;
rewrite ^(.*) https://$server_name redirect;
#return 302 https://$server_name$request_uri;
}
rewrite做伪静态
## 下载Discuz
[root@web01 ~]# rz
[root@web01 ~]# ls
anaconda-ks.cfg Discuz_X3.3_SC_GBK.zip host_ip.sh
## 创建目录
[root@web01 ~]# mkdir /discuz
## 解压
[root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /discuz/
[root@web01 ~]# ll /discuz/
total 4
drwxr-xr-x 2 root root 102 Jul 27 2017 readme
drwxr-xr-x 12 root root 4096 Jul 27 2017 upload
drwxr-xr-x 4 root root 72 Jul 27 2017 utility
## 安装php-fpm
[root@web01 ~]# mkdir /php
[root@web01 ~]# rz
[root@web01 php]# tar xf php.tgz
[root@web01 php]# yum localinstall *.rpm
## 修改配置文件
[root@web01 dev]# vim /etc/nginx/conf.d/diz.wsh.com.conf
server {
listen 80;
server_name diz.wsh.com;
root /discuz/upload;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 dev]# vim /etc/php-fpm.d/www.conf
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
## 启动和重启服务
[root@web01 dev]# systemctl restart nginx
[root@web01 dev]# systemctl start php-fpm.service
## 域名解析
10.0.0.7 diz.wsh.com
## 访问浏览器