第十七章 nginx动静分离和rewrite重写
一、动静分离
- 动静分离,通过中间件将动静分离和静态请求进行分离;
通过中间件将动态请求和静态请求分离,可以减少不必要的请求消耗,同时能减少请求的延时。
通过中间件将动态请求和静态请求分离,逻辑图如下:
1.单台机器动静分离
- #配置
[root@web01 /code]# cat /etc/nginx/conf.d/linux.blog.com.conf
server {
listen 80;
server_name linux.blog.com;
root /code/wordpress;
location / {
index index.php;
}
location ~* \.(jpg|png)$ {
root /code/pic;
root /code/wordpress;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#创建目录
[root@web01 /code]# mkdir /code/pic
#做软连接
[root@web01 /code]# ln -s /code/wordpress/wp-content /code/pic
2.多台机器做动静分离
1)环境准备
主机 | IP | 身份 |
---|---|---|
lb01 | 10.0.0.4,172.16.1.4 | 负载均衡 |
web01 | 172.16.1.7 | 静态资源 |
web03 | 172.16.1.9 | 动态资源 |
2)web01配置静态资源
- [root@web01 /code]# vim /etc/nginx/conf.d/jt.conf
server {
listen 80;
server_name linux.djfenli.com;
location ~* \.(jpg|png|gif)$ {
root /code/pic;
}
}
#重启
[root@web01 /code]# systemctl restart nginx
#上传一些图片
[root@web01 /code]# mkdir pic
[root@web01 /code/pic]# ll
total 1188
-rw-r--r-- 1 root root 407030 Sep 2 12:22 1.gif
-rw-r--r-- 1 root root 298866 Sep 2 12:21 3_web01.jpg
-rw-r--r-- 1 root root 410120 Sep 2 12:22 4_web02.jpg
-rw-r--r-- 1 root root 60494 Sep 2 12:21 timg_(2).jpg
-rw-r--r-- 1 root root 30607 Sep 2 12:21 timg_(3).jpg
3)web03配置动态资源
- #部署tomcat
[root@web03 ~]# yum install -y tomcat
[root@web03 ~]# mkdir /usr/share/tomcat/webapps/ROOT
[root@web03 ~]# cat /usr/share/tomcat/webapps/ROOT/java_test.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<HTML>
<HEAD>
<TITLE>曾老湿JSP Page</TITLE>
</HEAD>
<BODY>
<%
Random rand = new Random();
out.println("<h1>曾老湿随机数:<h1>");
out.println(rand.nextInt(99)+100);
%>
</BODY>
</HTML>
[root@web03 ~]# systemctl start tomcat
[root@web03 ~]# netstat -lntp
tcp6 0 0 :::8009 :::* LISTEN 34369/java
tcp6 0 0 :::8080 :::* LISTEN 34369/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 34369/java
4)配置负载均衡
- [root@lb01 ~]# cat /etc/nginx/conf.d/linux.djfenli.com.conf
upstream jt {
server 172.16.1.7:80;
}
upstream dt {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name linux.djfenli.com;
location ~* \.gif$ {
proxy_pass http://jt;
include proxy_params;
}
location ~* \.jsp$ {
proxy_pass http://dt;
include proxy_params;
}
}
5)检查配置文件并重启
- [root@lb01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 ~]# systemctl restart nginx
6)配置hosts访问
- 10.0.0.4 linux.djfenli.com
二、nginx资源分离
1.资源分离
- Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例
2.环境准备
主机 | IP | 资源 | 端口 |
---|---|---|---|
lb01 | 10.0.0.4 | 负载均衡 | 80 |
web01 | 172.16.1.7 | Android的页面 | 8081 |
web01 | 172.16.1.7 | iphone的页面 | 8082 |
web01 | 172.16.1.7 | pc的页面 | 8083 |
3.配置web服务器
- [root@web01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf
[root@web01 ~]# cat /etc/nginx/conf.d/linux.ziyuan.com.conf
server {
listen 8081;
server_name linux.ziyuan.com;
location / {
root /code/android;
index index.html;
}
}
server {
listen 8082;
server_name linux.ziyuan.com;
location / {
root /code/iphone;
index index.html;
}
}
server {
listen 8083;
server_name linux.ziyuan.com;
location / {
root /code/pc;
index index.html;
}
}
[root@web01 ~]# systemctl restart nginx
4.配置站点目录
- [root@web01 ~]# mkdir /code/{android,iphone,pc}
[root@web01 ~]# echo "我是pc" > /code/pc/index.html
[root@web01 ~]# echo "我是iphone" > /code/iphone/index.html
[root@web01 ~]# echo "我是android" > /code/android/index.html
[root@web01 ~]# chown -R www:www /code/
5.配置负载均衡
- [root@lb01 ~]# vim /etc/nginx/conf.d/linux.ziyuan.com.conf
upstream Android {
server 172.16.1.7:8081;
server 172.16.1.8:8081;
}
server {
listen 80;
server_name linux.ziyuan.com;
location / {
if ($http_user_agent ~* "Windows") {
proxy_pass http://172.16.1.7:8083;
}
if ($http_user_agent ~* "iPhone") {
proxy_pass http://172.16.1.7:8082;
}
if ($http_user_agent ~* "Android") {
proxy_pass http://Android;
}
return 500;
}
}
6.配置hosts访问测试
- 10.0.0.4 linux.ziyuan.com
三、nginx的rewrite重写
1.什么是rewrite?
- Rewrite主要实现url地址重写,以及重定向,就是把传入`web`的请求重定向到其他`url`的过程。
2.rewrite使用场景
- 1.地址跳转,用户访问www.baidu.com这个URL时,将其定向至一个新的域名mobile.baidu.com
2.协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3.伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时建上动态URL地址对外暴露过多的参数,提升更高的安全性。
4.搜索引擎,SEO优化依赖于url路径,好记的url便于智齿搜索引擎录入
3.rewrite语法
- Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if
rewrite #调用模块
regex #请求的链接(可以使用正则表达式)
replacement #跳转的链接
[flag]; #标签
#示例
server {
...
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
...
}
4.rewrite的flag标记
- rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记有如下表格所示:
flag | 作用 |
---|---|
last | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
break | 本条规则匹配完成后,停止匹配,不再匹配后面的规则 |
redirect | 返回302临时重定向,地址栏会显示跳转后的地址 |
permanent | 返回301永久重定向,地址栏会显示跳转后的地址 |
5.last和break的区别
- 1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
server {
listen 80;
server_name linux.rewrite.com;
root /code;
location ~ ^/break {
rewrite ^/break /test/ break;
}
location ~ ^/last {
rewrite ^/last /test/ last;
}
location /test/ {
default_type application/json;
return 200 "ok";
}
}
2.重启
[root@web01 ~]# nginx -t
[root@web01 ~]# systemctl restart nginx
3.配置hosts测试
10.0.0.7 linux.rewrite.com
#结果
1)访问 linux.rewrite.com/break,返回结果404
2)访问 linux.rewrite.com/last,返回结果ok
4.结论
break只要匹配到规则,则会去本地配置路径的目录中寻找请求的文件;
而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。
break请求:
1)请求 linux.rewrite.com/break
2)匹配 location ~ ^/break 会跳转请求 到 linux.rewrite.com/test
3)请求跳转后,会去查找本地的站点目录下的 test/index.html;
4)如果找到了,则返回站点目录下的 test/index.html的内容;
5)如果没找到该目录则报错404,如果找到该目录没找到对应的文件则403
last请求:
1)请求 rewrite.drz.com/last
2)匹配 location ~ ^/last 会跳转请求 到 linux.rewrite.com/test
2)请求跳转后,会去查找本地的站点目录下的 test/index.html;
3)如果找到了,则返回站点目录下的 test/index.html的内容;
4)如果没找到,会对当前server重新的发起一次请求,linux.rewrite.com/test/
5)如果有location匹配上,则直接返回该location的内容。
4)如果也没有location匹配,再返回404;
6.redirect和permanent的区别
- 1.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/linux.rw.com.conf
server {
listen 80;
server_name linux.rw.com;
root /code;
location /test {
#rewrite ^(.*)$ https://www.baidu.com redirect;
#rewrite ^(.*)$ https://www.baidu.com permanent;
#return 301 https://www.baidu.com;
return 302 https://www.baidu.com;
}
}
2.配置hosts测试
#配置redirect时
关闭nginx之后访问失败
#配置permanent时
关闭nginx仍然访问成功
#结论:
redirect,每次访问服务器都会进行询问,是否进行跳转
permanent,记录一次跳转,以后都不会询问,直接跳转页面,除非清空缓存
四、rewrite实践
1.案例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html
- 1.创建页面
[root@web01 ~]# mkdir /code/ccc/bbb/ -p
[root@web01 ~]# echo "/ccc/bbb/2.html" > /code/ccc/bbb/2.html
2.配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* /abc {
rewrite ^(.*)$ /ccc/bbb/2.html redirect;
}
}
2.案例二:用户访问/2018/ccc/bbb/2.html
实际上真实访问的是/2014/bbb/ccc/2.html
- [root@web01 ~]# mkdir /code/2014/bbb/ccc/ -p
[root@web01 ~]# echo "/2014/bbb/ccc/2.html" > /code/2014/bbb/ccc/2.html
#配置nginx
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* ^/2018 {
rewrite ^/2018/(.*)/(.*)/(.*) /2014/$2/$1/$3 redirect;
}
}
3.案例三:用户访问course-11-22-33.html
实际上真实访问的是/course/11/22/33/course_33.html
- [root@web01 ~]# mkdir /code/course/11/22/33/ -p
[root@web01 ~]# echo "/course/11/22/33/course_33.html" >/code/course/11/22/33/course_33.html
#配置
[root@web01 ~]# vim /etc/nginx/conf.d/rw.conf
server {
listen 80;
server_name nginx.rewrite.com;
root /code;
location ~* ^/course {
#灵活配法
rewrite ^/(.*)-(.*)-(.*)-(.*).html$ /$1/$2/$3/$4/$1_$4.html redirect;
#固定配法
#rewrite ^/course-(.*) /course/11/22/33/course_33.html redirect;
}
}
4.案例四:将http
请求跳转到https
- #Nginx跳转配置
server {
listen 80;
server_name www.baidu.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}
server {
listen 443;
server_name www.baidu.com;
ssl on;
}
浏览器输入:baidu.com
浏览器转换:http://www.baidu.com/index.html
server层转换,rewrite跳转:https://www.baidu.com/index.html
五、rewrite实现伪静态
1.搭建discuz论坛
- 1.创建站点目录
[root@web01 /code]# mkdir discuz
[root@web01 /code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
#授权
[root@web01 /code]# chown -R www.www /code/discuz/
2.配置nginx配置文件
[root@web01 /code]# vim /etc/nginx/conf.d/linux.discuz.com.conf
server {
listen 80;
server_name linux.discuz.com;
root /code/discuz/upload;
location / {
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;
}
}
3.重启访问
[root@web01 /code]# nginx -t
[root@web01 /code]# systemctl restart nginx
#配置hosts
10.0.0.7 nginx.rewrite.com linux.discuz.com
4.创建数据库
[root@db01 ~]# mysql -uroot -pLinhd@123
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on discuz.* to discuz@'172.16.1.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
5.配置伪静态
[root@web01 /code]# cat /etc/nginx/conf.d/linux.discuz.com.conf
server {
listen 80;
server_name linux.discuz.com;
root /code/discuz/upload;
location / {
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) {
return 404;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
http://linux.discuz.com/forum-2-1.html
http://linux.discuz.com/forum.php?mod=forumdisplay&fid=2&page=1
六、rewrite补充
1.rewrite匹配优先级
- 1.匹配优先级
1)先执行server层的rewrite
2)再根据location匹配优先级匹配
3)再执行location下的rewrite
4)最后再执行location下if配置的rewrite
2.配置
[root@web01 /code]# vim /etc/nginx/conf.d/youxianji.conf
server {
listen 80;
server_name linux.youxian.com;
location / {
rewrite (.*) http://www.jd.com;
}
location =/ {
rewrite (.*) http://www.taobao.com;
}
rewrite (.*) http://www.baidu.com;
}
2.rewrite的环境变量
- 1.$server_name
$server_name #当前请求的域名
server {
listen 80;
server_name test.linux.com;
rewrite ^(.*)$ https://$server_name$1;
}
http://test.linux.com/1.html
https://test.linux.com/1.html
2.请求文件
$request_filename #请求的文件路径名(带网站的站点目录 /code/images/test.jpg)
$request_uri #当前请求的文件路径(不带网站的站点目录 /images/test.jpg)
#大多数用于http协议转https协议
server {
listen 80;
server_name test.linux.com;
root /code;
return 302 https://$server_name$request_uri;
}
URL: http://test.linux.com/images/test.jpg
URI: /images/test.jpg
3.$scheme
$scheme 用的协议,比如http或者https
4.$http_host
#很久之前的配置方法
server {
listen 80;
server_name www.baidu.com baidu.com;
if ($http_host = baidu.com){
rewrite (.*) http://www.baidu.com$1;
}
}
#推荐书写格式
server {
listen 80;
server_name baidu.com;
return 302 http://www.baidu.com$request_uri;
}
server {
listen 80;
server_name www.drz.com;
}
3.rewrite开启日志
- #配置
[root@web01 /code]# vim /etc/nginx/nginx.conf
... ...
error_log /var/log/nginx/error.log notice;
... ...
http {
... ...
rewrite_log on;
... ...
}
第十七章 nginx动静分离和rewrite重写的更多相关文章
- Nginx系列二:(Nginx Rewrite 规则、Nginx 防盗链、Nginx 动静分离、Nginx+keepalived 实现高可用)
一.Nginx Rewrite 规则 1. Nginx rewrite规则 Rewrite规则含义就是某个URL重写成特定的URL(类似于Redirect),从某种意义上说为了美观或者对搜索引擎友好, ...
- Nginx动静分离经典
Nginx:安装nginx之前需要安装pcre包和zlib以支持重写,正则以及网页压缩等等]把所需的包下载到/usr/src下[根据自己的习惯,路径可以改变]1.首先安装pcre: cd /usr/s ...
- Nginx动静分离知识及配置
Nginx动静分离知识及配置,本质上与分离上传下载功能是相同功能,本质就是在文件服务器也是需要部署WEB应用的,只是在进行代理分离的时候分配到对应的文件服务器上去. 随着Nginx高性能Web服务器大 ...
- Nginx动静分离经典案例配置
随着Nginx高性能Web服务器大量被使用,目前Nginx最新稳定版为1.2.6,张宴兄在实际应用中大量使用Nginx,并分享Nginx高性能Web服务器知识,使得Nginx在国内也是飞速的发展.那今 ...
- Nginx动静分离基本概述
Nginx动静分离基本概述 动静分离,通过中间将动静分离和静态请求进行分离: 通过中间件将动态请求和静态请求分离,可以建上不必要的请求消耗,同时能减少请求的延时. 通过中间件将动态请求和静态请求分离, ...
- Nginx动静分离实现
Nginx动静分离实现: Nginx是一种轻量级,高性能,多进程的Web服务器,非常适合作为静态资源的服务器使用,而动态的访问操作可以使用稳定的Apache.Tomcat及IIS等来实现,这里就以Ng ...
- Nginx动静分离架构
Nginx动静分离简单来说就将动态与静态资源分开,不能理解成只是单纯的把动态页面和静态页面物理分离,严格意义上说应该是动态请求跟静态请求分开,可以理解成使用Nginx处理静态页面,Tomcat,Res ...
- Nginx 动静分离与负载均衡的实现
一.前提 企业中,随着用户的增长,数据量也几乎成几何增长,数据越来越大,随之也就出现了各种应用的瓶颈问题. 问题出现了,我们就得想办法解决,一般网站环境,均会使用LAMP或者LNMP,而我们对于网站环 ...
- Nginx动静分离
动静分离 Nginx动静分离是让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作,这就是网站静态化处理的核心思路. ...
随机推荐
- 【深入理解Linux内核架构】第3章:内存管理
3.1 概述 内存管理涵盖了许多领域: 内存中物理内存页的管理: 分配大块内存的伙伴系统: 分配小块内存的slab.slub.slob分配器: 分配非连续内存块的vmalloc机制: 进程的地址空间. ...
- 《方法总结》C路的方法发现
C语言方法荟萃 定义一个最大值和最小值:#define max(x,y) ( x>y?x:y ) #define min(x,y) ( x<y?x:y ) &&: 说得 ...
- Mybatis 注解形式
1.查询 // 查询 @Select("select id, name, type, numbers, cancelled, completed, percentage from c ...
- T和Class以及Class的理解
转载自https://blog.csdn.net/witewater/article/details/53462385 首先看下Class类 ,普通的非泛型类Class. 注意:class是java的 ...
- Win10使用VMWare15安装Ubuntu-18.04.2-desktop-amd64
本文在Win10系统中使用VMWare Workstation Pro 15.1.0虚拟机安装Ubuntu-18.04.2-desktop-amd64.iso系统,同时安装VMWare Tools(实 ...
- AI小白必读:深度学习、迁移学习、强化学习别再傻傻分不清
摘要:诸多关于人工智能的流行词汇萦绕在我们耳边,比如深度学习 (Deep Learning).强化学习 (Reinforcement Learning).迁移学习 (Transfer Learning ...
- hadoop分布式格式化时出现异常java.net.unknownhostexception
当搭建好分布式集群后,准备使用命令格式化时 hdfs namenode format 在日志的最后一行出现 java.net.unknownhostexception的异常,通常是你的主机名没有配置好 ...
- 高德AR & 车道级导航技术演进与实践
2020云栖大会于9月17日-18日在线上举行,阿里巴巴高德地图携手合作伙伴精心组织了"智慧出行"专场,为大家分享高德地图在打造基于DT+AI和全面上云架构下的新一代出行生活服务平 ...
- 智慧矿山-选矿工艺数字 3D 可视化
前言 现代科技和工业的发展对矿物原料的要求越来越高,直接开采的原矿石往往达不到标准,而原矿通过选矿加工后则可以满足要求.选矿技术在冶金.煤炭.化工.建材和环保等部门都得到应用,对国民经济的发展意义重大 ...
- springboot+websocket实现简单的在线聊天功能
效果如下: java实现逻辑: 1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId&g ...