nginx挂维护页面

本篇文章摘抄于他人的文章,来自于CSDN的JeremyIT同学,但我还是自己重新敲一遍。

需求1:访问网站的任何页面,都跳转到同一个页面,而这一个页面就是维护页面。(外部用户和公司内部的人都跳转到维护页面)

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) 对于新上线的功能,我们只是不希望外部访问到,但是我们公司内部得能访问,这样就能进行测试,测试完后再对外开放。

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)来说,我们可以访问一切位置。

对于外部人员来说,如果我们访问的页面不是/maintain.html或者/pay/index.html时,那么会强制跳转到/maintain.html页面。

等同于说,对外部人员开放的只有/maintain.html和/pay/index.html页面,其他业务无权访问。

这样就实现了需求。

备注:我在测试时,始终没有绕过弯,一直卡在对于外网地址,分为跳转和不跳转,结果一直没写出来。其实应该换一个思维,那就是哪些对外开放,哪些对外不开放。

开放的只有/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;
     }
}

 

posted @ 2017-05-15 17:27  道霖  阅读(11194)  评论(2编辑  收藏  举报