一、动静分离
1.单台动静分离
[root@web01 ~]# 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/wordpress;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2.多台机器动静分离
[root@lb01 ~]# cat /etc/nginx/conf.d/linux.djfenli.com.conf
upstream jt {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
upstream dt {
server 172.16.1.9:8080;
}
server {
listen 80;
server_name linux.djfenli.com;
location ~* \.(gif|png|jpg)$ {
proxy_pass http://jt;
include proxy_params;
}
location ~* \.jsp$ {
proxy_pass http://dt;
include proxy_params;
}
}
3.合并动静数据页面
1)配置负载均衡nginx
[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;
root /code/dj;
index index.html;
location ~* \.gif$ {
proxy_pass http://jt;
include proxy_params;
}
location ~* \.jsp$ {
proxy_pass http://dt;
include proxy_params;
}
}
2)配置站点目录
[root@lb01 ~]# mkdir /code/dj
[root@lb01 ~]# cat /code/dj/index.html
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试ajax和跨域访问</title>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
</head>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://linux.djfenli.com/java_test.jsp",
success: function(data){
$("#get_data").html(data)
},
error: function() {
alert("哎呦喂,失败了,回去检查你服务去~");
}
});
});
</script>
<body>
<h1>测试动静分离</h1>
<img src="http://linux.djfenli.com/1.gif">
<div id="get_data"></div>
</body>
</html>
3)重启访问
[root@lb01 ~]# systemctl restart nginx
#关闭静态资源或者动态资源,互不影响
二、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 nginx.nginx /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.配置host访问测试
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