Configure nginx to redirect to the system maintenance page

Configure nginx to redirect to the system maintenance page

Last weekend, a brother project was preparing to expand the server in order to provide better services. Some functions of the brother project were provided to me in real time, and I needed to temporarily block the corresponding system functions. Because I used nginx, I could directly configure nginx to redirect to the fixed system maintenance page.

Nginx redirection is actually very simple. You can use either the return or rewrite keyword. Because the redirection directly jumps to the static page, no subsequent operations or records are required, so a direct 301 permanent redirection is used.

Redirection can be configured in the server or in a specific location. The following is a brief introduction to each.

Configure in server:

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

    # Choose one of return or rewrite below. Among them, upgrade.html is the prompt page I wrote 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, we can see that return uses 301 parameter redirection, and rewrite uses permanent (of course, you can also use break and last. If you want to know the difference, please look up the information 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 have tried using A.com to redirect directly to A.com/upgrade.html, and it will report an error message saying that there are too many repetitions, which means it enters an infinite loop. 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, the if condition is used for judgment. Here we take the configuration in the server as an example:

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

        # Note that there must be a space after if! ! !
        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 redirect to upgrade.html. At this time, the redirection can be successful, and there will be no prompt for too many repetitions. However, there is another problem, that is, the pictures in upgrade.html cannot be displayed. I don’t have time to study how to avoid the pictures being redirected. I will add more later when I have time.

When testing the if condition, I encountered a particularly pitfall, that is, after adding the if and restarting nginx, an error message appeared:

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 information, including nginx: [emerg] unknown directive "if($request_uri" error. I found the answer. It turns out that there must be a space after if! ! ! ! It's too bad. The articles introducing nginxif on the Internet did not mention such important information...

Thanks for the 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

Knowledge points supplement

Configure nginx to redirect to the maintenance page when entering any address

Note: Configure nginx to redirect to the maintenance page when entering any address

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 details of nginx redirection to the system maintenance page. For more information about nginx redirection maintenance page, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to redirect nginx directory path
  • Detailed explanation of nginx configuration URL redirection-reverse proxy
  • Detailed explanation of redirection function in Nginx
  • How to set up 301 redirection in ngin configuration and 301 redirection in nginx subdirectory

<<:  Minimalistic website design examples

>>:  Solution to CSS flex-basis text overflow problem

Recommend

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

How to authorize remote connections in MySQL in Linux

Note: Other machines (IP) cannot connect to the M...

Learn SQL query execution order from scratch

The SQL query statement execution order is as fol...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Continuous delivery using Jenkins and Docker under Docker

1. What is Continuous Delivery The software produ...

jQuery implements shopping cart function

This article example shares the specific code of ...

How to install MySQL Community Server 5.6.39

This article records the detailed tutorial of MyS...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

CenterOS7 installation and configuration environment jdk1.8 tutorial

1. Uninstall the JDK that comes with centeros fir...

Vue implements carousel animation

This article example shares the specific code of ...

Solution to the problem that the Vue page image does not display

When making a new version of the configuration in...

Preventing SQL injection in web projects

Table of contents 1. Introduction to SQL Injectio...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

How to use docker to deploy spring boot and connect to skywalking

Table of contents 1. Overview 1. Introduction to ...

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...