首页 > 技术文章 > nginx服务跳转

zhangfushuai 2021-05-24 09:18 原文

1.什么是页面跳转

  • 将URL信息做改变
  • 将URI信息做改变
  • 完成伪静态配置

2.实现页面跳转的方法

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

2.1rewrite跳转
Syntax:	rewrite     regex              replacement           [flag];
                 匹配需要跳转的信息     跳转成什么地址       标记
Default:	—
Context:	server, location, if 

标记:

  • last:
  • break:
  • redirect: 临时跳转,状态码为302
  • permanent:永久跳转,状态码为301
2.2return跳转
Syntax:    return code [text];
return code URL;
return URL;
Default:    —
Context:    server, location, if
2.3页面跳转实践操作
01.跳转配置中last与break区别对比
[root@web01-172.16.1.7 /html]# vim /etc/nginx/conf.d/rewrite.conf 
server{
  listen 80;
  server_name rewrite.shuai.com;
  root    /html;

  location ~ ^/break/ {
     rewrite  ^/break/  /test/  break;

}
  location ~ ^/last/ {
     rewrite  ^/last/  /test/  last;

}
  location ~ /test/ {
     default_type  application/json;
     return 200 'ok';

}

}
~ 

02.break和last区别

break:一旦跳转完毕,就会停止后续操作过程,不会在显示跳转页面地址,没有跳转目录

last:一旦跳转完毕,会继续访问后端页面

2.4常见跳转示例情况测试说明
例一:用户访问/abc/1.html实际上真实访问的是/ccc/bbb/2.html

#http://rewrite.shuai.com/abc/1.html>>http://rewrite.shuai.com/ccc/bbb/2.html

01.准备路径
[root@web01-172.16.1.7 ~]# mkdir /html/ccc/bbb -p
[root@web01-172.16.1.7 ~]# echo "ccc_bbb_2" > /html/ccc/bbb/2.html

02.配置nginx跳转

[root@web01-172.16.1.7 ~]# cat /etc/nginx/conf.d/ccbb.conf 
server {
  listen 80;
  server_name rewrite.shuai.com; 
  location / {
   root /html;
   index index.html;
  }
  location /abc {
  rewrite (.*) /ccc/bbb/2.html redirect; 
  #return 302 /ccc/bbb/2.html;
  }
}

例二:用户访问/2019/ccc/bbb/2.html实际上访问的是/2020/ccc/bbb/2.html

http://rewrite.shuai.com/2019/ccc/bbb/2.html>>http://rewrite.shuai.com/2020/ccc/bbb/2.html

01.准备路径
[root@web01-172.16.1.7 ~]# mkdir /html/2020/ccc/bbb -p
[root@web01-172.16.1.7 ~]# echo "2020_ccc_bbb_2" > /html/2020/ccc/bbb/2.html

02.配置nginx跳转

[root@web01-172.16.1.7 ~]# vim /etc/nginx/conf.d/ccbb.conf 
server {
  listen 80;
  server_name rewrite.shuai.com;
  location / {
   root /html;
   index index.html;
  }
  location /2019 {
  rewrite ^/2019/(.*)$ /2020/$1 redirect;
  return 302 /2020/ccc/bbb/2.html;
  }
}

例三:用户访问/test目录下的任何内容,实际上是访问http://rewrite.shuai.com
01.准备路径
[root@web01-172.16.1.7 ~]# mkdir /html/test/1.jpg

02.配置nginx跳转

[root@web01-172.16.1.7 /html]# cat /etc/nginx/conf.d/caca.conf 
server {
  listen 80;
  server_name rewrite.shuai.com; 
  location / {
   root /html;
   index  1.jpg  index.html;
  }
  location /test {

   rewrite (.*) http://rewrite.shuai.com  redirect;


  }
}

例四:用户访问course-11-22-33.html实际上真实访问是/course/11/22/33/course_33.html

http://rewrite.shuai.com/course-11-22-33.html>>http://rewrite.shuai.com/html/11/22/33/course_33.html

01.准备路径
[root@web01-172.16.1.7 ~]# mkdir /html/coures/11/22/33 -p
[root@web01-172.16.1.7 /html/coures]# echo "mr_zhang.com">/html/coures/11/22/33/course_33.html

02.配置nginx跳转

[root@web01-172.16.1.7 /html/coures]# vim  /etc/nginx/conf.d/ccbb.conf 
server {
  listen 80;
  server_name rewrite.shuai.com;
  location / {
   root /html;
   index index.html;
   rewrite ^/course-(.*)-(.*)-(.*).html$ /coures/$1/$2/$3/course_$3.html break; #动态
   rewrite ^/course-(.*)  /coures/11/22/33/course_33.html redirect; #静态


  }
}

说明:last/break做跳转不会显示跳转的地址信息

3.基于URL跳转

rewrite.shuai.com>>www.ashuai.com

3.1修改dns解析
10.0.0.7  rewrite.shuai.com  www.ashuai.com
3.2配置nginx跳转
[root@web01-172.16.1.7 /etc/nginx/conf.d]# cat ccbb.conf 
server {
  listen 80;
  server_name rewrite.shuai.com;
  rewrite ^/(.*) http://www.jd.com/$1  permanent;

}

server {
  listen 80;
  server_name www.jd.com; 
  location / {
   root /html;
   index index.html;
  }  
}



##################################################或
server {
  listen 80;
  server_name    rewrite.shuai.com   www.jd.com; 
  location / {
   root /html;
   index index.html;
   if ($http_host ~*  ^rewrite.shuai.com){
       rewrite ^/(.*) http://www.jd.com/$1  permanent;
     }
  }  
}

4.将http请求,跳转至https

4.1创建https访问
4.1.1nginx安装ssl模块
4.1.2安装证书,黑户证书,实际环境不可用
openssl genrsa -idea -out server.key 2048
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt 

4.1.3修改nginx配置文件

[root@web01-172.16.1.7 /etc/nginx/conf.d]# cat blog.conf

server{ 
    listen        443  ssl;
    server_name   blog.ashuai.com;
    fastcgi_buffers  512  64k;
    ssl_certificate     /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
    location / {
        root  /blog/wordpress;
        index  index.php;
    }
     location ~ \.php$ {
        root /blog/wordpress;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass  127.0.0.1:9000;   
        include fastcgi_params;   
   }
} 

4.2实现http跳转为https

[root@web01-172.16.1.7 /etc/nginx/conf.d]# cat blog.conf
server{
   listen     80;
   server_name blog.ashuai.com;
   rewrite (.*) https://$server_name$1 redirect;
}
server{
    listen    443 ssl;
    server_name   blog.ashuai.com;
    fastcgi_buffers  512  64k;
    ssl_certificate     /etc/nginx/server.crt;
    ssl_certificate_key /etc/nginx/server.key;
    location / {
        root  /blog/wordpress;
        index  index.php;
    }
     location ~ \.php$ {
        root /blog/wordpress;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass  127.0.0.1:9000;   
        include fastcgi_params;   
   }
} 

 

推荐阅读