静态和动态最大的区别是是否调用数据库。

什么是rewrite

将浏览器发送到服务器的请求重写,然后再返回给用户。

就是修改url,提高用户体验

rewrite的用途

  1. 80强转443 (优化用户体验)
  2. 匹配客户端规则,返回对应页面 (优化用户体验),电脑登陆淘宝为www.taobao.com 手机登陆是m.taobao.com
  3. 伪静态(便于做SEO)

什么是伪静态?

原本的动态页面,需要调用数据库,但是在浏览器中的url里面,返回的是一个静态页面,以html,css,js,shtml结尾。

为什么要做伪静态?

  1. 安装
  2. 为了百度的推广做SEO

rewrite使用

rewrite标记flage

flag 作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则 开发做
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则 开发做
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址
[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com;
root /website; location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}

break 只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;

而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

break请求:
1、请求rewrite.gong.com/break
2、首先:会去查找本地的/website/test/index.html;
3、如果找到了,则返回return的内容;
4、如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403 last请求:
1、请求rewrite.gong.com/last
2、首先:会去查找本地的/website/test/index.html;
3、如果找到了,则返回/website/test/index.html的内容;mv
4、如果没找到,会对当前server重新的发起一次请求,rewrite.gong.com/test/
5、如果有location匹配上,则直接返回该location的内容。
4、如果也没有location匹配,再返回404;

所以,在访问/break和/last请求时,虽然对应的请求目录/test都是不存在的,理论上都应该返回404,但是实际上请求/last的时候,是会有后面location所匹配到的结果返回的,原因在于此。

redirect和permanent的区别

[root@web01 /etc/nginx/conf.d]# vi blog_rewrite.conf
server {
listen 80;
server_name www.linux.com; location / {
root /website/dist;
index index.html;
} location /download {
rewrite ^(.*)$ http://download.linux.com redirect;
# return 302 "http://download.linux.com";
} location /friends {
rewrite ^(.*)$ http://friend.linux.com permanent;
# return 302 "http://friend.linux.com";
}
location /blog {
# rewrite ^(.*)$ http://blog.linux.com redirect;
return 302 "http://blog.linux.com";
}
}

访问下载站点的时候使用的redirect302 跳转

访问下载站点的时候使用的permanent301 跳转

结论

301 和 302 的不同,永久重定向在不清除本地缓存的情况下,服务端挂了,客户端也可以重定向。(.\*)和 ^(.\*)$ 的区别,前者匹配uri后者匹配整个url 。

rewrite案例

案例一

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

# 浏览器输入rewrite.gong.com/abc 的时候会自动跳转
[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com;
root /website;
index index.html; location /abc {
# (.*) 表示的是只替换uri部分
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
} # 2、创建站点
[root@web02 /website]# mkdir -p ccc/bbb
[root@web02 /website]# echo 123 > ccc/bbb/2.html

案例二

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

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com;
root /website;
index index.html; location /2018 { # 这种写法具有一定的可扩展性,引入了变量。
rewrite ^/2018/(.*)$ /2014/$1 redirect;
# 如果使用这种写法,在重定向目录过多的情况下显然不好使。
# rewrite (.*) /2014/ccc/bbb/2.html redirect;
}
} [root@web02 /website]# mkdir -p 2014/ccc/bbb/
[root@web02 /website]# echo 222 > 2014/ccc/bbb/2.html

案例三

用户访问/test实际上真实访问的是rewrite.gong.com

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com;
root /website;
index index.html; location /test {
rewrite (.*) http://rewrite.gong.com redirect;
}
}

这中就像是平时在访问网站的时候在后面输入index.html,会自动的变成一个域名。

案例四

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

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com;
root /website; location / {
# 这里需要引入变量,如果说在没有变量的情况下,每变一个uri就要配置一条rewrite就不适用了
rewrite ^/course-(.*)-(.*)-(.*).html /course/$1/$2/$3/course_$3.html redirect;
#固定配法
#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
}
} [root@web02 /website]# mkdir -p course/11/22/33/
[root@web02 /website]# echo course > course/11/22/33/course_33.html

案例五

http重定向https

[root@web02 /etc/nginx/conf.d]# vi rewrite.conf
server {
listen 80;
server_name rewrite.gong.com; location / {
rewrite ^(.*)$ https://rewrite.gong.com redirect;
}
}

浏览器输入rewrite.gong.com会自动的跳转到 https://rewrite.gong.com

搭建Discuss

环境

角色 IP 主机名
web服务器 10.0.0.8 web02
数据库服务器 10.0.0.51 db01
# 1、编辑配置文件
[root@web02 /etc/nginx/conf.d]# vi dis.conf
server {
listen 80;
server_name dis.gong.com;
root /website/upload;
index index.php; location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
} # 2、解压写代码
[root@web02 /website]# unzip Discuz_X3.3_SC_GBK.zip # 3、授权
[root@web02 ~]# chown www.www -R /website/ # 4、配置数据库服务器
[root@db01 ~]# mysql -uroot -p123 MariaDB [(none)]> create database dis; MariaDB [(none)]> grant all on dis.* to dis_user@'%' identified by '123'; # 5、重新加载nginx配置文件
[root@web02 ~]# nginx -s reload

把网页上自动生成的放到配置文件中。

root@web02 /etc/nginx/conf.d]# cat dis.conf
server {
listen 80;
server_name dis.gong.com;
root /website/upload;
index index.php; 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_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
}

nginx中的常用变量

Rewrite与Nginx全局变量

Rewrite在匹配过程中,会用到一些Nginx全局变量

$remote_addr        //获取客户端ip
$binary_remote_addr //客户端ip(二进制)
$remote_port //客户端port,如:50472
$remote_user //已经经过Auth Basic Module验证的用户名
$host //请求主机头字段,否则为服务器名称,如:blog.sakmon.com
$request //用户请求信息,如:GET ?a=1&b=2 HTTP/1.1
$request_filename //当前请求的文件的路径名,由root或alias和URI request组合而成,如:/2013/81.html
$status //请求的响应状态码,如:200
$body_bytes_sent // 响应时送出的body字节数数量。即使连接中断,这个数据也是精确的,如:40
$content_length // 等于请求行的“Content_Length”的值
$content_type // 等于请求行的“Content_Type”的值
$http_referer // 引用地址
$http_user_agent // 客户端agent信息,如:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
$args //与$query_string相同 等于当中URL的参数(GET),如a=1&b=2
$document_uri //与$uri相同 这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$document_root //针对当前请求的根路径设置值
$hostname //如:centos53.localdomain
$http_cookie //客户端cookie信息
$cookie_COOKIE //cookie COOKIE变量的值
$is_args //如果有$args参数,这个变量等于”?”,否则等于”",空值,如?
$limit_rate //这个变量可以限制连接速率,0表示不限速
$query_string // 与$args相同 等于当中URL的参数(GET),如a=1&b=2
$request_body // 记录POST过来的数据信息
$request_body_file //客户端请求主体信息的临时文件名
$request_method //客户端请求的动作,通常为GET或POST,如:GET
$request_uri //包含请求参数的原始URI,不包含主机名,如:/2013/81.html?a=1&b=2
$scheme //HTTP方法(如http,https),如:http
$uri //这个变量指当前的请求URI,不包括任何参数(见$args) 如:/2013/81.html
$request_completion //如果请求结束,设置为OK. 当请求未结束或如果该请求不是请求链串的最后一个时,为空(Empty),如:OK
$server_protocol //请求使用的协议,通常是HTTP/1.0或HTTP/1.1,如:HTTP/1.1
$server_addr //服务器IP地址,在完成一次系统调用后可以确定这个值
$server_name //服务器名称,如:blog.sakmon.com
$server_port //请求到达服务器的端口号,如:80
$server_name    # 当前用户请求的域名

server {
listen 80;
server_name test.drz.com;
# 重定向为https
rewrite ^(.*)$ https://$server_name$1;
}
$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)
$request_uri 当前请求的文件路径(不带网站的主目录/inages/test.jpg) #大多数用于http协议转gttps协议
server {
listen 80;
server_name php.drz.com;
# return的方式也可用作跳转。
return 302 https://$server_name$request_uri;
}
$scheme 用的协议,比如http或者https

rewrite匹配的优先级

匹配 优先级
server中的rewrite 1
location匹配 2
location中的rewrite 3

nginx rewite重定向详解及实例解析的更多相关文章

  1. Android开发之位置定位详解与实例解析(GPS定位、Google网络定位,BaiduLBS(SDK)定位)

    在android开发中地图和定位是很多软件不可或缺的内容,这些特色功能也给人们带来了很多方便.定位一般分为三种发方案:即GPS定位.Google网络定位以及基站定位 最简单的手机定位方式当然是通过GP ...

  2. Linux I/O 重定向详解及应用实例

    Linux I/O 重定向详解及应用实例 简解 > 输出 < 输入 >> 追加 & [> | < | >>]之前:输入输出; ls /dev & ...

  3. Nginx 安装与详解

    nginx简介 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件.它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用.nginx比它大哥apache性能改 ...

  4. httpd配置文件详解及实例

    httpd配置文件详解及实例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.http协议的组成 http协议是C/S架构:我们可以把浏览器(如:IE,Firefox,Safar ...

  5. Nginx配置配置文件详解

    文章目录 配置文件 nginx.conf配置文件详解 用于调试.定位问题的配置参数 正常运行必备的配置参数 优化性能的配置参数 事件相关配置 Fastcgi相关配置参数 常需要调整的参数 nginx作 ...

  6. Nginx配置文件nginx.conf中文详解(转)

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  7. Nginx配置文件中文详解

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  8. nginx.conf配置详解

    ######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...

  9. Nginx安全相关配置和nginx.conf中文详解

    一.centos下redis安全相关 1.背景 在使用云服务器时,如果我们的redis关闭了protected-mode模式,被病毒攻击的可能会大大增加,因此我们使用redis时候,最好更改默认端口, ...

随机推荐

  1. ATMS中去拉起新的进程,并在新进程启动后调用attachApplication时,resume待启动的Activity

    相关文章: ATMS中去pause Activity A. 目录 ATMS拉起新进程 堆栈 resumeTopActivityInnerLocked:1684, ActivityStack start ...

  2. 测开新手:从0到1,自动化测试接入Jenkins学习

    大家好,我叫董鑫,一个在测试开发道路上的新手,之前一直从事手工功能测试,前段时间抽空又温习了一遍老师全栈测开训练营中自动化测试.CICD的知识,最近公司正好有一个项目可以实践练手,趁热打铁,将自动化测 ...

  3. mariadb_2 单表的增删改查

    命令关键字: 创建表  create 删除表  drop 修改表的内容  update 修改表的结构  alter 删除表中内容 delete 增加表中内容 insert 查询表中内容 select ...

  4. Java多线程编程(同步、死锁、生产消费者问题)

    Java多线程编程(同步.死锁.生产消费): 关于线程同步以及死锁问题: 线程同步概念:是指若干个线程对象并行进行资源的访问时实现的资源处理保护操作: 线程死锁概念:是指两个线程都在等待对方先完成,造 ...

  5. 1-3 Postman 注册账号与登录

    1.为什么要注册postman账号 注册postman账号是免费的.当注册并登陆一个postman账号后,用户可以获得如下权限. (1)同步和备份历史,集合,环境,和预置头. (2)可以轻松的处理来自 ...

  6. 【ShardingSphere】ShardingSphere学习(二)-核心概念-SQL

    逻辑表 水平拆分的数据库(表)的相同逻辑和数据结构表的总称. 例:订单数据根据主键尾数拆分为10张表,分别是t_order_0到t_order_9,他们的逻辑表名为t_order. 真实表 在分片的数 ...

  7. hdu4530 水题

    题意: 小Q系列故事--大笨钟 Time Limit: 600/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total ...

  8. hdu4941 map交换行列

    题意:      有一个大矩阵,某些格子上有数字,然后有三种操作, 1 交换行 2 交换列 3 询问当前坐标数值 思路:      直接用map去映射行列,用二维的map去存数字就行了,水题,想不通的 ...

  9. 利用ICMP进行命令控制和隧道传输

    目录 使用ICMP进行命令控制 使用ICMP搭建隧道 使用ICMP进行命令控制 攻击机:Kali  192.168.10.11 靶机:Windows 192.168.10.1 使用的工具:icmpsh ...

  10. Windows 10 系统进入测试模式命令

    首先需要开启测试模式,内容如下: @bcdedit -set loadoptions DDISABLE_INTEGRITY_CHECKS @bcdedit -set TESTSIGNING ON 取消 ...