0
点赞
收藏
分享

微信扫一扫

Nginx实现Rewrite重写

夏木之下 2022-06-22 阅读 65

Nginx实现Rewrite重写

什么是Rewrite

Rewrite主要实现url地址重写,以及重定向,就是把传入web的请求重定向到其他url的过程。

做伪静态:将动态页面url转换成静态页面url.

Rewrite使用场景

  • 地址跳转
  • www.taobao.com跳转成:main.m.taobao.com
  • 协议跳转
  • ​​http://blog.driverzeng.com跳转成:https://blog.driverzeng.com​​
  • 伪静态
  • 将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态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

匹配到last的规则后可以继续匹配到后面的location

break

匹配到break的规则后,无法再匹配后面的location

redirect

302临时重定向

permanent

301永久重定向

# 1.redirect临时重定向配置
[root@web01 conf.d]# vim rewrite.conf
server {
listen 80;
server_name rewrite.jin.com;
root /code;
index index.html;

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

## 第二种写法
[root@web01 conf.d]# vim rewrite.conf
server {
listen 80;
server_name rewrite.jin.com;
root /code;
index index.html;

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

# 2.重新加载nginx
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl reload nginx

# 3.域名解析
10.0.0.7 rewrite.jin.com

# 4.访问rewrite.jin.com/test

Nginx实现Rewrite重写_nginx

# 1.permanent永久重定向配置
[root@web01 conf.d]# vim rewrite_permanent.conf
server {
listen 80;
server_name permanent.jin.com;
root /code;
index index.html;

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

##第二种写法

[root@web01 conf.d]# vim rewrite_permanent.conf
server {
listen 80;
server_name permanent.jin.com;
root /code;
index index.html;

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

# 2.重新加载nginx
[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 conf.d]# systemctl reload nginx

# 3.域名解析
10.0.0.7 permanent.jin.com

# 4.访问permanent.jin.com/test

# 5.关闭nginx
[root@web01 conf.d]# systemctl stop nginx

# 6.在访问permanent.jin.com/test

Nginx实现Rewrite重写_nginx_02

rewrite实践

开启rewrite日志

#  开启rewrite日志,错误日志的级别要改成 notice ,在http层加上rewrite_log on;
[root@web01 conf.d]# vim /etc/nginx/nginx.conf
error_log /var/log/nginx/error.log notice;

http {
rewrite_log on;
...
}

# 重启nginx
[root@web01 conf.d]# systemctl reload nginx

案例一

用户访问/abc/1.html 实际上真实访问的是/ccc/bbb/2.html

# 1.添加rewrite配置文件
[root@web01 conf.d]# vim rewrite_1.conf
server {
listen 80;
server_name rewrite_1.jin.com;
root /code;
index index.html;

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

# 2.创建站点目录
[root@web01 conf.d]# mkdir /code/ccc/bbb/ -p

# 3.编写2.html代码
[root@web01 conf.d]# echo '/ccc/bbb/' > /code/ccc/bbb/2.html

# 4.重启nginx
[root@web01 conf.d]# systemctl reload nginx

# 5.域名解析
10.0.0.7 rewrite_1.jin.com

# 6.浏览器访问:rewrite_1.jin.com/abc

Nginx实现Rewrite重写_html_03

案例二

用户访问/2018/ccc/2.html 实际上真实访问/2014/ccc/bbb/2.html

# 1.添加rewrite配置文件(正则后向引用匹配)
[root@web01 conf.d]# vim rewrite_2.conf
server {
listen 80;
server_name rewrite_2.jin.com;
root /code;
index index.html;

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

# 2.创建目录
[root@web01 conf.d]# mkdir /code/2014/ccc/bbb -p

# 3.编写2.html代码
[root@web01 conf.d]# echo '2014/ccc/bbb/2.html' > /code/2014/ccc/bbb/2.html

# 4.重新加载nginx
[root@web01 conf.d]# systemctl reload nginx

# 5.域名解析
10.0.0.7 rewrite_2.jin.com

# 6.浏览器访问rewrite_2.jin.com/2018/ccc/2.html

Nginx实现Rewrite重写_html_04

案例三

用户访问course-11-22-33.html实际上真实访问的是/course/11/22/33/course_33.html

# 1.添加rewrite配置文件
[root@web01 conf.d]# vim rewrite_3.conf

server {
listen 80;
server_name rewrite_3.jin.com;
root /code;
index index.html;

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

# 2.创建站点目录
[root@web01 conf.d]# mkdir /code/course/11/22/33/ -p

# 3.编写course_33.html代码
[root@web01 33]# echo 'course/11/22/33/course_33.html' > /code/course/11/22/33/course_33.html

# 4.重新加载nginx
[root@web01 33]# systemctl reload nginx

# 5.域名解析
10.0.0.7 rewrite_2.jin.com rewrite_3.jin.com

# 6.浏览器访问:rewrite_33.jin.com/course-11-22-33.htm

Nginx实现Rewrite重写_nginx_05

案例四

80端口强制跳转443端口

[root@web01 conf.d]# vim 80_443.conf
server {
listen 80;
server_name rewrite_80_443.jin.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}

rewrite做伪静态(discuz)

部署php

# 1.添加php第三方源
[root@web01 <sub>]# vim /etc/yum.repos.d/php.repo
[php-webtatic]
name = PHP Repository
baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
gpgcheck = 0

# 2.安装php
[root@web01 </sub>]# yum -y install php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

# 3.创建用户
[root@web01 <sub>]# groupadd www -g 666
[root@web01 </sub>]# useradd www -u 666 -g 666 -M -s /sbin/nologin

# 4.修改nginx运行用户
[root@web01 <sub>]# vim /etc/nginx/nginx.conf
user www;

# 5.启动php并加入开机自启
[root@web01 </sub>]# systemctl start php-fpm
[root@web01 ~]# systemctl enable php-fpm

部署discue

# 1.下载discue
[root@web01 app]# wget http://test.driverzeng.com/Nginx_Code/Discuz_X3.3_SC_GBK.zip

# 2.解压
[root@web01 app]# tar xf Discuz_X3.3_SC_GBK.zip

# 3.编辑nginx配置文件
[root@web01 app]# vim /etc/nginx/conf.d/discuz.conf
server {
listen 80;
server_name discue.jin.com;
root /app/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 /etc/nginx/fastcgi_params;
}

}

# 4.检查nginx语法和重新加载
[root@web01 app]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 app]# systemctl reload nginx

# 5.给站点目录授权
[root@web01 app]# chown -R www:www /app/

# 6.域名解析
10.0.0.7 discue.jin.com

# 7.浏览器访问:discue.jin.com

Nginx实现Rewrite重写_php_06

Nginx实现Rewrite重写_nginx_07

Nginx实现Rewrite重写_html_08

Nginx实现Rewrite重写_nginx_09

Nginx实现Rewrite重写_html_10

Nginx实现Rewrite重写_php_11

Nginx实现Rewrite重写_html_12

## 修改nginx配置文件
[root@web01 conf.d]# vim discuz.jin.com.conf

server {
listen 80;
server_name discuz.jin.com;

location / {
root /app/upload;
index index.php index.html;
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) {

}
}

location ~ \.php$ {
root /app/upload;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

## 重新加载nginx
[root@web01 conf.d]# systemctl reload nginx

Nginx实现Rewrite重写_nginx_13

rewrite做伪静态(wordprass)

## 修改nginx配置文件
[root@web01 conf.d]# vim /etc/nginx/conf.d/blog.jin.com.conf
server {
listen 80;
server_name blog.jin.com;
root /blog/wordpress;
index index.php index.html;

location / {
root /blog/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 ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}

}

Nginx实现Rewrite重写_php_14

举报

相关推荐

0 条评论