Nginx Rewrite common examples
I believe that everyone in the daily operation of the work if you use Nginx as the front-end reverse proxy server, you will be the rewrite of nginx and love and hate, love it is because you have done it, completed the developer's jump demand you will feel very cool, feel really strong, Hate it is because when some strange jump demand when you will not have the clue, a variety of debugging, the Internet praying are uncertain when really want to die of heart have, of course, there are many classic examples of nginx rewrite, but I feel that my work is not much help. Here are some examples of rewrite that I have encountered in my work. To share with you.
First, Access http://www.abc.com Normal, now need to visit abc.com, jump directly to http://www.abc.com.
- server {
- Listen 80;
- server_name abc.com;
- Access_log off;
- Rewrite (. +) $scheme://www. $host $ permanent;
- }
Second, if your domain name is http://www.abc.com, now want to change a domain name http://www.def.com, and do not want to lose the previous domain access volume, need to configure jump.
The first thing you need to do is write www.abc.com and www.def.cn in your server_name.
Then make the following configuration:
- Location/{
- if ($host = ' www.abc.com ')
- {
- Rewrite ^/(. *) $ http://www.def.com/$1 permanent;
- }
- }
Third, jump maintenance pages, such as today's maintenance, want to hang a maintenance page, I hope that in addition to certain specific IP address can be accessed normally, because the need to test. The rest of the IP addresses are Access maintenance pages, which I've written before, but it's a little different today. Add the following content to the server:
- Set $rewrite true;
- if ($remote _addr = "xxx.xxx.xxx.xxx") {
- Set $rewrite false;
- }
- if ($rewrite = True) {
- Rewrite (. +) http://www.abc.com/maintenance.html;
- }
- Location =/maintenance.html {
- Root/var/vhost;
- }
Four, for example, now your domain name is http://bbs.abc.com, now will be the domain name below the post access to Http://www.abc.com/bbs, pay attention to the parameters after the jump to remain consistent.
For example: http://bbs.abc.com/post/addpost/913.shtml jump to Http://www.abc.com/bbs/post/addpost/913.shtml
- Location/post {
- Rewrite (. +) http://www.abc.com/bbs$1 permanent;
- }
V. URI-based jumps
- if ($request _uri ~* ^/note\.php\?product_code= (. *) $) {
- Rewrite (. *) http://www.abc.com permanent;
- }
- if ($request _uri ~ ^/forum-(140|141|142|143|144|145|150|151|152|153|154|155|156|157|158|159|200|222|223|224|225| 226|227|228)-(\d+). html$) {
- Rewrite (. *) Http://www.abc.com/list permanent;
- }
Vi. jumps based on the file below the directory
- Location ~*/upload/.*\.php$ {
- return 404;
- }
Seven, the most common URL to the URL jump
- Location ~* ^/ab/maintain/main.html {
- Rewrite (. +) http://www.abc.com/maintain/main.html permanent;
- }
Although examples are not exhaustive, there may be some other better jump examples to add later. There is no place to welcome everyone to shoot bricks.
http://www.bkjia.com/PHPjc/1119674.html www.bkjia.com true http://www.bkjia.com/PHPjc/1119674.html techarticle Nginx Rewrite Common examples believe that everyone in the daily operation and maintenance work if you use Nginx as the front-end reverse proxy server, you will be the rewrite of Nginx love and Hate, love it is because you engage in ...