Nginx基于時(shí)間進(jìn)行跳轉(zhuǎn)
一、需求:
晚上6點(diǎn)到早上9點(diǎn),nginx默認(rèn)跳轉(zhuǎn)到一個(gè)正在維護(hù)的頁
二、創(chuàng)建維護(hù)頁面
2.1、查看nignx配置,查看到根目錄映射到/opt/module目錄
location / {
root /opt/module;
charset utf-8;
index index.html index.htm;
proxy_set_header Cookie $http_cookie;
autoindex off;
}
2.2、需要將維護(hù)頁面放到/opt/module目錄下,后綴名為html
mkdir /opt/module/weihu
vim /opt/module/weihu/index.html
<h1>系統(tǒng)維護(hù)中<h1>
三、nginx配置
server {
listen 7050;
server_name 10.255.33.30;
###修改字符值,解決中文亂碼情況(如果不能解決,在location也要加上)
charset utf-8;
###使用nginx的內(nèi)置變量獲取系統(tǒng)時(shí)間
if ( $time_local ~ "^(d+)/(w+)/(d+):(d+):(d+):(d+) +(d+)" ) {
###獲取到小時(shí)
set $hour $4;
}
set $flag true;
###匹配到09為真
if ( $hour ~ "09" ) {
set $flag true;
}
###匹配到10到18為真
if ( $hour = "^1[0-8]" ) {
set $flag true;
}
###當(dāng)這個(gè)變量不為真時(shí),重寫到維護(hù)頁面
if ( $flag = false ) {
rewrite (.+) /weihu/index.html;
}
四、網(wǎng)頁測試
