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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

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

文章目錄

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

四層負(fù)載均衡

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

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

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

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

#四層負(fù)載均衡stream模塊跟http模塊同級別,不能配置在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ù)載均衡配置

#配置主配置文件里面一個包含的語句 [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 #如果重啟報錯 [root@lb4 nginx]# rm -rf /etc/nginx/conf.d/*#配置hosts訪問測試

優(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ā)

#請求負(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;} }#請求負(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## 編寫nginx配置 mkdir /etc/nginx/stream.d## 編寫配置文件 [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

動靜分離

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

單臺的動靜分離

[root@web01 conf.d]# vim blog.conf server {listen 80;server_name blog.linux.com;location / {root /code/wordpress;index index.php;}#如果請求的是以 .jpg結(jié)尾的靜態(tài)文件 就去/code/images 目錄下訪問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/#實現(xiàn)動靜分離 方式一:把文件挪到/code/images/cp -r /code/wordpress/wp-content /code/images/ 方式二:做軟連接cd /codeln -s wordpress images

多臺機(jī)器動靜分離

#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 ,訪問圖片#web02動態(tài)內(nèi)容#安裝tomcat [root@web02 code]# yum install -y tomcat#配置動態(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>#啟動tomcat [root@web02 webapps]# systemctl start tomcat#訪問頁面 http://10.0.0.8:8080/java_test.jsp#負(fù)載均衡上配置頁面 #配置負(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,訪問頁面 http://dj.linux.com/java_test.jsp http://dj.linux.com/1.jpg#整合靜態(tài)內(nèi)容和動態(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>測試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://dj.linux.com/java_test.jsp",success: function(data){$("#get_data").html(data)},error: function() {alert("哎呦喂,失敗了,回去檢查你服務(wù)去~");}}); }); </script><body><h1>測試動靜分離</h1><img src="http://dj.linux.com/1.gif"><div id="get_data"></div></body> </html>#授權(quán)站點目錄 [root@lb01 conf.d]# chown -R nginx.nginx /code/#訪問域名,測試,靜態(tài)內(nèi)容和動態(tài)內(nèi)容關(guān)閉其一,互不影響

做動靜分離和資源隔離的網(wǎ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動態(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訪問
1.本地配置hosts192.168.15.7 linux12.dj.com2、訪問靜態(tài)資源http://linux12.dj.com/1.jpg

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

2.1.安裝tomcat
[root@web02 ~]# yum install -y tomcat
2.2.配置動態(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>測試動態(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訪問
1、配置本地hosts192.168.15.8 linux12.dj.com2、訪問動態(tài)資源http://linux12.dj.com:8080/java_test.jsp

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

3.1創(chuàng)建站點目錄并授權(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>測試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://linux12.dj.com/java_test.jsp",success: function(data){$("#get_data").html(data)},error: function() {alert("小姐姐,斷網(wǎng)了,重新檢查網(wǎng)絡(luò)再來哦~");}}); }); </script><body><h1>測試動靜分離---霉霉</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訪問

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

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

1.準(zhǔn)備環(huán)境
主機(jī)IP主機(jī)角色條件
web01192.168.15.7Android頁面關(guān)閉防火墻和selinux
web02192.168.15.8iPhone頁面關(guān)閉防火墻和selinux
web03192.168.15.9PC端頁面關(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)建站點目錄
[root@web01 ~]# mkdir -p /yang/android [root@web01 ~]# echo "我是android" >> /yang/android/index.html [root@web01 ~]# chown -R www.www /yang/android/
3、訪問測試
# 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)建站點文件
[root@web02 ~]# mkdir -p /yang/iphone [root@web02 ~]# echo "我是Iphone" >> /yang/iphone/index.html [root@web02 ~]# chown -R www.www /yang/iphone/
3、訪問測試
# 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)建站點文件
[root@web03 ~]# mkdir -p /yang/pc [root@web03 ~]# echo "我是pc端" >> /yang/pc/index.html [root@web03 ~]# chown -R www.www /yang/pc
3、訪問測試
# 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") { #判斷如果是蘋果端proxy_pass http://iphone; #代理到iphone虛擬主機(jī)池}if ($http_user_agent ~* "Trident") { #判斷如果是IE瀏覽器return 403; #直接返回403}proxy_pass http://pc; #如果沒有匹配到以上內(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") { #判斷如果是蘋果端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; #如果沒有匹配到以上內(nèi)容,默認(rèn)都代理到pc虛擬主機(jī)池include proxy_params;} }## 檢查nginx -t 并重啟 [root@web01 ~]# systemctl restart nginx
2、訪問測試
# 1.配置hosts 192.168.15.5 yang.sj.com

Rewrite

rewrite概述

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

rewrite使用場景

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

rewrite配置語法

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

rewrite標(biāo)記 flag

flag作用
last本條規(guī)則匹配完成后,停止匹配,不再匹配后面的規(guī)則
break本條規(guī)則匹配完成后,停止匹配,不再匹配后面的規(guī)則
redirect返回302臨時重定向,地址欄會顯示跳轉(zhuǎn)后的地址
permanent返回301永久重定向,地址欄會顯示跳轉(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#訪問頁面測試break 只要匹配到規(guī)則,就回去本地路徑目錄中尋找請求的文件; last 匹配到規(guī)則,跳轉(zhuǎn)后沒有內(nèi)容,則帶著跳轉(zhuǎn)后的請求,重新的向server發(fā)起一次請求break請求:1.請求rw.linux.com/break;2.首先,會去查找本地的/code/rewrite/test/index.html;3.如果找到了,則返回/code/rewrite/test/index.html內(nèi)容;4.如果沒有找到則返回404,找到目錄卻沒有主頁,則返回403;last請求:1.請求rw.linux.com/last;2.首先,會去查找本地的/code/rewrite/test/index.html;3.如果找到了,則返回/code/rewrite/test/index.html內(nèi)容;4.如果沒找到,會帶著新跳轉(zhuǎn)的URI再向server發(fā)起一次請求,請求rw.linux.com/test;5.如果匹配到新的location,則返回該location匹配的內(nèi)容;6.如果沒有匹配到新的,則再返回404或403;共同點: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測試,查看結(jié)果redirect:每次請求都會詢問服務(wù)器,是否跳轉(zhuǎn),如果服務(wù)器不可用,則跳轉(zhuǎn)失敗 permanent:請求一次后,會記錄跳轉(zhuǎn)的地址,以后不再詢問,直接跳轉(zhuǎn),通過瀏覽器緩存記錄

rewrite規(guī)則匹配實例

1、通過192.168.15.7/index/1/2/3/4/5/6.html 訪問更目錄下的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訪問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, 怎樣通過192.168.15.7/index/test訪問index-test.html, 使用192.168.15.7/xxx/abc訪問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只支持簡單正則,高級正則不支持。

用戶訪問/abc/1.html實際上真實訪問的是/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;}

用戶訪問/2018/ccc/2.html實際上真實訪問的是 /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;}

用戶訪問/test實際上真實訪問的是www.baidu.com

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

用戶訪問 couese-11-22-33.html 實際上真實訪問的是 /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請求跳轉(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)實例

搭建discuz論壇

#創(chuàng)建站點目錄 [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)站點目錄 [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訪問#創(chuàng)建數(shù)據(jù)庫 [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,訪問論壇,發(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)前用戶請求的域名 server {listen 80;server_name rw.linux.com;root /code;rewrite ^(.*)$ https://$server_name; }$request_filename #請求的文件路徑和名字(帶著網(wǎng)站站點目錄的路徑和文件 /code/images/1.jpg) $request_uri #請求的文件路徑和名字(不帶網(wǎng)站站點目錄的路徑和文件 /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;} }#實際寫法 server {listen 80;server_name baidu.com;rewrite (.*) http://www.baidu.com;} server {listen 80;server_name www.baidu.com;root /code; }

rewrite可以開啟日志

#NGINX主配置文件,錯誤日志級別改成notice error_log /var/log/nginx/error.log notice;#http層開啟rewrite日志 rewrite_log on;

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

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

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伪静态实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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