经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Linux/Shell » 查看文章
Linux动静分离与Rewrite
来源:cnblogs  作者:JZEason  时间:2022/1/17 10:58:32  对本文有异议

一、动静分离

1.1 单台机器动静分离

img

  1. 1、创建NFS挂载点(NFS服务端)
  2. mkdir /static
  3. vim /etc/exports
  4. /static 172.16.1.0/20(rw,sync,all_squash,anonuid=666,anongid=666)
  5. systemctl restart nfs-server
  6. chown -R www.www /static/
  7. 2、挂载到lb(LB服务端)
  8. yum install nfs-utils -y
  9. mount -t nfs 172.16.1.31:/static /opt/static/
  10. 3、将静态资源放置于挂载点内(web01)
  11. mkdir /opt/static/s
  12. [root@web01 static]# cp -r /opt/bbs/static/* /opt/static/s/
  13. 4、测试

1.2 多台机器动静分离

img

  1. 准备环境

    主机 作用 服务 IP
    lb01 负载均衡 nginx 172.16.1.5
    web01 静态资源 nginx 172.16.1.7
    web02 动态资源 tomcat 172.16.1.8
  2. 配置web01的静态资源

    1. 1.配置nginx
    2. [root@web01 ~]# vim /etc/nginx/conf.d/linux.dj.com.conf
    3. server {
    4. listen 80;
    5. server_name linux.dj.com;
    6. location ~* \.(jpg|png|mp4|gif)$ {
    7. root /code/picture;
    8. }
    9. }
    10. [root@web01 ~]# systemctl restart nginx
    11. 2.上传静态资源
    12. [root@web01 ~]# mkdir /code/picture
    13. [root@web01 ~]# cd /code/picture/
    14. [root@web01 picture]# rz 1.jpg
    15. [root@web01 picture]# ll
    16. total 1756
    17. -rw-r--r--. 1 root root 156617 Dec 7 08:54 1.jpg
    18. -rw-r--r--. 1 root root 47542 Dec 7 08:54 2.jpg
    19. -rw-r--r--. 1 root root 1586108 Dec 7 08:54 3.jpg
    20. 3.测试静态资源
    21. 1)配置hosts
    22. 172.16.1.7 linux.dj.com
    23. 2)请求静态资源
    24. http://linux.dj.com/1.jpg
  3. 配置web02的动态资源

    1. 1.安装tomcat
    2. [root@web02 ~]# yum install -y tomcat
    3. 2.配置动态资源
    4. [root@web02 ~]# cd /usr/share/tomcat/webapps
    5. [root@web02 webapps]# mkdir ROOT
    6. [root@web02 webapps]# vim ROOT/java_test.jsp
    7. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    8. <HTML>
    9. <HEAD>
    10. <TITLE>测试动态资源</TITLE>
    11. </HEAD>
    12. <BODY>
    13. <%
    14. Random rand = new Random();
    15. out.println("<h1>随机数:<h1>");
    16. out.println(rand.nextInt(99)+100);
    17. %>
    18. </BODY>
    19. </HTML>
    20. 3.启动方式
    21. [root@web02 ~]# systemctl start tomcat
    22. 4.访问测试动态页面
    23. 1)配置hosts
    24. 172.16.1.8 linux.dj.com
    25. 2)访问
    26. http://linux.dj.com:8080/java_test.jsp
  4. 配置LoadBalance负载均衡

    1. 1.配置
    2. [root@lb01 ~]# vim /etc/nginx/conf.d/linux.dj.com.conf
    3. upstream dt {
    4. server 172.16.1.8:8080;
    5. }
    6. upstream jt {
    7. server 172.16.1.7;
    8. }
    9. server {
    10. listen 80;
    11. server_name linux.dj.com;
    12. location / {
    13. root /code/dj;
    14. index index.html;
    15. }
    16. location ~* \.(jpg|png|gif)$ {
    17. proxy_pass http://jt;
    18. include proxy_params;
    19. }
    20. location ~* \.(php|jsp)$ {
    21. proxy_pass http://dt;
    22. include proxy_params;
    23. }
    24. }
    25. 2.重启
    26. [root@lb01 ~]# systemctl restart nginx
    27. 3.访问测试
    28. 1)配置hosts
    29. 172.16.1.5 linux.dj.com
    30. 2)访问
    31. http://linux.dj.com/java_test.jsp
    32. http://linux.dj.com/1.jpg
  5. 整合LoadBalance静态资源和动态资源

    1. 1.创建站点目录
    2. [root@lb01 ~]# mkdir -p /code/dj
    3. 2.编辑html文件
    4. [root@lb01 ~]# vim /code/dj/index.html
    5. <html lang="en">
    6. <head>
    7. <meta charset="UTF-8" />
    8. <title>测试ajax和跨域访问</title>
    9. <script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script>
    10. </head>
    11. <script type="text/javascript">
    12. $(document).ready(function(){
    13. $.ajax({
    14. type: "GET",
    15. url: "http://linux.dj.com/java_test.jsp",
    16. success: function(data){
    17. $("#get_data").html(data)
    18. },
    19. error: function() {
    20. alert("哎呦喂,失败了,回去检查你服务去~");
    21. }
    22. });
    23. });
    24. </script>
    25. <body>
    26. <h1>测试动静分离</h1>
    27. <img src="http://linux.dj.com/1.jpg">
    28. <div id="get_data"></div>
    29. </body>
    30. </html>
    31. 3.授权
    32. [root@lb01 ~]# chown -R www.www /code/
    33. 4.访问域名测试
    34. http://linux.dj.com/
    35. 结论:静态资源出现问题不影响动态资源,动态资源出问题不影响静态资源

二、Nginx资源分离

2.1 准备环境

主机 IP 功能
lb01 172.16.1.5 负载均衡
web01 172.16.1.7 Android页面
web02 172.16.1.8 iPhone页面
web03 172.16.1.9 PC端页面

2.2 配置web01服务器

  1. 配置Nginx

    1. [root@web01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.sj.com;
    5. charset utf8;
    6. location / {
    7. root /code/android;
    8. index index.html;
    9. }
    10. }
    11. [root@web01 ~]# systemctl restart nginx
  2. 创建站点目录

    1. [root@web01 ~]# mkdir /code/android
    2. [root@web01 ~]# echo "我是android" >> /code/android/index.html
    3. [root@web01 ~]# chown -R www.www /code/android/
  3. 访问测试

    1. 1.配置hosts
    2. 172.16.1.7 linux.sj.com

2.3 配置web02服务器

  1. 配置Nginx

    1. [root@web02 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.sj.com;
    5. charset utf8;
    6. location / {
    7. root /code/iphone;
    8. index index.html;
    9. }
    10. }
  2. 创建站点目录

    1. [root@web02 ~]# mkdir /code/iphone
    2. [root@web02 ~]# echo "我是Iphone" >> /code/iphone/index.html
    3. [root@web02 ~]# chown -R www.www /code/iphone/
  3. 访问测试

    1. 1.配置hosts
    2. 172.16.1.8 linux.sj.com

2.4 配置web03服务器

  1. 配置Nginx

    1. [root@web03 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.sj.com;
    5. charset utf8;
    6. location / {
    7. root /code/pc;
    8. index index.html;
    9. }
    10. }
    11. [root@web02 ~]# systemctl restart nginx
  2. 创建站点文件

    1. [root@web03 ~]# mkdir /code/pc -p
    2. [root@web03 ~]# echo "我是pc端" >> /code/pc/index.html
    3. [root@web03 ~]# chown -R www.www /code/pc/
  3. 访问测试

    1. 1.配置hosts
    2. 172.16.1.9 linux.sj.com

2.5 配置LoadBalance负载均衡

  1. 配置Nginx

    1. [root@lb01 ~]# vim /etc/nginx/conf.d/linux.sj.com.conf
    2. upstream android {
    3. server 10.0.0.7;
    4. }
    5. upstream iphone {
    6. server 10.0.0.8;
    7. }
    8. upstream pc {
    9. server 10.0.0.9;
    10. }
    11. server {
    12. listen 80;
    13. server_name linux.sj.com;
    14. location / {
    15. if ($http_user_agent ~* "Android") { #判断如果是安卓端
    16. proxy_pass http://android; #代理到android虚拟主机池
    17. }
    18. if ($http_user_agent ~* "iPhone") { #判断如果是苹果端
    19. proxy_pass http://iphone; #代理到iphone虚拟主机池
    20. }
    21. if ($http_user_agent ~* "WOW64") { #判断如果是IE浏览器
    22. return 403; #直接返回403
    23. }
    24. proxy_pass http://pc; #如果没有匹配到以上内容,默认都代理到pc虚拟主机池
    25. include proxy_params;
    26. }
    27. }
  2. 重启访问

    1. 1.检测并重启
    2. [root@lb01 ~]# nginx -t
    3. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    4. nginx: configuration file /etc/nginx/nginx.conf test is successful
    5. [root@lb01 ~]# systemctl restart nginx
    6. 2.配置hosts
    7. 172.16.1.5 linux.sj.com
    8. 3.访问测试

三、Nginx的rewrite重写

3.1 Rewrite基本概述

3.1.1 什么是rewrite

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

3.1.2 rewrite使用场景
  • 地址跳转,如用户访问www.linux.com这个URL时,将其定向至一个新的域名www.baidu.com。
  • 协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式。
  • 伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
  • 搜索引擎,SEO优化依赖于url路径,好记的url便于搜索引擎录入。
3.1.3 rewrite语法
  1. Syntax: rewrite regex replacement [flag];
  2. Default:
  3. Context: server, location, if
  4. rewrite #模块命令
  5. regex #请求的链接(支持正则表达式)
  6. replacement #跳转的链接
  7. [flag]; #标签
  8. eg:
  9. location /download/ {
  10. rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
  11. rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
  12. return 403;
  13. }

3.2 Rewrite标记flag

rewrite指令根据表达式来重定向URL,或者修改字符串,可以应用于server,location,if环境下,每行rewrite指令最后跟一个flag标记,支持的flag标记如下表格所示:

flag 作用
last 本条规则匹配完成后,停止匹配,不再匹配后面的规则
break 本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect 返回302临时重定向,地址栏会显示跳转后的地址
permanent 返回301永久重定向,地址栏会显示跳转后的地址
3.2.1 last和break的区别
  1. 配置Nginx(web01)

    1. [root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.rewrite.com;
    5. root /code;
    6. location ~ ^/break {
    7. rewrite ^/break /test/ break;
    8. }
    9. location ~ ^/last {
    10. rewrite ^/last /test/ last;
    11. }
    12. location /test/ {
    13. default_type application/json;
    14. return 200 "ok";
    15. }
    16. }
  2. 检测并重启

    1. [root@web01 ~]# nginx -t
    2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3. nginx: configuration file /etc/nginx/nginx.conf test is successful
    4. [root@web01 ~]# systemctl restart nginx
  3. 配置hosts访问测试

    1. 172.16.1.7 linux.rewrite.com
  4. 结论

    break 只要匹配到规则,就会去本地配置路径的目录中寻找请求的文件;
    而last只要匹配到规则,会对其所在的server(...)标签重新发起请求。

    break请求:
    1.请求linux.rewrite.com/break
    2.匹配 location ~ ^/break 会跳转到 linux.rewrite.com/test
    3.请求跳转后,会去查找本地站点目录下的 /test
    4.如果找到了,则返回/code/test/index.html的内容;
    5.如果没找到该目录则报错404,如果找到该目录没找到对应的文件则报错403

    last请求:
    1.请求linux.rewrite.com/last
    2.匹配 location ~ ^/last 会跳转到 linux.rewrite.com/test
    3.如果找到了,则返回/code/test/index.html的内容;
    4.如果没有找到,会重新对当前server发起请求,这个时候访问地址就变成 linux.rewrite.com/test
    5.重新请求server会匹配到 location /test/ 直接返回该location的内容
    6.如果也没有location匹配,再返回404;

3.2.2 redirect和permanent的区别
  1. 配置Nginx(web01)

    1. [root@web01 ~]# vim /etc/nginx/conf.d/linux.rewrite.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.rewrite.com;
    5. root /code;
    6. location /test {
    7. rewrite ^(.*)$ https://www.baidu.com redirect;
    8. #rewrite ^(.*)$ https://www.baidu.com permanent;
    9. }
    10. }
  2. 配置hosts测试

    1. 1.配置hosts
    2. 172.16.1.7 linux.rewrite.com
    3. #关闭nginx后访问测试
    4. 1)配置redirect时,访问失败,直接拒绝访问
    5. 2)配置permanent时,访问成功,继续进行跳转,清除缓存后访问失败
  3. 结论

    redirect: 每次请求都会询问服务器,如果当服务器不可用时,则会跳转失败。
    permanent: 第一次请求会询问,浏览器会记录跳转的地址,第二次则不再询问服务器,直接通过浏览器缓存的地址跳转。

3.3 Rewrite案例

将http请求跳转到https(LoadBalance):

  1. #Nginx跳转配置
  2. server {
  3. listen 80;
  4. server_name linux.rewrite.com;
  5. rewrite ^(.*) https://$server_name$1 redirect;
  6. #return 302 https://$server_name$request_uri;
  7. }
  8. server {
  9. listen 443 ssl;
  10. server_name blog.driverzeng.com;
  11. # ssl on;
  12. }
  13. URL: http://www.baidu.com:80/code/index.html
  14. URI: /code/index.html

3.4 Rewrite伪静态

搭建discuz论坛(web01)
  1. 创建站点目录

    1. [root@web01 ~]# mkdir /code/discuz
  2. 解压代码

    1. 1.上传代码包
    2. [root@web01 ~]# rz
    3. [root@web01 ~]# ll
    4. -rw-r--r--. 1 root root 10829853 Dec 7 12:04 Discuz_X3.3_SC_GBK.zip
    5. 2.解压
    6. [root@web01 ~]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/
    7. [root@web01 ~]# chown -R www.www /code/discuz/
  3. 配置Nginx配置文件

    1. [root@web01 ~]# vim /etc/nginx/conf.d/linux.discuz.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.discuz.com;
    5. root /code/discuz/upload;
    6. location / {
    7. root /code/discuz/upload;
    8. index index.php;
    9. }
    10. location ~* \.php$ {
    11. fastcgi_pass 127.0.0.1:9000;
    12. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    13. include fastcgi_params;
    14. }
    15. }
  4. 检测并重启

    1. [root@web01 ~]# nginx -t
    2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3. nginx: configuration file /etc/nginx/nginx.conf test is successful
    4. [root@web01 ~]# systemctl restart nginx
  5. 配置hosts访问测试

    1. 172.16.1.7 linux.discuz.com
    2. http://linux.discuz.com/install/
  6. 根据页面操作

  7. 创建数据库

    1. [root@db01 ~]# mysql -uroot -p123
    2. #建库
    3. MariaDB [(none)]> create database discuz;
    4. Query OK, 1 row affected (0.00 sec)
    5. #授权用户
    6. MariaDB [(none)]> grant all on discuz.* to discuz@'172.16.1.%' identified by '123456';
    7. Query OK, 0 rows affected (0.00 sec)
  8. 配置伪静态

    1. [root@web01 ~]# vim /etc/nginx/conf.d/linux.discuz.com.conf
    2. server {
    3. listen 80;
    4. server_name linux.discuz.com;
    5. root /code/discuz/upload;
    6. location / {
    7. root /code/discuz/upload;
    8. index index.php;
    9. rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
    10. rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
    11. rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    12. rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    13. rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
    14. rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
    15. rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
    16. rewrite ^([^\.]*)/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
    17. rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
    18. if (!-e $request_filename) {
    19. return 404;
    20. }
    21. }
    22. location ~* \.php$ {
    23. fastcgi_pass 127.0.0.1:9000;
    24. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    25. include fastcgi_params;
    26. }
    27. }

四、Rewrite补充

4.1 rewrite匹配优先级

  1. 优先级

    1. 先执行server块的rewrite指令
    2. 其次执行location匹配规则
    3. 最后执行location中的rewrite
  2. 配置

    1. server {
    2. listen 80;
    3. server_name linux.youxianxji.com;
    4. rewrite (.*) http://www.baidu.com;
    5. location / {
    6. rewrite (.*) http://www.jd.com;
    7. }
    8. location =/ {
    9. rewrite (.*) http://www.taobao.com;
    10. }
    11. }

4.2 rewrite的环境变量

  1. $server_name

    1. $server_name #当前用户请求的域名
    2. server {
    3. listen 80;
    4. server_name linux.test.com;
    5. rewrite ^(.*)$ https://$server_name$1;
    6. }
  2. 请求变量

    1. $request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg
    2. $request_uri 当前请求的文件路径(不带网站的主目录/images/test.jpg
    3. #大多数用于http协议转https协议
    4. server {
    5. listen 80;
    6. server_name linux.test.com;
    7. return 302 https://$server_name$request_uri;
    8. }
  3. $http_host

    1. #很古董的配置方法
    2. server {
    3. listen 80;
    4. server_name www.baidu.com baidu.com www.baidu.cn;
    5. if ($http_host = baidu.com){
    6. rewrite (.*) http://www.baidu.com$1;
    7. }
    8. }
    9. #推荐书写格式
    10. server {
    11. listen 80;
    12. server_name baidu.com;
    13. rewrite (.*) http://www.baidu.com$1;
    14. }
    15. server {
    16. listen 80;
    17. server_name www.baidu.com;
    18. location / {...}
    19. }

4.3 rewrite开启日志

  1. [root@web01 ~]# vim /etc/nginx/nginx.conf
  2. ... ...
  3. error_log /var/log/nginx/error.log notice;
  4. ... ...
  5. http {
  6. ... ...
  7. rewrite_log on;
  8. ... ...
  9. }

原文链接:http://www.cnblogs.com/JZjuechen/p/15777940.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号