Nginx Rewrite implementation of two-level domain name, three-level domain name, generic domain name, path rewrite

Source: Internet
Author: User

Most common: Static addresses are redirected to dynamic addresses with parameters

Rewrite "^ (. *)/service/(. *) \.html$" $1/service.php?sid=$2 permanent;

In turn: Dynamic addresses with parameters redirected to static addresses

if ($query _string ~* id= (. *)) {
Set $id $;
Rewrite "^ (. *)/article.asp$" $1/article/$id. htm last;
}


Pan Domain Name Resolution

View Plaincopy to Clipboardprint?
server_name www.w3cgroup.com *.w3cgroup.com;
Server_name_in_redirect off;
#设置默认root
Set $rootdir/usr/local/nginx/html/w3cgroup/;

#匹配三级或三级以上的域名

if ($host ~* ^ (. +) \. ( [^\.] +)\. ([^\.] +)\. ([^\.] +)$)  {

}
#匹配三级域名
if ($host ~* ^ ([^\.] +)\. ([^\.] +)\. ([^\.] +)\. ([^\.] +)$) {
#三级域名中有访问指定的目录则重定向到相应的二级域名下
Rewrite "^.+upload/? (. *) $ "http://upload.w3cgroup.com/$1 permanent;
Rewrite "^.+ijc/? (. *) $ "http://ijc.w3cgroup.com/$1 permanent;
Break
}
#匹配二级域名
if ($host ~* ^ ([^\.] +)\. ([^\.] +)\. ([^\.] +)$) {
Set $rs 1 $;
}
#设置www时root
if ($rs 1 ~* ^www$) {
Set $rootdir/usr/local/nginx/html/platform_ig/;
#二级域名中有访问指定的目录则重定向到相应的二级域名下, notice that you use the last
Rewrite "^.+upload/? (. *) $ "upload/$1 last;
Rewrite "^.+ijc/? (. *) $ "ijc/$1 last;
Break
}
#设置非www二级域名时root
if ($rs 1!~* ^www$) {
Set $rootdir/usr/local/nginx/html/w3cgroup/$rs 1;
#二级域名中有访问指定的目录则重定向到相应的二级域名下
Rewrite "^.+upload/? (. *) $ "http://upload.w3cgroup.com/$1 permanent;
Rewrite "^.+ijc/? (. *) $ "http://ijc.w3cgroup.com/$1 permanent;
Break
}
#应用root
Root $rootdir;
Index index.php index.html;
Error_page 404 http:/$host/;

Note: A space is required between if () {}, otherwise nginx.conf will report unknow directive Error!

Reference:

Nginx Rewrite Flags
* Last equivalent to the [L] mark in Apache, indicating completion of rewrite
* Break terminates the match and no longer matches the following rule
* REDIRECT Return 302 temporary redirect
* Permanent return 301 Permanent redirect

Nginx Regular expression matching
* ~ For case-sensitive matching
* ~* for case-insensitive matching
*!~ and!~* are case insensitive and case insensitive

Nginx File and directory matching
*-F and!-f to determine if a file exists
*-D and!-d to determine if a directory exists
*-E and!-e to determine if a file or directory exists
*-X and!-x to determine if the file is executable

Nginx Global Variables
View Plaincopy to Clipboardprint?
$args
$content _length
$content _type
$document _root
$document _uri
$host
$http _user_agent
$http _cookie
$limit _rate
$request _body_file
$request _method
$remote _addr
$remote _port
$remote _user
$request _filename
$request _uri
$query _string
$scheme
$server _protocol
$server _addr
$server _name
$server _port
$uri

Nginx Rewrite implementation of two-level domain name, three-level domain name, generic domain name, path 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.