nginx掛維護頁面

本篇文章摘抄於他人的文章,來自於CSDN的JeremyIT同窗,但我仍是本身從新敲一遍。html

需求1:訪問網站的任何頁面,都跳轉到同一個頁面,而這一個頁面就是維護頁面。(外部用戶和公司內部的人都跳轉到維護頁面)nginx

server {
    listen 80;
    index index.html index.htm;

    server_name www.example.com;

if ($request_uri !~ "^/maintain.html$") { rewrite ^(.*) http://www.example.com/maintain.html permanent; } location / { ... } }

 

需求2:測試

(1) 不能關閉全部頁面的訪問,對於某些重要頁面仍是得開放給外部用戶,好比充值頁面等。網站

(2) 對於新上線的功能,咱們只是不但願外部訪問到,可是咱們公司內部得能訪問,這樣就能進行測試,測試完後再對外開放。spa

server {
    listen 80;

    server_name www.example.com;
     
    set $flag 0;

    if ($remote_addr !~ "192.168.198.2") {
        set $flag "${flag}1";
    }

    if ($request_uri !~* ^(/maintain.html|/pay/index.html)$) {
        set $flag "${flag}2";
    }

    if ($flag = "012") {
        rewrite ^(.*) http://192.168.198.131/maintain.html permanent;
    }

    location / {
        ...
    }
}

對於公司內部(192.168.198.2)來講,咱們能夠訪問一切位置。.net

對於外部人員來講,若是咱們訪問的頁面不是/maintain.html或者/pay/index.html時,那麼會強制跳轉到/maintain.html頁面。code

等同於說,對外部人員開放的只有/maintain.html和/pay/index.html頁面,其餘業務無權訪問。server

這樣就實現了需求。htm

備註:我在測試時,始終沒有繞過彎,一直卡在對於外網地址,分爲跳轉和不跳轉,結果一直沒寫出來。其實應該換一個思惟,那就是哪些對外開放,哪些對外不開放。blog

開放的只有/maintain.html和/pay/index.html,其餘都不開放。

 

需求3::當用戶訪問咱們的網站,出現502狀態碼時,nginx自動跳到個性化頁面,而不是顯示冰冷的502數字。

server {
     listen 80;
     server_name www.example.com;

     # ... 省略掉 N 行代碼


     error_page 502 = @tempdown;

     location @tempdown {
         rewrite ^(.*)$ /custom.html break;
     }
}
相關文章
相關標籤/搜索