Configure nginx redirection to system maintenance page

  • 2021-11-01 05:25:28
  • OfStack

Last weekend, Brother Project prepared to expand the server to provide better service. Some functions of Brother Project were provided to me in real time, so I need to temporarily block the corresponding system functions. Because nginx is used, nginx can be directly configured to redirect to the fixed system maintenance page.

nginx redirection is actually very simple, and return or rewrite keywords can be used. Because after redirection, you can jump directly to a static page without subsequent operations and records, so you can directly redirect 301 permanently.

Among them, redirection can be configured in server or specific location, which are briefly introduced below.

Configure in server:


http {
    server{
        listen 80;
        server_name A.com;

           #  The following return  Or  rewrite  Select which 1 Just one. Among them upgrade.html  It is a prompt page written by myself 
        return 301 http://B.com/upgrade.html;  
        # rewrite ^/(.*)$ http://B.com/upgrade.html permanent;
        location / {            #  The following configuration content is omitted here  }    } }

Or configure in location:


http {
    server{
        listen 80;
        server_name A.com;
        location / {
            rewrite ^/(.*)$ http://B.com/upgrade.html permanent;
             #  The following configuration content is omitted here  }    } }

From the above examples, it can be seen that return is redirected with 301 parameters, and rewrite is redirected with permanent (of course, break and last can also be used, if different, check the data by yourself).

I don't know if you have noticed that in the above two examples, A. com is used to redirect to B. com. I tried to redirect to A. com/upgrade. html directly with A. When managing multiple domain names at the same time, you can configure A to redirect B, but what if there is only one domain name A?

At this time, if condition judgment is used. Here, we take the configuration in server as an example:


http {
    server{
        listen 80;
        server_name A.com;        

        #  Attention  if  There must be one in the back 1 A space! ! ! 
        if ($request_uri !~ "/upgrade.html$") {
            return 301 http://A.com/upgrade.html;
        }

        location / {
                #  The following configuration content is omitted here 
           }
     } 
}

The above example shows that when the access path does not contain/upgrade. html, it will be redirected to upgrade. html, which can be redirected at this time without prompting too many repetitions, but there is another problem, that is, the pictures in upgrade. html cannot be displayed, so there is no time to study how to avoid the pictures being redirected for the time being, and there is time to supplement them later.

When testing if conditions, I encountered a special pit, that is, after adding if, I restarted nginx to report an error:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.

Enter systemctl status nginx. service to view the error message, where nginx: [emerg] unknown directive "if ($request_uri" error finds the answer, it turns out that there must be 1 space after if! ! ! ! It's too bad. Those articles about nginxif on the Internet don't mention such important information. . .

Thank you for your information:

There must be a space after if: https://blog.csdn.net/palet/article/details/103394236

return and rewrite in nginx: https://blog.csdn.net/u010982507/article/details/104025717

Supplement of knowledge points

Configure nginx to enter any address and jump to the maintenance page

Note 1: Configure nginx. Enter any address and jump to the maintenance page


server {
    listen 80;
    root /xxx/xxx/src;
    index index.html index.htm;

    server_name test.xxx.com;

    set $flag 0;
      if ($request_uri !~ "(/static/.*)$"){
          set $flag "${flag}1";
      }
      if ($request_uri !~ "/502.html$" ){
          set $flag "${flag}2";
      }
      if ($flag = "012") {
         rewrite ^(.*) http://test.xxx.com/502.html permanent;
      }

 location /{
     ...

The above is the nginx redirection to the system maintenance page details, more information about the nginx redirection maintenance page please pay attention to other related articles on this site!


Related articles: