地址重寫
地址重寫語法——
關于Nginx服務器的地址重寫,主要用到的配置參數是rewrite
語法格式:
rewrite regex replacement flag
rewrite 舊地址?? 新地址??? [選項]
地址重寫步驟:
#修改配置文件(訪問a.html重定向到b.html)
cd /usr/local/nginx/
cp conf/nginx.conf.default conf/nginx.conf? #還原配置文件
cp: overwrite 'conf/nginx.conf'? y
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen?????? 80;
server_name? localhost;
rewrite? /a.html? /b.html;?????? #新添加地址重寫,a.html重定向到b.html ?
...
location / {
root?? html;
index? index.html index.htm;
}
}
echo "nginx-B~~" > /usr/local/nginx/html/b.html
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試
http://192.168.88.5/a.html????????? #內容顯示的是nginx-B~~,但是地址欄沒有發生變化,還是a.html頁面
此時配置文件中直接寫rewrite? /a.html? /b.html; 配置,在測試是其實會有些問題,比如在瀏覽器中訪問時把192.168.88.5/a.html寫成192.168.88.5/a.htmldc 或者寫成 192.168.88.5/dc/a.html,訪問都會正常顯示b.html的頁面,這是因為此時寫的是只要包含a.html的都會跳轉,沒有進行精準匹配,可以進行以下修改,只有寫a.html時才會正確跳轉
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
??????? listen?????? 80;
server_name? localhost;
rewrite? ^/a\.html$? /b.html;?????? #新添加地址重寫,a.html重定向到b.html ?
...
/usr/local/nginx/sbin/nginx? -s? reload
瀏覽器重新訪問測試即可192.168.88.5/a.html,顯示b.html頁面內容
測試重定向——
#redirect? 臨時重定向,狀態碼302
#permanent 永久重定向,狀態碼301
#修改Nginx服務配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
??????? listen?????? 80;
server_name? localhost;
rewrite ^/a\.html$? /b.html? redirect;????? #新修改,redirect重定向,測試完之后把redirect換成permanent,是一樣的效果
...
location / {
root?? html;
index? index.html index.htm;
}
}
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
瀏覽器測試,地址欄同時發生變化
http://192.168.88.5/a.html? #內容顯示的是nginx-B~~,地址欄發生變化,是b.html頁面
不同網站間跳轉——
#修改Nginx服務配置實現訪問192.168.88.5的請求重定向至www.tmooc.cn
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen?????? 80;
server_name? localhost;
rewrite /? http://www.tmooc.cn/;??????? #新修改,訪問舊網站的任意內容都跳轉到新網站
location / {
root?? html;
index? index.html index.htm;
}
}
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試
http://192.168.88.5???? #可以成功跳轉
子頁面重定向——
#修改配置文件(訪問192.168.88.5/下面子頁面,重定向至www.tmooc.cn/下相同的子頁面)
vim /usr/local/nginx/conf/nginx.conf
...
server {
listen?????? 80;
server_name? localhost;
rewrite? /(.*)? http://www.tmooc.cn/$1;???? #新修改
location / {
root?? html;
index? index.html index.htm;
}
}
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試
http://192.168.88.5/b.html????? #成功跳轉
實現不同瀏覽器跳轉到不同頁面——
#創建網頁目錄以及對應的頁面文件:
mkdir? html/firefox
echo? firefox~~? >? html/firefox/abc.html?? #火狐專用頁面
cho? others~~? >? html/abc.html??????????? #其他瀏覽器專用頁面
火狐訪問192.168.88.5/abc.html時可以看到html/firefox/abc.html里面內容
其他瀏覽器訪問192.168.88.5/abc.html時可以看到html/abc.html里面內容
#修改Nginx服務配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen?????? 80;
server_name? localhost;
if ($http_user_agent ~* firefox) {? #如果用戶使用了火狐瀏覽器就進行地址重寫,讓用戶看到火狐專用頁面,否則就是其他頁面
rewrite /(.*)? /firefox/$1;???? #$http_user_agent是nginx的內置變量,包含了發起 HTTP 請求的客戶端的用戶代理(User-Agent)字符串,比如用的什么瀏覽器
}
location / {
root?? html;
index? index.html index.htm;
}
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試
用火狐瀏覽器與其他瀏覽器訪問相同地址192.168.88.5/abc.html,可以得到不同結果
火狐瀏覽器訪問192.168.88.5/abc.html,得到結果firefox~~
其他瀏覽器訪問192.168.88.5/abc.html,得到結果others~~
其他選項測試——
#last 不再讀其他語句,但還會繼續匹配其他location語句
#break 不再讀其他語句,結束請求
測試last不再讀其他語句
#修改Nginx服務配置
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen?????? 80;
server_name? localhost;
rewrite /a.html /b.html;??????? #新修改
rewrite /b.html /c.html;??????? #新修改
...
}
...
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
echo nginx-c~~ > html/c.html
#瀏覽器測試
192.168.88.5/a.html #內容顯示的是nginx-c~~
#如果想要訪問的是b.html的內容,可以做以下更改
vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
listen?????? 80;
server_name? localhost;
rewrite /a.html /b.html last;?????? #新修改
rewrite /b.html /c.html;
...
}
...
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#瀏覽器測試訪問
192.168.88.5/a.html #內容顯示的是nginx-b~~
測試last會繼續匹配其他location語句
vim /usr/local/nginx/conf/nginx.conf
...
location / {??????????????? #此處為默認的location
rewrite /a.html /b.html last;?? #新添加
root?? html;
index? index.html index.htm;
}
location /b.html {??????????????? #這里是新添加的location
rewrite /b.html /c.html;
}
...?????? ?
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試: http://192.168.88.5/a.html,顯示為nginx-c~~
break 不再讀其他語句,結束請求
vim /usr/local/nginx/conf/nginx.conf
...
location / {
rewrite /a.html /b.html break;??????? #break可以阻止后面的語句
root?? html;
index? index.html index.htm;
}
location /b.html {
rewrite /b.html /c.html;
}
...?????? ?
#重新加載配置文件
/usr/local/nginx/sbin/nginx? -s? reload
#客戶端測試: http://192.168.88.5/a.html,顯示為nginx-b~~