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 supplementConfigure 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:
|
<<: Minimalistic website design examples
>>: Solution to CSS flex-basis text overflow problem
Software Download Download software link: https:/...
To achieve the background color flashing effect, j...
1. Always use :key in v-for Using the key attribu...
Copy code The code is as follows: <style> ....
Table of contents Use of CURRENT_TIMESTAMP timest...
MySQL 5.7 installation We are learning MySQL data...
grammar background: linear-gradient(direction,col...
Suddenly when I logged into MySQL, it said that a...
Table of contents Preface 1. The database name or...
To use Nginx under Windows, we need to master som...
Table of contents 1. Introduction 2. Direct recov...
Copy code The code is as follows: <iframe src=...
1. Download mysql-8.0.17-winx64 from the official...
Table of contents text LOCK parameter ALGORITHM p...
1. Explanation of provide and inject Provide and ...