1, rewrite syntax:
Command syntax: rewrite regex replacement [flag];
Default: none
Application Location: server, location, if
rewrite is important to realize URL redirection command, he jumps to the replacement to match the content according to regex (regular expression), marking the end of a flag
Simple small example:
1
|
rewrite ^/(.*) http:
//www
.baidu.com/ permanent;
# 匹配成功后跳转到百度,执行永久301跳转
|
Common regular expression:
character | description |
\ | The latter is then marked as a character or a special character or a literal character back-reference |
^ | Matches the input character string starting position |
$ | Matches the input end of the string |
* | Matches the preceding character zero or more times |
+ | Matching one or more times before the string |
? | Matching string preceding zero or one |
. | All matches single character except "\ n" in |
(pattern) | Pattern matching in brackets |
rewrite the last one flag parameters:
Mark symbol | Explanation |
last | After the completion of this rule match to continue down the new location URI matching rules |
break | After the completion of the termination of this rule match, not match any of the rules |
redirect | Returned 302 temporary redirect |
permanent | Returned 301 permanent redirect |
2, application scenarios:
- Adjust the user to browse URL, look norms
- To make search engines site content, allowing users to experience better
- After replacing the new website domain name
- Depending on the particular variables, catalog, client information Jump
3, commonly used 301 Jump:
Before we did it address access the same resources in different virtual host by way of using aliases, and now we can use a better way to do this, and that is the way to jump
Or use www.brian.com web hosting as an example, modify the configuration file brian.conf:
[root@Nginx www_date]# cat brian.conf server { # 添加个server区块做跳转 listen 80; server_name brian.com; rewrite ^/(.*) http://www.brian.com/$1 permanent; } server { listen 80; server_name www.brian.com; location / { root html/brian; index index.html index.htm; } access_log logs/brian.log main gzip buffer=128k flush=5s; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
Check grammar:
[root@Nginx conf]# [root@Nginx conf]# ../sbin/nginx -t nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx//conf/nginx.conf test is successful
GR:
1
|
[root@Nginx conf]
# ../sbin/nginx -s reload
|
windows test results:
4, domain jump:
Not only can we do the same virtual host resource domain jump, you can do different virtual host name Jump, jump on our next visit brian.com when www.baidu.com domain name to jump to the page:
Modify www.brian.com brian.conf virtual host configuration file:
[root@Nginx www_date]# cat brian.conf server { listen 80; server_name brian.com; location / { root html/brian; index index.html index.htm; } if ( $http_host ~* "^(.*)") { set $domain $1; rewrite ^(.*) http://www.baidu.com break; } access_log logs/brian.log main gzip buffer=128k flush=5s; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
windows test access brian.com :( Go to the www.baidu.com)