十二 nginx中location重写和匹配规则

作者 : admin 本文共1461个字,预计阅读时间需要4分钟 发布时间: 2024-06-11 共1人阅读

十二 location匹配规则 
= ^~ ~ ~*  !~ !~* /a   / @
@内部服务跳转

十三 nginx地址重写rewrite
if rewrite set return 

13.1 if 应用环境 
server location

-x  文件是否可执行 
$args  
$document_rot
$host
$limit_rate 
$remote_addr 
$server_name 
$document_uri  

if (coduction) {
… 
 }

13.2 rewrite  flag  
last     
break     本条匹配完成后 终止匹配
redirect     302  临时重定向 
permanent   301  永久重定向 

80 http
443 https  

案例1
修改前:http://www.dadishu.com/a/index.html 
修改后:http://www.dadishu.com/b/index.html
server   {
      listen 80;
      server_name www.dadishu.com;
location / {
       root   /data/wwwroot 
       index index.html
      }
location /a {
    root /html; 
    index   index.html;
   rewrite  .* /b/2.html permanent;
}
location /b {
    root /html; 
    index  index.html;
}
}

案例2 
http://www.testpm.com/2019/a/index.html 
http://www.testpm.com/2018/a/index.html
location /2019/a {
    root     /html;
    index  index.html;
    rewrite ^/2019/(.*)$ /2018/$1  permanent; 
}
location /2018/a {
    root /html;
    index  index.html;
}

案例三 核心
http://www.qf.com/a/1.html   http://jd.com 
location /a  {
    root /html;
    if ($host ~* www.qf.com) {
    rewrite .* http//jd.com permanent;
    }
}

案例四 
http://www.qf.com/a/1.html  http://jd.com/a/1.html 

location /a {
    root /html;
    if ($host ~* qf.com){
rewrite .* http://jd.com$request_uri permanent;
    }
}

案例五 
修改前:http//www.tianyun.com/login/tianyun.html 
修改后:http://www.tianyun.com/reg/login.html?user=tianyun
location /login {
    root /usr/share/nginx/html;
    rewrite ^/login/(.*)\.html$ http://$host/reg/login.html?user=$1;

location /reg {
root /usr/share/nginx/html;
index login.html;    
}

13.3  set指令 
应用环境 server location if 

13.4 return  
server location if 

 

本站无任何商业行为
个人在线分享 » 十二 nginx中location重写和匹配规则
E-->