At the end of last week, the brothers project was going to expand the server to provide better services , Brother project has some functions that are provided to me in real time , I need to temporarily block the corresponding system functions , Because use nginx, So you can configure it directly nginx Redirect to fixed system maintenance page .
nginx Redirection is actually very simple , use return or rewrite All keywords are OK , Because retargeting jumps back directly to the static page , No subsequent operations and records are required , So direct 301 Permanent redirection .
Redirection can be done in server Middle configuration , It can also be in specific location Middle configuration , Here is a brief introduction .
stay server Middle configuration :
http { server{ listen 80; server_name A.com; # following return or rewrite Just choose one of them . among upgrade.html It's a reminder 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
}
}
}
perhaps stay location Middle configuration :
http { server{ listen 80; server_name A.com;
location / { rewrite ^/(.*)$ http://B.com/upgrade.html permanent;
# The following configuration content is omitted here
}
}
}
It can be seen from the above examples that ,return use 301 Parameter redirection ,rewrite use permanent( Of course, it can also be used break,last, If you want to make a difference, look up the information yourself ).
I don't know if you have found , In the above two examples , It's all used A.com To redirect to B.com , I tried , use A.com Redirect directly to A.com/upgrade.html, It will report an error and repeat too many times , It's going into a dead cycle . It can be configured to manage multiple domain names at the same time A Redirect B, But if there is only one domain name A How to do that ?
This is the time to use if The conditions are right , Here we are in server For example, the configuration in the :
http { server{ listen 80; server_name A.com; # Be careful if There must be a space after !!! if ($request_uri !~ "/upgrade.html$") { return 301 http://A.com/upgrade.html; } location / { # The following configuration content is omitted here } } }
The above examples show that , When the access path does not contain /upgrade.html And then redirect to upgrade.html, At this point, you can redirect , There won't be too many repetitions , But there's another problem , Namely upgrade.html The picture in cannot be displayed , There's no time to study how to avoid redirection of images , I'll add later .
test if conditional , I met a special pit , Is to add if Restart after nginx Report errors :
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
Input systemctl status nginx.service You can view error messages , among nginx: [emerg] unknown directive "if($request_uri" Error finding the answer , Turned out to be if There must be a space after it !!!!, It's too much , The introductions on the Internet nginx if None of my articles mentioned such important information ...
Thank you for the information :
if There must be a space after :https://blog.csdn.net/palet/article/details/103394236
nginx in return and rewrite:https://blog.csdn.net/u010982507/article/details/104025717