創(chuàng)新互聯(lián)長期為數(shù)千家客戶提供的網(wǎng)站建設(shè)服務(wù),團隊從業(yè)經(jīng)驗10年,關(guān)注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為臨汾企業(yè)提供專業(yè)的網(wǎng)站設(shè)計、成都網(wǎng)站制作,臨汾網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
rewrite
進行匹配跳轉(zhuǎn)if
匹配全局變量后跳轉(zhuǎn)location
匹配再跳轉(zhuǎn)if
全局變量匹配proxy_ pass
反向代理字符 | 說明 |
---|---|
^ |
匹配輸入字符串的起始位置 |
$ |
匹配輸入字符串的結(jié)束位置 |
* |
匹配前面的字符零次或多次 |
+ |
匹配前面的字符一次或多次 |
? |
匹配前面的字符零次或一次 |
. |
匹配除“\n”之外的任何單個字符。使用諸如"[.\n]"之 類的模式,可匹配包括“n”在內(nèi)的任意字符 |
\ |
將后面接著的字符標(biāo)記為一個特殊字符或一個原義字符或一個向后引用 |
\d |
匹配純數(shù)字 |
{n} |
重復(fù)n次 |
{n,} |
重復(fù)n次或更多次 |
[c] |
匹配單個字符c |
[a-z] |
匹配a-z小寫字母的任意一個 |
[a-zA-Z] |
匹配a-z小寫字母或A-Z大寫字母的任意一 個 |
rewrite <regex> <replacement> [flag];
<regex> //正則 <replacement> //跳轉(zhuǎn)后的內(nèi)容 [flag] //rewrite支持flag標(biāo)記
標(biāo)記 | 說明 |
---|---|
last |
相當(dāng)于Apache 的[L] 標(biāo)記,表示完成rewrite |
break |
本條規(guī)則匹配完成即終止,不再匹配后面的任何規(guī)則 |
redirect |
返回302 臨時重定向,瀏覽器地址會顯示跳轉(zhuǎn)后的URL 地址,爬蟲不會更新url |
permanent |
返回301 永久重定向,瀏覽器地址欄會顯示跳轉(zhuǎn)后的URL 地址,爬蟲更新url |
last | break | |
---|---|---|
使用場景 | 一般寫在server 和if 中 |
一般使用在location 中 |
URL 匹配 |
不終止重寫后的url 匹配 |
終止重寫后的url 匹配 |
location = patt {} [精準(zhǔn)匹配]
location patt {} [一般匹配]
location ~ patt {} [正則匹配]
標(biāo)記 | 說明 |
---|---|
~ |
執(zhí)行一個正則匹配,區(qū)分大小寫 |
~* |
執(zhí)行一個正則匹配,不區(qū)分大小寫 |
!~ |
執(zhí)行一個正則匹配,區(qū)分大小寫不匹配 |
!~* |
執(zhí)行一個正則匹配,不區(qū)分大小寫不匹配 |
^~ |
普通字符匹配;使用前綴匹配。如果匹配成功,則不再匹配其他location |
= |
普通字符精確匹配。也就是完全匹配 |
@ |
定義一個命名的location ,使用在內(nèi)部定向時 |
=
類型^~
類型表達式~
和~*
)類型rewrite
是在同一域名內(nèi)更改獲取資源的路徑location
是對一-類路徑做控制訪問或反向代理,還可以proxy_pass
到其他機器server
塊里面的rewrite
指令location
匹配location
中的rewrite
指令location =/ {
[ configuration A ] //精確匹配/,主機名后面不能帶任何字符串
}
location/ {
[ configuration B ] //所有的地址都以/開頭,這條規(guī)則將匹配到所有請求,但正則和最長字符串會優(yōu)先匹配
}
location /documents/ {
[ configuration C ] //匹配任何以/documents/開頭的地址,當(dāng)后面的正則表達式?jīng)]有匹配到時,才起作用
}
location ~ /documents/abc {
[ configuration D ] //匹配任何以/documents/abc開頭的地址當(dāng)后面的正則表達式?jīng)]有匹配到時,才會起作用
}
location ^~ /images/ {
[ configuration E ] //以/images/開頭的地址,匹配符合后,停止往下匹配
}
location ~* \.(gif|jipg|jpeg)$ {
[ configuration F ]
}
//匹配所有以gif,jpg或jpeg結(jié)尾的請求,/images/下的圖片會被[ configuration E ]處理,因為^~的優(yōu)先級更高
location /images/abc {
[ configuration G ] //最長字符匹配到/images/abc,優(yōu)先級最低
}
location ~ /images/abc {
[ configuration H ] //以/images/abc開頭的,優(yōu)先級次之
}
location /images/abc/1.html {
[ configuration I ] //如果和正則~ /images/abc/1.html相比,正則優(yōu)先級更高
}
location =
完整路徑) > (location ^~
完整路徑) > (location ~*
完整路徑) > (location ~
完整路徑) > (location
完整路徑) > (location /
)location=
目錄) > (location ^~
目錄/) > (location ~
目錄) > (location ~*
目錄) > (location
目錄) > (location /
)[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm //安裝nginx官方源碼包
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.ymMjSa: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@localhost ~]# yum install nginx -y //安裝nginx服務(wù)
已加載插件:fastestmirror, langpacks
已加載插件:fastestmirror, langpacks
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
...//省略部分內(nèi)容...
已安裝:
nginx.x86_64 1:1.16.1-1.el7.ngx
完畢!
[root@localhost ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf //配置文件路徑
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
[root@localhost ~]# vim /etc/nginx/conf.d/default.conf //編輯配置文件
server {
listen 80;
server_name www.aacp.com; //更改域名
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main; //更改日志文件名稱,并開啟功能
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
...//省略部分內(nèi)容...
:wq
[root@localhost ~]# yum install bind -y //安裝DNS功能
已加載插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.163.com
...//省略部分內(nèi)容...
已安裝:
bind.x86_64 32:9.11.4-9.P2.el7
...//省略部分內(nèi)容...
完畢!
[root@localhost ~]# vim /etc/named.conf
...//省略部分內(nèi)容...
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; };
...//省略部分內(nèi)容...
:wq
[root@localhost ~]# vim /etc/named.rfc1912.zones
...//省略部分內(nèi)容...
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
...//省略部分內(nèi)容...
:wq
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost accp.com.zone
[root@localhost named]# vim accp.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.144.133
:wq
[root@localhost named]# systemctl start named //啟動DNS服務(wù)
[root@localhost named]# systemctl start nginx //啟動nginx服務(wù)
[root@localhost named]# systemctl stop firewalld.service //關(guān)閉防火墻
[root@localhost named]# setenforce 0 //關(guān)閉增強性安全功能
[root@localhost named]# netstat -ntap | grep nginx //查看nginx服務(wù)是否開啟
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 2769/nginx: master
[root@localhost named]# vim /etc/nginx/conf.d/default.conf //編輯Nginx配置文件
...//省略部分內(nèi)容...
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location / {
if ( $host = "www.accp.com" ){
rewrite ^/(.*)$ http://www.kgc.com/$1 permanent;
//在location模塊中添加if判斷語句,當(dāng)輸入"www.accp.com"訪問網(wǎng)頁時跳轉(zhuǎn)到www.kgc.com中
}
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
...//省略部分內(nèi)容...
:wq
[root@localhost named]# vim /etc/named.rfc1912.zones
...//省略部分內(nèi)容...
zone "accp.com" IN {
type master;
file "accp.com.zone";
allow-update { none; };
};
zone "kgc.com" IN {
type master;
file "kgc.com.zone";
allow-update { none; };
};
...//省略部分內(nèi)容...
:wq
[root@localhost named]# cp -p accp.com.zone kgc.com.zone
[root@localhost named]# systemctl restart nginx
[root@localhost named]# systemctl restart named
[root@localhost conf.d]# vim default.conf //編輯配置文件
...//省略部分內(nèi)容...
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
set $rewrite true; //設(shè)置是否合法的IP標(biāo)記
if ($remote_addr = "192.168.144.128"){ //判斷是否為合法地址,如果為合法地址執(zhí)行操作
set $rewrite false;
}
if ($rewrite = true){ //判斷是否為非法地址,如果為非法地址則打上標(biāo)記,合法地址不做操作
rewrite (.+) /main.html;
}
location = /main.html { //匹配標(biāo)記,執(zhí)行跳轉(zhuǎn)
root /usr/share/nginx/html;
}
location / { //注意此處刪除上面設(shè)置的基于域名跳轉(zhuǎn)的配置條目
root /usr/share/nginx/html;
index index.html index.htm;
}
...//省略部分內(nèi)容...
:wq
[root@localhost conf.d]# cd /usr/share/nginx/html/ //進nginx服務(wù)站點
[root@localhost html]# ls //查看信息
50x.html index.html
[root@localhost html]# vim main.html //編輯跳轉(zhuǎn)的網(wǎng)頁內(nèi)容
<h2>this is test web</h2>
:wq
[root@localhost html]# systemctl restart nginx //重啟網(wǎng)站
http://bbs.accp.com
下面的發(fā)帖都跳轉(zhuǎn)到http://www.accp.com/bbs
,且域名跳轉(zhuǎn)后保持參數(shù)不變[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name bbs.aacp.com; //更改域名
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location /post { //設(shè)置匹配字段,字段匹配執(zhí)行跳轉(zhuǎn)操作
rewrite (.+) http://www.accp.com/bbs$1 permanent;
}
//注意刪除上面設(shè)置的基于客戶端IP訪問的跳轉(zhuǎn)的配置條目
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
...//省略部分內(nèi)容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone //編輯DNS服務(wù)區(qū)域數(shù)據(jù)文件
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
bbs IN A 192.168.144.133 //更改主機頭解析
:wq
[root@localhost html]# systemctl restart nginx //重啟nginx服務(wù)
[root@localhost html]# systemctl restart named //重啟DNS服務(wù)
http://www.accp.com/100-(100|200)-100.html
跳轉(zhuǎn)到http://www.accp.com
頁面[root@localhost html]# vim /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name www.aacp.com; //將服務(wù)器域名更改會www
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){ //刪除此處上面設(shè)置的基于新、舊域名跳轉(zhuǎn)并加目錄跳轉(zhuǎn)的配置條目
rewrite (.*) http://www.accp.com permanent; 并設(shè)置基于參數(shù)訪問時跳轉(zhuǎn)回主網(wǎng)頁
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
...//省略部分內(nèi)容...
:wq
[root@localhost html]# vim /var/named/accp.com.zone
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.144.133 //更改主機頭解析
:wq
[root@localhost html]# systemctl restart nginx //重啟nginx服務(wù)
[root@localhost html]# systemctl restart named //重啟DNS服務(wù)
http://www.accp.com/upload/1.php
跳轉(zhuǎn)到首頁[root@localhost html]# vim /etc/nginx/conf.d/default.conf //編輯配置文件
server {
listen 80;
server_name www.aacp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location ~* /upload/.*\.php$ { //刪除上面設(shè)置的匹配參數(shù)跳轉(zhuǎn)訪問,配置匹配所有php結(jié)尾訪問跳轉(zhuǎn)回主頁
rewrite (.+) http://www.accp.com permanent;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
...//省略部分內(nèi)容...
:wq
[root@localhost html]# systemctl restart nginx //重啟nginx服務(wù)
[root@localhost html]# vim /etc/nginx/conf.d/default.conf //編輯配置文件
server {
listen 80;
server_name www.aacp.com;
#charset koi8-r;
access_log /var/log/nginx/www.accp.com-access.log main;
location ~* ^/abc/123.html { //刪除上面的配置,并重新編輯以具體的某個頁面訪問網(wǎng)頁時跳轉(zhuǎn)回主頁
rewrite (.+) http://www.accp.com permanent;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
...//省略部分內(nèi)容...
:wq
[root@localhost html]# systemctl restart nginx //重啟nginx服務(wù)
當(dāng)前名稱:配置Nginx服務(wù)中Rewrite的應(yīng)用
URL地址:http://aaarwkj.com/article30/iioppo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、自適應(yīng)網(wǎng)站、網(wǎng)站排名、靜態(tài)網(wǎng)站、虛擬主機、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)