日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例

發(fā)布時(shí)間:2024/3/13 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

文章目錄

      • 四層負(fù)載均衡
      • 四層負(fù)載均衡特點(diǎn)
      • 查看四層負(fù)載均衡語(yǔ)法
      • 四層負(fù)載均衡配置
      • 優(yōu)化配置文件
      • 四層負(fù)載均衡日志配置
      • nginx的TCP負(fù)載均衡---端口轉(zhuǎn)發(fā)
      • 動(dòng)靜分離
      • 單臺(tái)的動(dòng)靜分離
      • 多臺(tái)機(jī)器動(dòng)靜分離
      • 做動(dòng)靜分離和資源隔離的網(wǎng)站。
      • 一、動(dòng)靜分離的網(wǎng)站
          • 0.準(zhǔn)備環(huán)境
        • 1)環(huán)境準(zhǔn)備
        • 1.靜態(tài)資源
        • 1.1.上傳靜態(tài)資源
          • 1.2.配置靜態(tài)資源網(wǎng)站
          • 1.3.本地hosts訪問(wèn)
        • 2.配置動(dòng)態(tài)資源
          • 2.1.安裝tomcat
          • 2.2.配置動(dòng)態(tài)資源網(wǎng)站
          • 2.3.本地hosts訪問(wèn)
        • 3.負(fù)載均衡設(shè)置
          • 3.1創(chuàng)建站點(diǎn)目錄并授權(quán)
          • 3.2.編輯html文件
          • 3.3配置負(fù)載均衡文件
        • 3.4本地hosts訪問(wèn)
      • 二、資源分離的網(wǎng)站
          • 1.準(zhǔn)備環(huán)境
          • 2.配置web01服務(wù)器
          • 2、創(chuàng)建站點(diǎn)目錄
          • 3、訪問(wèn)測(cè)試
          • 3.配置web02服務(wù)器
          • 2、創(chuàng)建站點(diǎn)文件
          • 3、訪問(wèn)測(cè)試
          • 4.配置web03服務(wù)器
          • 2、創(chuàng)建站點(diǎn)文件
          • 3、訪問(wèn)測(cè)試
          • 5.配置負(fù)載均衡
          • 2、訪問(wèn)測(cè)試
      • Rewrite
      • rewrite概述
      • rewrite使用場(chǎng)景
      • rewrite配置語(yǔ)法
      • rewrite標(biāo)記 flag
      • last和break的區(qū)別
      • redirect和permanent的區(qū)別
      • rewrite規(guī)則匹配實(shí)例
      • 用戶訪問(wèn)`/abc/1.html`實(shí)際上真實(shí)訪問(wèn)的是`/ccc/bbb/2.html`
      • 用戶訪問(wèn)`/2018/ccc/2.html`實(shí)際上真實(shí)訪問(wèn)的是 `/2014/ccc/bbb/2.html`
      • 用戶訪問(wèn)`/test`實(shí)際上真實(shí)訪問(wèn)的是`www.baidu.com`
      • 用戶訪問(wèn) couese-11-22-33.html 實(shí)際上真實(shí)訪問(wèn)的是 /course/11/22/33/course_33.html
      • 將http請(qǐng)求跳轉(zhuǎn)到https
      • rewrite偽靜態(tài)實(shí)例
      • 搭建discuz論壇
      • 配置hosts,訪問(wèn)論壇,發(fā)表帖子
      • 配置rewrite偽靜態(tài)
      • rewrite全局變量
      • rewrite可以開(kāi)啟日志
      • rewrite規(guī)則補(bǔ)充
      • rewrite匹配的優(yōu)先級(jí)
      • uri和url的區(qū)別

四層負(fù)載均衡

1、四層+七層來(lái)做負(fù)載均衡,四層可以保證七層的負(fù)載均衡的高可用性; 2、負(fù)載均衡可以做端口轉(zhuǎn)發(fā) 3.四層可以做:mysql讀從庫(kù)的負(fù)載均衡 跳板機(jī)的端口映射

四層負(fù)載均衡特點(diǎn)

1>、四層負(fù)載均衡僅能轉(zhuǎn)發(fā)TCP/IP協(xié)議、UDP協(xié)議、通常用來(lái)轉(zhuǎn)發(fā)端口,如:tcp/22、udp/53; 2>、四層負(fù)載均衡可以用來(lái)解決七層負(fù)載均衡端口限制問(wèn)題;(七層負(fù)載均衡最大使用65535個(gè)端口號(hào)) 3>、四層負(fù)載均衡可以解決七層負(fù)載均衡高可用問(wèn)題;(多臺(tái)后端七層負(fù)載均衡能同事的使用) 4>、四層的轉(zhuǎn)發(fā)效率比七層的高得多,但僅支持tcp/ip協(xié)議,不支持http和https協(xié)議; 5>、通常大并發(fā)場(chǎng)景通常會(huì)選擇使用在七層負(fù)載前面增加四層負(fù)載均衡。

查看四層負(fù)載均衡語(yǔ)法

#四層負(fù)載均衡stream模塊跟http模塊同級(jí)別,不能配置在http里面,重新配置 stream {upstream backend {server backend1.example.com:12345 weight=5;server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;}server {listen 12345;proxy_connect_timeout 1s;proxy_timeout 3s;proxy_pass backend;} }

四層負(fù)載均衡配置

#配置主配置文件里面一個(gè)包含的語(yǔ)句 [root@lb4 nginx]# vim nginx.conf events {worker_connections 1024; }include /etc/nginx/conf.c/*.conf;http { ... }#創(chuàng)建四層負(fù)載配置的目錄 [root@lb4 nginx]# mkdir /etc/nginx/conf.c#配置四層負(fù)載 [root@lb4 nginx]# vim /etc/nginx/conf.c/lb_proxy.conf stream {upstream lbserver {server 172.16.1.4:8080;server 172.16.1.5:80;}server {listen 80;proxy_connect_timeout 1s;proxy_timeout 3s;proxy_pass lbserver;} }#重啟nginx [root@lb4 nginx]# systemctl restart nginx #如果重啟報(bào)錯(cuò) [root@lb4 nginx]# rm -rf /etc/nginx/conf.d/*#配置hosts訪問(wèn)測(cè)試

優(yōu)化配置文件

[root@Nginx ~]# vim /etc/nginx/proxy_params proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60;proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;

四層負(fù)載均衡日志配置

[root@lb4 nginx]# vim /etc/nginx/conf.c/lb_proxy.conf stream {log_format main '$remote_addr $remote_port - [$time_local] $status $protocol ''"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;access_log /var/log/nginx/lb4_access.log main;upstream lbserver {server 172.16.1.4:80;server 172.16.1.5:80;}server {listen 80;proxy_connect_timeout 1s;proxy_timeout 3s;proxy_pass lbserver;} }

nginx的TCP負(fù)載均衡—端口轉(zhuǎn)發(fā)

#請(qǐng)求負(fù)載均衡的5555端口,跳轉(zhuǎn)至172.16.1.7的22端口stream { #轉(zhuǎn)發(fā)ssh的22端口upstream ssh_7 {server 172.16.1.7:22;}server {listen 5555;proxy_pass ssh_7;} }#請(qǐng)求負(fù)載均衡的6666端口,跳轉(zhuǎn)至172.16.1.51的3306端口 stream {#轉(zhuǎn)發(fā)mysql的3306端口upstream mysql_51 {server 172.16.1.51:3306;server 172.16.1.52:3306;server 172.16.1.53:3306;server 172.16.1.54:3306;server 172.16.1.55:3306;server 172.16.1.56:3306;server 172.16.1.57:3306;}server {listen 6666;proxy_pass mysql_51;} } # nginx必須啟用--with-stream## 編寫(xiě)nginx配置 mkdir /etc/nginx/stream.d## 編寫(xiě)配置文件 [root@lb01 stream.d]# cat mysql.conf stream {upstream mysql_conn {server 172.16.1.51:3306;}server {listen 33060;proxy_pass mysql_conn;} }## 重啟nginx [root@lb01 stream.d]# systemctl restart nginx## 利用nginx轉(zhuǎn)發(fā)連接MySQL [root@db01 ~]# mysql -uyangge -pyangge -h172.16.1.5 -P 33060

動(dòng)靜分離

動(dòng):動(dòng)態(tài)頁(yè)面(php、python) 靜:靜態(tài)頁(yè)面(html、gif)CDN(可以提高訪問(wèn)頁(yè)面或者圖片的速度)動(dòng)靜分離,通過(guò)中間件將動(dòng)靜分離和靜態(tài)請(qǐng)求進(jìn)行分離; 通過(guò)中間件將動(dòng)態(tài)請(qǐng)求和靜態(tài)請(qǐng)求分離,可以建上不必要的請(qǐng)求消耗,同事能減少請(qǐng)求的延時(shí)。 通過(guò)中間件將動(dòng)態(tài)請(qǐng)求和靜態(tài)請(qǐng)求分離

單臺(tái)的動(dòng)靜分離

[root@web01 conf.d]# vim blog.conf server {listen 80;server_name blog.linux.com;location / {root /code/wordpress;index index.php;}#如果請(qǐng)求的是以 .jpg結(jié)尾的靜態(tài)文件 就去/code/images 目錄下訪問(wèn)location ~* \.jpg$ {root /code/images;}location ~* \.php$ {root /code/wordpress;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;} }#創(chuàng)建目錄 [root@web01 conf.d]# mkdir /code/images/#實(shí)現(xiàn)動(dòng)靜分離 方式一:把文件挪到/code/images/cp -r /code/wordpress/wp-content /code/images/ 方式二:做軟連接cd /codeln -s wordpress images

多臺(tái)機(jī)器動(dòng)靜分離

#web01靜態(tài)內(nèi)容 #配置nginx [root@web01 conf.d]# vim jt.conf server {listen 80;server_name dj.linux.com;location ~* \.(jpg|png|gif)${root /code/picture;} }#重啟nginx [root@web01 conf.d]# systemctl restart nginx#上傳圖片 [root@web01 conf.d]# mkdir /code/picture [root@web01 conf.d]# cd /code/picture/ [root@web01 picture]# rz 1.jpg#配置hosts ,訪問(wèn)圖片#web02動(dòng)態(tài)內(nèi)容#安裝tomcat [root@web02 code]# yum install -y tomcat#配置動(dòng)態(tài)內(nèi)容 [root@web02 code]# cd /usr/share/tomcat/webapps/ [root@web02 webapps]# mkdir ROOT [root@web02 webapps]# vim 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>隨機(jī)數(shù):<h1>");out.println(rand.nextInt(99)+100);%></BODY> </HTML>#啟動(dòng)tomcat [root@web02 webapps]# systemctl start tomcat#訪問(wèn)頁(yè)面 http://10.0.0.8:8080/java_test.jsp#負(fù)載均衡上配置頁(yè)面 #配置負(fù)載均衡的nginx [root@lb01 conf.d]# vim dj.linux.com.conf upstream jt {server 172.16.1.7:80; } upstream dt {server 172.16.1.9:8080; }server {listen 80;server_name dj.linux.com;location / {root /code/dj;index index.html;}location ~* \.(jpg|gif|png)$ {proxy_pass http://jt;proxy_set_header HOST $http_host;}location ~ \.jsp$ {proxy_pass http://dt;proxy_set_header HOST $http_host;} }#重啟nginx [root@lb01 conf.d]# systemctl restart nginx#配置host,訪問(wèn)頁(yè)面 http://dj.linux.com/java_test.jsp http://dj.linux.com/1.jpg#整合靜態(tài)內(nèi)容和動(dòng)態(tài)內(nèi)容代碼 [root@lb01 conf.d]# mkdir /code/dj -p [root@lb01 conf.d]# vim /code/dj/index.html <html lang="en"> <head><meta charset="UTF-8" /><title>測(cè)試ajax和跨域訪問(wèn)</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://dj.linux.com/java_test.jsp",success: function(data){$("#get_data").html(data)},error: function() {alert("哎呦喂,失敗了,回去檢查你服務(wù)去~");}}); }); </script><body><h1>測(cè)試動(dòng)靜分離</h1><img src="http://dj.linux.com/1.gif"><div id="get_data"></div></body> </html>#授權(quán)站點(diǎn)目錄 [root@lb01 conf.d]# chown -R nginx.nginx /code/#訪問(wèn)域名,測(cè)試,靜態(tài)內(nèi)容和動(dòng)態(tài)內(nèi)容關(guān)閉其一,互不影響

做動(dòng)靜分離和資源隔離的網(wǎng)站。

一、動(dòng)靜分離的網(wǎng)站

0.準(zhǔn)備環(huán)境

1)環(huán)境準(zhǔn)備

主機(jī)作用服務(wù)地址
lb01負(fù)載均衡nginx proxy172.16.1.5
web01靜態(tài)資源nginx static172.16.1.7
web02動(dòng)態(tài)資源tomcat server172.16.1.8

1.靜態(tài)資源

1.1.上傳靜態(tài)資源

## 創(chuàng)建目錄 并授權(quán) [root@web01 ~]# mkdir /yeg/picture -p [root@web01 ~]# chown -R www.www /yeg/ [root@web01 ~]# cd /yeg/picture/ [root@web01 picture]# rz上傳 -rw-r--r-- 1 www www 746368 May 6 16:51 1.jpg
1.2.配置靜態(tài)資源網(wǎng)站
[root@web01 conf.d]# vim linux.dj.conf server {listen 80;server_name linux12.dj.com;location ~* \.(jpg|png|mp4|gif)$ {root /yeg/picture;} } ## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
1.3.本地hosts訪問(wèn)
1.本地配置hosts192.168.15.7 linux12.dj.com2、訪問(wèn)靜態(tài)資源http://linux12.dj.com/1.jpg

2.配置動(dòng)態(tài)資源

2.1.安裝tomcat
[root@web02 ~]# yum install -y tomcat
2.2.配置動(dòng)態(tài)資源網(wǎng)站
[root@web02 ~]# cd /usr/share/tomcat/webapps[root@web02 webapps]# mkdir ROOT [root@web02 webapps]# vi ROOT/java_test.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <HTML><HEAD><TITLE>測(cè)試動(dòng)態(tài)的資源</TITLE></HEAD><BODY><%Random rand = new Random();out.println("<h1>隨機(jī)數(shù):<h1>");out.println(rand.nextInt(99)+100);%></BODY> </HTML>[root@web01 ~]# systemctl restart tomcat
2.3.本地hosts訪問(wèn)
1、配置本地hosts192.168.15.8 linux12.dj.com2、訪問(wèn)動(dòng)態(tài)資源http://linux12.dj.com:8080/java_test.jsp

3.負(fù)載均衡設(shè)置

3.1創(chuàng)建站點(diǎn)目錄并授權(quán)
[root@lb01 opt]# mkdir -p /yeg/dj [root@lb01 opt]# chown -R www.www /yeg/
3.2.編輯html文件
[root@lb01 ~]# vim /yeg/dj/index.html <head><meta charset="UTF-8" /><title>測(cè)試ajax和跨域訪問(wèn)</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://linux12.dj.com/java_test.jsp",success: function(data){$("#get_data").html(data)},error: function() {alert("小姐姐,斷網(wǎng)了,重新檢查網(wǎng)絡(luò)再來(lái)哦~");}}); }); </script><body><h1>測(cè)試動(dòng)靜分離---霉霉</h1><img src="http://linux12.dj.com/1.jpg"><div id="get_data"></div></body> </html>
3.3配置負(fù)載均衡文件
[root@lb01 conf.d]# vim linux.dj.conf server {listen 80;server_name linux12.dj.com;location / {root /yeg/dj;index index.html;}location ~* \.(jpg|png|gif)$ {proxy_pass http://192.168.15.7;include proxy_params;}location ~* \.(php|jsp)$ {proxy_pass http://192.168.15.8:8080;include proxy_params;} }## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx

3.4本地hosts訪問(wèn)

1、配置本地hosts192.168.15.5 linux12.dj.com

二、資源分離的網(wǎng)站

1.準(zhǔn)備環(huán)境
主機(jī)IP主機(jī)角色條件
web01192.168.15.7Android頁(yè)面關(guān)閉防火墻和selinux
web02192.168.15.8iPhone頁(yè)面關(guān)閉防火墻和selinux
web03192.168.15.9PC端頁(yè)面關(guān)閉防火墻和selinux
lb01192.168.15.5 172.16.1.5負(fù)載均衡關(guān)閉防火墻和selinux
2.配置web01服務(wù)器
# 1、配置nginx [root@web01 conf.d]# cat linux.sj.com.conf server {listen 80;server_name linux12.dj.com;charset utf8;location / {root /yang/android;index index.html;} }## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
2、創(chuàng)建站點(diǎn)目錄
[root@web01 ~]# mkdir -p /yang/android [root@web01 ~]# echo "我是android" >> /yang/android/index.html [root@web01 ~]# chown -R www.www /yang/android/
3、訪問(wèn)測(cè)試
# 1.配置hosts 192.168.15.7 linux12.dj.com
3.配置web02服務(wù)器
1、配置nginx [root@web02 conf.d]# cat linux12.sj.com.conf server {listen 80;server_name linux12.dj.com;charset utf8;location / {root /yang/iphone;index index.html;} } ## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
2、創(chuàng)建站點(diǎn)文件
[root@web02 ~]# mkdir -p /yang/iphone [root@web02 ~]# echo "我是Iphone" >> /yang/iphone/index.html [root@web02 ~]# chown -R www.www /yang/iphone/
3、訪問(wèn)測(cè)試
# 1.配置hosts 192.168.15.8 linux12.dj.com
4.配置web03服務(wù)器
# 1、配置nginx [root@web03 conf.d]# cat linux12.sj.com.conf server {listen 80;server_name linux12.dj.com;charset utf8;location / {root /yang/pc;index index.html;} }## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
2、創(chuàng)建站點(diǎn)文件
[root@web03 ~]# mkdir -p /yang/pc [root@web03 ~]# echo "我是pc端" >> /yang/pc/index.html [root@web03 ~]# chown -R www.www /yang/pc
3、訪問(wèn)測(cè)試
# 1.配置hosts 192.168.15.9 linux12.dj.com
5.配置負(fù)載均衡
# 1.配置nginx -01 [root@lb01 conf.d]# vim linux12.sj.com.conf upstream android {server 192.168.15.7:8081; }upstream iphone {server 192.168.15.8:8082; }upstream pc {server 192.168.15.9:8083; }server {listen 80;server_name linux12.dj.com;location / {if ($http_user_agent ~* "Android") { #判斷如果是安卓端proxy_pass http://android; #代理到android虛擬主機(jī)池}if ($http_user_agent ~* "iPhone") { #判斷如果是蘋(píng)果端proxy_pass http://iphone; #代理到iphone虛擬主機(jī)池}if ($http_user_agent ~* "Trident") { #判斷如果是IE瀏覽器return 403; #直接返回403}proxy_pass http://pc; #如果沒(méi)有匹配到以上內(nèi)容,默認(rèn)都代理到pc虛擬主機(jī)池} }# 1.配置nginx -02 [root@lb01 conf.d]# vim linux12.sj.com.conf server {listen 80;server_name linux12.dj.com;location / {if ($http_user_agent ~* "Android") { #判斷如果是安卓端proxy_pass http://192.168.15.7; #代理到android虛擬主機(jī)池}if ($http_user_agent ~* "iPhone") { #判斷如果是蘋(píng)果端proxy_pass http://192.168.15.8; #代理到iphone虛擬主機(jī)池}if ($http_user_agent ~* "WOW64") { #判斷如果是IE瀏覽器return 403; #直接返回403}proxy_pass http://192.168.15.9; #如果沒(méi)有匹配到以上內(nèi)容,默認(rèn)都代理到pc虛擬主機(jī)池include proxy_params;} }## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
2、訪問(wèn)測(cè)試
# 1.配置hosts 192.168.15.5 yang.sj.com

Rewrite

rewrite概述

Rewrite主要實(shí)現(xiàn)url地址重寫(xiě),以及重定向,就是把傳入web的請(qǐng)求重定向到其他url的過(guò)程。

rewrite使用場(chǎng)景

1、地址跳轉(zhuǎn),用戶訪問(wèn)www.drz.com這個(gè)URL是,將其定向至一個(gè)新的域名mobile.drz.com 2、協(xié)議跳轉(zhuǎn),用戶通過(guò)http協(xié)議請(qǐng)求網(wǎng)站時(shí),將其重新跳轉(zhuǎn)至https協(xié)議方式 3、偽靜態(tài),將動(dòng)態(tài)頁(yè)面顯示為靜態(tài)頁(yè)面方式的一種技術(shù),便于搜索引擎的錄入,同時(shí)建上動(dòng)態(tài)URL地址對(duì)外暴露過(guò)多的參數(shù),提升更高的安全性。 4、搜索引擎,SEO優(yōu)化依賴于url路徑,好記的url便于智齒搜索引擎錄入

rewrite配置語(yǔ)法

Syntax: rewrite regex replacement [flag]; Default: — Context: server, location, if#一般用于切換維護(hù)場(chǎng)景 rewrite ^(.*)$ /page/404.html last;

rewrite標(biāo)記 flag

flag作用
last本條規(guī)則匹配完成后,停止匹配,不再匹配后面的規(guī)則
break本條規(guī)則匹配完成后,停止匹配,不再匹配后面的規(guī)則
redirect返回302臨時(shí)重定向,地址欄會(huì)顯示跳轉(zhuǎn)后的地址
permanent返回301永久重定向,地址欄會(huì)顯示跳轉(zhuǎn)后的地址

last和break的區(qū)別

#配置nginx [root@web01 conf.d]# vim rewrite.con server {listen 80;server_name rw.linux.com;root /code/rewrite;location ~ ^/break {rewrite ^/break /test/ break;}location ~ ^/last {rewrite ^/last /test/ last;}location /test/ {default_type application/json;return 200 "ok";} }#重啟nginx [root@web01 conf.d]# systemctl restart nginx#訪問(wèn)頁(yè)面測(cè)試break 只要匹配到規(guī)則,就回去本地路徑目錄中尋找請(qǐng)求的文件; last 匹配到規(guī)則,跳轉(zhuǎn)后沒(méi)有內(nèi)容,則帶著跳轉(zhuǎn)后的請(qǐng)求,重新的向server發(fā)起一次請(qǐng)求break請(qǐng)求:1.請(qǐng)求rw.linux.com/break;2.首先,會(huì)去查找本地的/code/rewrite/test/index.html;3.如果找到了,則返回/code/rewrite/test/index.html內(nèi)容;4.如果沒(méi)有找到則返回404,找到目錄卻沒(méi)有主頁(yè),則返回403;last請(qǐng)求:1.請(qǐng)求rw.linux.com/last;2.首先,會(huì)去查找本地的/code/rewrite/test/index.html;3.如果找到了,則返回/code/rewrite/test/index.html內(nèi)容;4.如果沒(méi)找到,會(huì)帶著新跳轉(zhuǎn)的URI再向server發(fā)起一次請(qǐng)求,請(qǐng)求rw.linux.com/test;5.如果匹配到新的location,則返回該location匹配的內(nèi)容;6.如果沒(méi)有匹配到新的,則再返回404或403;共同點(diǎn):break和last匹配到之后,不再向下匹配

redirect和permanent的區(qū)別

[root@web01 conf.d]# cat rewrite.conf server {listen 80;server_name rw.linux.com;root /code/rewrite;location /test {rewrite ^(.*)$ http://www.mumusir.com redirect;#rewrite ^(.*)$ http://www.mumusir.com permanent;} }#配置兩種跳轉(zhuǎn),關(guān)閉nginx測(cè)試,查看結(jié)果redirect:每次請(qǐng)求都會(huì)詢問(wèn)服務(wù)器,是否跳轉(zhuǎn),如果服務(wù)器不可用,則跳轉(zhuǎn)失敗 permanent:請(qǐng)求一次后,會(huì)記錄跳轉(zhuǎn)的地址,以后不再詢問(wèn),直接跳轉(zhuǎn),通過(guò)瀏覽器緩存記錄

rewrite規(guī)則匹配實(shí)例

1、通過(guò)192.168.15.7/index/1/2/3/4/5/6.html 訪問(wèn)更目錄下的index-1-2-3-4-5-6.html server {listen 80;server_name _;root /www/resources;location / {rewrite ^/index/([0-9])/([0-9])/([0-9])/([0-9])/([0-9])/([0-9]) /index-$1-$2-$3-$4-$5-$6.html break;} }2、使用192.168.15.7/jd訪問(wèn)www.jd.com server {listen 80;server_name _;root /www/resources;location / {rewrite ^/(.*) http://www.$1.com redirect;} }3、根目錄有index-test.html和xxx-abc.html, 怎樣通過(guò)192.168.15.7/index/test訪問(wèn)index-test.html, 使用192.168.15.7/xxx/abc訪問(wèn)xxx-abc.html server {listen 80;server_name _;root /www/resources;location / {rewrite ^/(.*)/(.*) /$1-$2.html break;}} # 格式 rewrite [匹配規(guī)則] [轉(zhuǎn)發(fā)內(nèi)容] flag nginx只支持簡(jiǎn)單正則,高級(jí)正則不支持。

用戶訪問(wèn)/abc/1.html實(shí)際上真實(shí)訪問(wèn)的是/ccc/bbb/2.html

[root@web01 conf.d]# vim rw.conf server {listen 80;server_name rw.linux.com;root /code;location ~ /abc {rewrite ^(.*)$ /ccc/bbb/2.html redirect;#rewrite /abc/1.html /ccc/bbb/2.html redirect;} }#/abc/1.html /ccc/bbb/1.htmllocation ~ /abc {rewrite /abc/(.*)\.html /ccc/bbb/$1.html redirect;}

用戶訪問(wèn)/2018/ccc/2.html實(shí)際上真實(shí)訪問(wèn)的是 /2014/ccc/bbb/2.html

[root@web01 conf.d]# mkdir /code/2014/ccc/bbb/ -p [root@web01 conf.d]# echo "2014-ccc-bbb-222" > /code/2014/ccc/bbb/2.html server {listen 80;server_name rw.linux.com;root /code;location ~ /2018 {rewrite /2018/ccc/2.html /2014/ccc/bbb/2.html redirect;} }#/2018/ccc/bbb/2.html 跳轉(zhuǎn) /2014/ccc/bbb/2.htmllocation ~ /2018 {rewrite /2018/(.*) /2014/$1 redirect;}

用戶訪問(wèn)/test實(shí)際上真實(shí)訪問(wèn)的是www.baidu.com

server {listen 80;server_name rw.linux.com;root /code;location ~ /test {rewrite (.*) https://www.baidu.com redirect;} }

用戶訪問(wèn) couese-11-22-33.html 實(shí)際上真實(shí)訪問(wèn)的是 /course/11/22/33/course_33.html

[root@web01 conf.d]# mkdir /code/course/11/22/33/ -p [root@web01 conf.d]# echo "course-111-222-333" > /code/course/11/22/33/course_33.html [root@web01 conf.d]# vim rw.conf server {listen 80;server_name rw.linux.com;root /code;location / {#rewrite ^/course-11-22-33.html /course/11/22/33/course_33.html redirect;rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html redirect;} }

將http請(qǐng)求跳轉(zhuǎn)到https

server {listen 80;server_name www.mumusir.com;#rewrite (.*) https://www.mumusir.com redirect;return 302 https://www.mumusir.com; }http://www.mumusir.com --> https://www.mumusir.comserver {listen 443;server_name www.mumusir.com;ssl on;ssl...... *.key;ssl..... *.crt; }

rewrite偽靜態(tài)實(shí)例

搭建discuz論壇

#創(chuàng)建站點(diǎn)目錄 [root@web01 ~]# mkdir /code/discuz [root@web01 code]# rz Discuz_X3.3_SC_GBK.zip [root@web01 code]# unzip Discuz_X3.3_SC_GBK.zip -d /code/discuz/#授權(quán)站點(diǎn)目錄 [root@web01 code]# chown -R www.www /code/discuz/#配置discuz論壇的配置文件 [root@web01 conf.d]# cp blog.conf discuz.linux.com.conf server {listen 80;server_name discuz.linux.com;location / {root /code/discuz/upload;index index.php;}location ~* \.php$ {root /code/discuz/upload;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;} }#配置hosts訪問(wèn)#創(chuàng)建數(shù)據(jù)庫(kù) [root@db02 ~]# mysql -u root -p123456 。。。。。。MariaDB [(none)]> create database discuz charset utf8; 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.04 sec)MariaDB [(none)]>

配置hosts,訪問(wèn)論壇,發(fā)表帖子

#查看帖子地址http://discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=

配置rewrite偽靜態(tài)

[root@web01 conf.d]# vim /etc/nginx/conf.d/discuz.linux.com.conf server {listen 80;server_name discuz.linux.com;location / {root /code/discuz/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$ {root /code/discuz/upload;fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;} }[root@web01 conf.d]# systemctl restart nginx http://discuz.linux.com/thread-1-1-1.htmlrewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;discuz.linux.com/thread-1-1-1.html discuz.linux.com/forum.php?mod=viewthread&tid=1&extra=page%3D1&page=1

rewrite全局變量

$server_name #當(dāng)前用戶請(qǐng)求的域名 server {listen 80;server_name rw.linux.com;root /code;rewrite ^(.*)$ https://$server_name; }$request_filename #請(qǐng)求的文件路徑和名字(帶著網(wǎng)站站點(diǎn)目錄的路徑和文件 /code/images/1.jpg) $request_uri #請(qǐng)求的文件路徑和名字(不帶網(wǎng)站站點(diǎn)目錄的路徑和文件 /images/1.jpg)server {listen 80;server_name rw.linux.com;root /code;rewrite ^(.*)$ https://$server_name$request_uri; }#很久很久以前,網(wǎng)站優(yōu)化 server {listen 80;server_name www.baidu.com baidu.com;root /code;if ($http_host = baidu.com) {rewrite (.*) http://www.baidu.com;} }#實(shí)際寫(xiě)法 server {listen 80;server_name baidu.com;rewrite (.*) http://www.baidu.com;} server {listen 80;server_name www.baidu.com;root /code; }

rewrite可以開(kāi)啟日志

#NGINX主配置文件,錯(cuò)誤日志級(jí)別改成notice error_log /var/log/nginx/error.log notice;#http層開(kāi)啟rewrite日志 rewrite_log on;

rewrite規(guī)則補(bǔ)充

rewrite匹配的優(yōu)先級(jí)

1.先執(zhí)行server模塊的rewrite指令 2.其次執(zhí)行l(wèi)ocation匹配規(guī)則 3.最后執(zhí)行l(wèi)ocation里面的rewriteserver {listen 80;server_name rw.linux.com;#rewrite ^(.*)$ http://www.jingdong.com;location /test {rewrite ^(.*)$ http://www.mumusir.com;}location =/ {rewrite ^(.*)$ http://www.baidu.com;} }

uri和url的區(qū)別

http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.htmlurl : http://192.168.15.7/video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html uri : /video-sousuo-117877-18-0-0-0-1-all-complex-0-0-0-0-0-0.html

總結(jié)

以上是生活随笔為你收集整理的四层负载均衡 动静分离和资源分离 Rewrite rewrite伪静态实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。