Example tutorial on pan domain configuration in Nginx

  • 2020-05-10 23:32:33
  • OfStack

I was planning to change the Linux server, so I need to use the static page to temporarily replace 1 for the data migration. id=xxx all have to be redirected to the static file article/ xxx.htm, nginx rewrite is much more powerful than apache, if statement can also be used, it is easy to understand, let's see how Nginx Rewrite!
Tip: the following rewrite directive is written in the nginx.conf configuration of server {... }
Most common: static addresses redirect to dynamic addresses with arguments


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

Conversely: the dynamic address with parameters is redirected to the static address


if ($query_string ~* id=(.*)) {
set $id $1;
rewrite "^(.*)/article.asp$" $1/article/$id.htm last;
}

Generic domain name

Suppose the directory structure of the site is
html
├ ─ ─ bbs
└ ─ ─ www
html is the default path to source code in the nginx installation directory.
bbs is the forum program source code path
www is the home page program source code path
Put the corresponding program into the above path through
http: / / www. youdomain. com access is the home page
http: / / bbs. yourdomain. com access is BBS
Other level 2 domain name analogy.


server_name www.w3cgroup.com *.w3cgroup.com;
server_name_in_redirect off;
# Set the default root
set $rootdir /usr/local/nginx/html/w3cgroup/;
# matching 3 Level of the domain name 
if ($host ~* ^([^/.]+)/.([^/.]+)/.([^/.]+)/.([^/.]+)$) {
set $rootdir /usr/local/nginx/html/w3cgroup/$2/$1;
#3 Level domain has access to the specified directory is redirected to the corresponding 2 Level under the domain name 
rewrite "^.+upload/?(.*)$" http://upload.w3cgroup.com/$1 permanent;
rewrite "^.+ijc/?(.*)$" http://ijc.w3cgroup.com/$1 permanent;
break;
}
# matching 2 Level of the domain name 
if ($host ~* ^([^/.]+)/.([^/.]+)/.([^/.]+)$) {
set $rs1 $1;
}
# Set up the www when root
if ($rs1 ~* ^www$) {
set $rootdir /usr/local/nginx/html/platform_ig/;
#2 Level domain has access to the specified directory is redirected to the corresponding 2 Level under the domain name , Notice, we're going to use it here last
rewrite "^.+upload/?(.*)$" upload/$1 last;
rewrite "^.+ijc/?(.*)$" ijc/$1 last;
break;
}
# Set up the www2 Level domain root
if ($rs1 !~* ^www$) {
set $rootdir /usr/local/nginx/html/w3cgroup/$rs1;
#2 Level domain has access to the specified directory is redirected to the corresponding 2 Level under the domain name 
rewrite "^.+upload/?(.*)$" http://upload.w3cgroup.com/$1 permanent;
rewrite "^.+ijc/?(.*)$" http://ijc.w3cgroup.com/$1 permanent;
break;
}
# application 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 is equivalent to the [L] tag in Apache, indicating the completion of rewrite break terminates the match and no longer matches the following rule redirect returns 302 temporary redirection permanent returns a 301 permanent redirect

Nginx regular expression matching

~ matches for case sensitivity ~* is case insensitive ! ~ and! ~* are case sensitive mismatches and case insensitive mismatches, respectively

Nginx file and directory matching

  - f and! -f is used to determine if a file exists - d and! -d is used to determine if a directory exists - e and! -e is used to determine if a file or directory exists - x and! -x is used to determine whether a file is executable

Nginx global variable


$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


Related articles: