Nginx URL Rewrite

Source: Internet
Author: User

Nginx rewrite to achieve level two domain name jump
When accessing http://www.jbyuan.com jump to http://www.jbyuan.com/nvxingjiankang/

Method One:
This way the browser address will change www.jbyuan.com/nvxingjiankang/
Implementation access is as follows:

Copy CodeThe code is as follows: server {
Listen 80;
server_name www.test.com;
Location/{
Root/data/test;
Index index.html;
}
}
server {
Listen 80;
server_name *.test.com;
if ($http _host ~* "^ (. *) \.test\.com$") {
Set $domain $;
Rewrite ^ (. *) http://www.test.com/test/$domain/break;
}
}

Method Two,
When accessing http://www.jbyuan.com jump to http://www.jbyuan.com/nvxingjiankang/

Copy CodeThe code is as follows: server {
Listen 80;
server_name *.test.com;
root/usr/local/www;
#这是里可以加多个目录, if you do not add directories, you will not be able to access files in the http://www.jbyuan.com/nvxingjiankang/directory,/images
Location ~ ^/(test|images|styles)/
{
Proxy_redirect off;
Proxy_set_header Host www.test.com;
Proxy_pass http://192.168.1.2:8080;
}
Location/{
Set $domain default;
if ($http _host ~* "^ (. *) \.test\.com$") {
Set $domain $;
}
Rewrite ^/(. *)/test/$domain/$1 last;
}
Access_log off;
}

Rewrite command
Nginx rewrite is equivalent to Apache Rewriterule (in most cases you can use the original Apache rewrite rules in quotation marks can be used directly), it can be used in the server,location and if conditions to determine the block, life

The order format is as follows:
Rewrite regular expression replaces the target flag flag
The flag tag can be in several formats:
Last-basically use this flag.
Break-Abort Rewirte, do not continue matching
Redirect-returns the HTTP status of temporary redirection 302
Permanent-Returns the HTTP status of permanent redirection 301
For example, the following setting Nginx redirects the files below a directory to another directory, with the corresponding string in the second parenthesis (. *):

Copy CodeThe code is as follows: location/download/{
Rewrite ^ (/download/.*)/m/(. *) \. *$ $1/nginx-rewrite/$2.gz break;
}

If condition judgment of nginx redirection
In the case of server and location two, you can use Nginx if condition to judge, the condition can be the following several kinds:
Regular expressions

Such as:
Match judgment
~ Case-sensitive matching;!~ is case-sensitive
~* for case-insensitive matching;!~ for case insensitive

is when the user input www.a.com.cn automatically jump to www.a.com this domain name:

Rewrite ^/(. *) $ http://www.a.com/$1 permanent; or CNAME
For example, the following setting Nginx is redirected to the/nginx-ie directory using IE's use of the user:
if ($http _user_agent ~ MSIE) {
Rewrite ^ (. *) $/nginx-ie/$1 break;
}
File and directory Judgments
-F and!-f determine if a file exists
-D and!-d determine if a directory exists
-E and!-e determine if a file or directory exists
-X and!-x determine if the file is executable

For example, the following setting Nginx redirects when the file and directory does not exist:

Copy CodeThe code is as follows: if (!-e $request _filename) {
Proxy_pass http://127.0.0.1;
}

Return
Return HTTP code, such as setting the Nginx anti-theft chain:

Copy CodeThe code is as follows: Location ~* \. (gif|jpg|png|swf|flv) $ {
Valid_referers none blocked www.jefflei.comwww.leizhenfang.com;
if ($invalid _referer) {
return 404;
}
}

Remember a regular, match a non-word
Because you want to rewrite an address from
/mag/xx/xxx/-/m/xxx
But the original/mag/xx/more/to keep
This will have to write a more peculiar regular, tried a lot of writing and did not succeed

The first thing to think about is:

Copy CodeThe code is as follows: Location ~* ^/mag/[^/]+/[^ (more)]+/{
Rewrite ^/mag/[^/]+/(. *)/m/$1 permanent;
}

[] The writing does not work, the inside is matched with a single character, so invalid, not match

Or shooting Ferguson forever classmate Good, research in-depth, to provide a non-word of the writing (?!) More

Copy CodeThe code is as follows: Location ~* ^/mag/[^/]+/(?! more) ([^/]+)/{
Rewrite ^/mag/[^/]+/(. *)/m/$1 permanent;
}

The writing was barely manageable, and the matching unit behind it was not perfect, but it was able to deal with all of my requirements.
You can refer to this notation if necessary.

Common grouping syntax

Capture
(exp) matches exp, and captures text into an automatically named group
(? exp) to match exp, and capture text to a group named name, you can also write (? ' Name ' exp ')
(?: EXP) matches exp, does not capture matching text, and does not assign group numbers to this group
(? =exp) matches the position of the exp front
(? <=exp) matches the position after exp
(?! EXP) match the location followed by the EXP
(? <!exp) matches a position that is not previously exp

Why use 301 Redirects

Web site in the construction of the need to redirect a lot of situations: such as web directory structure changes, page renaming, the extension of the page changes, website domain name changes. If you do not redirect, the user's collection and the old address in the search engine database can only let the visitor get a 404 error message page, access traffic lost in vain. Not only that, all the previous accumulation of the page (such as PR value) is in vain.

301 redirects not only enable the page to automatically jump, for the search engine, may also be able to pass the PR value.

For more information on nginx redirection rules , please refer to: http://www.jbxue.com/article/2186.html

301 Redirect Method

301 redirects were made to merge www. jefflei.com and jefflei.com and merge the previous domain names together. There are two implementation methods, the first method is to determine the NGINX core variable host (the old version is Http_host):

Copy CodeThe code is as follows: server {
server_name www.jefflei.com jefflei.com;
if ($host! = ' www.jefflei.com ') {
Rewrite ^/(. *) $ http://www.jefflei.com/$1 permanent;
}
...
}

The second method:

Copy CodeThe code is as follows: server {
server_name jefflei.com;
Rewrite ^/(. *) http://www.jefflei.com/$1 permanent;
}

Test the first method OK, in both methods, permanent is the key, detailed description of the Nginx redirection rule description.

last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of permanent redirection 301

OK, now you can check the results, here you can see the HTTP header information returned:
Http://www.seoconsultants.com/tools/headers.asp

The second method did not test successfully ...

Test whether the orientation is successful
http://qinfy.net/301-redirect-for-nginx/

Input instruction ~
/usr/local/nginx/sbin/nginx-t

Tips:
The configuration file/usr/local/nginx/conf/nginx.conf syntax is OK
Configuration file/usr/local/nginx/conf/nginx.conf test is successful

Test successful-Restart nginx~ input command ~
/usr/local/nginx/sbin/nginx-s Reload

After the restart test ~ Whether the successful setting is complete! Input instruction ~
Curl-i imcat.tk

Will output:
http/1.1 301 Moved Permanently
server:nginx/0.7.65
Date:tue, 01:12:37 GMT
Content-type:text/html
content-length:185
Connection:keep-alive
location:http://qinfy.net/


Nginx URL Rewrite

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.