(1)location = / {}
=为精确匹配 / ,主机名后面不能带任何字符串,比如访问 / 和 /data,则 / 匹配,/data 不匹配
再比如 location = /abc,则只匹配/abc ,/abc/或 /abcd不匹配。若 location /abc,则即匹配/abc 、/abcd/ 同时也匹配 /abc/。
(2)location / {}
因为所有的地址都以 / 开头,所以这条规则将匹配到所有请求 比如访问 / 和 /data, 则 / 匹配, /data 也匹配,
但后面前缀路径会和最长字符串优先匹配(最长匹配)
(3)location /documents/ {}
匹配任何以 /documents/ 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的前缀路径没有匹配到时,才会采用这一条
(4)location /documents/abc {}
匹配任何以 /documents/abc 开头的地址,匹配符合以后,还要继续往下搜索其它 location
只有其它 location后面的前缀路径没有匹配到时,才会采用这一条
(5)location ^~ /images/ {}
匹配任何以 /images/ 开头的地址,匹配符合以后,停止往下搜索正则,采用这一条
(6)location ~* .(gif|jpg|jpeg)$ {}
匹配所有以 gif、jpg或jpeg 结尾的请求
然而,所有请求 /images/ 下的图片会被 location ^~ /images/ 处理,因为 ^~ 的优先级更高,所以到达不了这一条正则
(7)location /images/abc {}
最长字符匹配到 /images/abc,优先级最低,继续往下搜索其它 location,会发现 ^~ 和 ~ 存在
(8)location ~ /images/abc {}
匹配以/images/abc 开头的,优先级次之,只有去掉 location ^~ /images/ 才会采用这一条
(9)location /images/abc/1.html {}
匹配/images/abc/1.html 文件,如果和正则location ~ /images/abc/1.html 相比,正则优先级更高
首先看 优先级:精确= > 前缀^~ > 正则,* > 一般 > 通用/
在没有精准匹配的情况下,先看前缀匹配的长度,然后根据最长的前缀匹配的优先级去确定是否再去看其它正则匹配location,
如果最长的前缀匹配带有 ^~ 则不再看其它正则匹配location,如果最长的前缀匹配是没有修饰符的一般匹配则会再看其它正则匹配location
前缀匹配看长度,最长的优先匹配
正则匹配看上下顺序,由上往下依次匹配,当有匹配成功时候,停止匹配,按当前匹配规则处理请求
只有在精准、前缀、正则、一般 都没有匹配到的时候才会看通用匹配
(1) 执行 server 块里面的 rewrite 指令。
(2) 执行 location 匹配。
(3) 执行选定的 location 中的 rewrite 指令。
(1)基于域名的跳转
现在公司旧域名www.kgc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.kgc.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.benet.com/$1 permanent; #$1为正则匹配的内容,即“域名/”之后的字符串
}
root html;
index index.html index.htm;
}
}
echo "192.168.247.131 www.kgc.com www.benet.com" >> /etc/hosts
systemctl restart nginx
(2)基于客户端 IP 访问跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log; #日志修改
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.80.10"){ #当客户端IP为192.168.80.10时,将变量值设为false,不进行重写
set $rewrite false;
}
#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #将域名后边的路径重写成/weihu.html后转发,例如www.kgc.com/weihu.html
}
location = /weihu.html {
root /var/www/html; #网页返回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /var/www/html/
echo "We are maintaining now!
" > /var/www/html/weihu.html
systemctl restart nginx
(3)基于旧域名跳转到新域名后面加目录
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.kgc.com www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
#添加
location /post {
rewrite (.+) http://www.kgc.com/bbs$1 permanent; #这里的$1为位置变量,代表/post
}
location / {
root html;
index index.html index.htm;
}
}
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.80.10 bbs.kgc.com" >> /etc/hosts
systemctl restart nginx
(4)基于参数匹配的跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$) {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
(5)基于目录下所有 php 结尾的文件跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
(6)基于最普通一条 url 请求的跳转
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* ^/abc/123.html {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx