小葵花妈妈课堂之Nginx Rewirte
寫在文章之前:現在Nginx已經成為很多公司作為前端反向代理(proxy pass)服務器的首選,在實際工作中往往會遇到很多跳轉(重寫URL)的需求。
比如:更換域名后需要保持舊的域名能跳轉到新的域名上、某網頁發生改變需要跳轉到新的頁面、網站防盜鏈等等需求。如果在后端使用的 Apache服務器,雖然也能做跳轉,規則庫也很強大,但是用Nginx跳轉效率會更高
?
一、Rewrite的介紹
? 和apache等web服務軟件一樣,rewrite的組要功能是實現RUL地址的重定向。Nginx的rewrite功能需要PCRE軟件的支持,即通過perl兼容正則表達式語句進行規則匹配的。默認參數編譯nginx就會支持rewrite的模塊,但是也必須要PCRE的支持
? rewrite是實現URL重寫的關鍵指令,根據regex(正則表達式)部分內容,重定向到replacement,結尾是flag標記。
二、rewrite在企業里的應用場景
三、nginx rewrite 基礎知識
3.1 nginx里常用的正則表達式元字符
3.2 rewrite基本用法
語法:rewrite regex replacement [flag]; PS: <regex>:匹配正則 <replacement>:跳轉后的內容 [flag]:rewrite支持的flag標記flag標志位: last : 相當于Apache的[L]標記,表示完成rewrite break : 停止執行當前虛擬主機的后續rewrite指令集 redirect : 返回302臨時重定向,地址欄會顯示跳轉后的地址 permanent : 返回301永久重定向,地址欄會顯示跳轉后的地址last與break的區別: last: 適用場景,一般寫在server和if中URL匹配,不終止重寫后的url匹配 break: 適用場景,一般使用再location中URL匹配, 終止重寫后的url、匹配3.3 rewrite執行順序
①執行server塊里面的rewrite指令。
②執行選定的location中的rewrite指令。
③執行選定的location中 if 中的rewrite指令
四、location跳轉
4.1 location跳轉分類
4.2 location常用的匹配規則
4.3 location匹配優先級
首先精確匹配 = 其次前綴匹配 ^~ ##匹配到后不再往下匹配## 其次是按文件中順序的正則匹配 ~或~* 然后匹配不帶任何修飾的前綴匹配 最后是交給 / 通用匹配示例:
(1)location = / {}
=為精確匹配 / ,主機名后面不能帶任何字符串,比如訪問 / 和 /data,則 / 匹配,/data 不匹配
再比如 location = /abc,則只匹配/abc ,/abc/或 /abcd不匹配。若 location ?/abc,則即匹配/abc 、/abcd/ 同時也匹配 /abc/。
(2)location / {}
因為所有的地址都以 / 開頭,所以這條規則將匹配到所有請求 比如訪問 / 和 /data, 則 / 匹配, /data 也匹配,
但若后面是正則表達式會和最長字符串優先匹配(最長匹配)
(3)location /documents/ {}
匹配任何以 /documents/ 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location
只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條
(4)location /documents/abc {}
匹配任何以 /documents/abc 開頭的地址,匹配符合以后,還要繼續往下搜索其它 location
只有其它 location后面的正則表達式沒有匹配到時,才會采用這一條
(5)location ^~ /images/ {}
匹配任何以 /images/ 開頭的地址,匹配符合以后,停止往下搜索正則,采用這一條
(6)location ~* \.(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 結尾的請求
然而,所有請求 /images/ 下的圖片會被 location ^~ /images/ 處理,因為 ^~ 的優先級更高,所以到達不了這一條正則
(7)location /images/abc {}
最長字符匹配到 /images/abc,優先級最低,繼續往下搜索其它 location,會發現 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
匹配以/images/abc 開頭的,優先級次之,只有去掉 location ^~ /images/ 才會采用這一條
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正則 ~ /images/abc/1.html 相比,正則優先級更高
location優先級總結:
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (location /)4.4?實際網站使用中,至少有三個匹配規則定義
第一個必選規則
直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,比如說官網。 這里是直接轉發給后端應用服務器了,也可以是一個靜態首頁location = / {proxy_pass http://tomcat_server/; }第二個必選規則
處理靜態文件請求,這是nginx作為http服務器的強項 有兩種配置模式,目錄匹配或后綴匹配,任選其一或搭配使用location ^~ /static/ {root /webroot/static/; }location ~* \.(html|gif|jpg|jpeg|png|css|js|ico)$ {root /webroot/res/; }第三個必選規則
比如用來轉發帶.php、.jsp后綴的動態請求到后端應用服務器 非靜態文件請求就默認是動態請求location / {proxy_pass http://tomcat_server; }五、具體操作
5.1 基于域名的跳轉
[root@server ~]# systemctl stop firewalld [root@server ~]# systemctl disable firewalld [root@server ~]# setenforce 0 [root@server ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemonLoaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)Active: inactive (dead)Docs: man:firewalld(1) [root@server ~]# vim /etc/resolv.confnameserver 114.114.114.114 [root@server ~]# [root@server ~]# vim /etc/hosts192.168.152.130 www.1.com www.2.com[root@server ~]# mkdir -p /var/log/nginx/ [root@server ~]# vim /usr/local/nginx/conf/nginx.conf#gzip on;server {listen 80;server_name www.1.com; #域名修改charset utf-8;access_log /var/log/nginx/www.1.com-access.log;#取消注釋,開啟并對日志保存路徑進行修改location / {#在原有location位置添加內容if ($host ='www.1.com') {rewrite^/(.*)$ http:// www.2.com/$1 permanent;}root html;index index.html index.htm;}[root@server ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server ~]# [root@server ~]# [root@server ~]# systemctl restart nginx訪問www.1.com自動跳轉www.2.com
5.2 基于IP地址的跳轉
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf#gzip on;server {listen 80;server_name www.1.com;charset utf-8;access_log /var/log/nginx/www.1.com-access.log;set $rewrite true;if ($remote_addr = "192.168.152.130") {set $rewrite false;}if ($rewrite = true){rewrite (.+) /weihu.html;}location = /weihu.html {root /var/www/html; }location / {root html;index index.html index.htm;}#error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}[root@server ~]# mkdir -p /var/www/html [root@server ~]# echo '<h1> this is weihu web! </h1>' > /var/www/html/weihu.html [root@server ~]# [root@server ~]# nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@server ~]# systemctl restart nginx5.3?基于舊域名跳轉到新域名后面加目錄
[root@server ~]# mkdir -p /usr/local/nginx/html/bbs/post [root@server ~]# echo "<h1> this is 1.html </h1>" >> /usr/local/nginx/html/bbs/post/1.html [root@server ~]# [root@server ~]# echo "192.168.152.130 bbs.1.com" >> /etc/hosts [root@server ~]# vim /etc/hosts127.0.0.1 localhost server localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.152.130 bbs.1.com www.1.com www.2.com[root@server ~]# vim /usr/local/nginx/conf/nginx.conf#gzip on;server {listen 80;server_name bbs.1.com;charset utf-8;access_log /var/log/nginx/www.1.com-access.log;location /post {rewrite (.+) http://www.1.com/bbs$1 permanent; }location / {root html;index index.html index.htm;}5.4?基于參數匹配(多余的)的跳轉
[root@server html]# vim /usr/local/nginx/conf/nginx.confserver {listen 80;server_name www.1.com;charset utf-8;access_log /var/log/nginx/www.1.com-access.log;if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$) {rewrite (.*) http://www.1.com permanent;}location / {root html;index index.html index.htm;}5.5?基于目錄下所有php結尾的文件跳轉
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf#gzip on;server {listen 80;server_name www.1.com;charset utf-8;access_log /var/log/nginx/www.1.com-access.log;location ~* /upload/.*\.php$ {rewrite (.+) http://www.1.com permanent;}location / {root html;index index.html index.htm;} [root@server ~]# nginx -t [root@server ~]# systemctl restart nginx5.6?基于最普通一條url請求的跳轉
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf#gzip on;server {listen 80;server_name www.1.com;charset utf-8;access_log /var/log/nginx/www.1.com-access.log;location ~* /abc/123.html {rewrite (.+) http://www.1.com permanent;}location / {root html;index index.html index.htm;}[root@server ~]# nginx -t [root@server ~]# systemctl restart nginx總結
以上是生活随笔為你收集整理的小葵花妈妈课堂之Nginx Rewirte的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谷歌账户跑着跑着没点击了,跑不出去什么原
- 下一篇: Nginx中传输带宽限制