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;
}
}

nginx服务跳转的更多相关文章

  1. 9. nginx服务实验笔记

    LNMP安装与配置   Nginx与apache.lighttp性能综合对比,如下图:     一.系统需求: CentOS/RHEL/Fedora/Debian/Ubuntu系统 需要3GB以上硬盘 ...

  2. nginx服务部署 说明

    第1章 常用的软件 1.1 常用来提供静态服务的软件   Apache :这是中小型Web服务的主流,Web服务器中的老大哥,   Nginx :大型网站Web服务的主流,曾经Web服务器中的初生牛犊 ...

  3. 【实战分享】又拍云 OpenResty / Nginx 服务优化实践

    2018 年 11 月 17 日,由 OpenResty 主办的 OpenResty Con 2018 在杭州举行.本次 OpenResty Con 的主题涉及 OpenResty 的新开源特性.业界 ...

  4. nginx服务企业应用

    1.1 常用来提供静态服务的软件 Apache :这是中小型Web服务的主流,Web服务器中的老大哥, Nginx :大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大. Nginx 的 ...

  5. Nginx服务优化

    1.1Nginx.conf配置文件基本参数优化 1.1.1  隐藏nginx header内版本号信息 一些特定的系统及服务漏洞一般都和特定的软件及版本号有关,我们应尽量隐藏服务器的敏感信息(软件名称 ...

  6. 企业级Nginx服务基础到架构优化详解

    1.隐藏nginx header版本号 2.更改源码隐藏软件名称 3.更改nginx默认用户及用户组 4.配置nginx worker进程个数 5.根据CPU核数进行nginx进程优化 6.nginx ...

  7. Nginx服务的地址重写

    调整Nginx服务器配置,实现: 1.所有访问a.html的请求,重定向到b.html; 2.所有访问Nginx服务器(192.168.4.1)的请求重定向至www.baidu.com: 3.所有访问 ...

  8. Nginx服务部署

    1 企业常用网站服务 处理静态资源:nginx.apache.Lighttpd处理动态资源:tomcat(java语言编写).php(php语言编写).python(python语言编写)nginx网 ...

  9. 【Linux】nginx服务配置

    一. 部署LNMP环境 准备工作 Linux系统准备 设置IP 关闭防火墙 yum源配置 安装: 传输软件包 1. tar -zxvf lnmp1.2-full.tar.gz cd lnmp1.2-f ...

随机推荐

  1. Tomcat的使用和配置

    Tomcat的使用 安装 在tomcat官网找到你需要用的 Tomcat 版本对应的 zip 压缩包,解压到需要安装的目录即可 目录介绍 bin : 专门用来存放Tomcat服务器的可执行文件 con ...

  2. [bug] IDEA 右侧模块灰色

    参考 https://blog.csdn.net/weixin_44188501/article/details/104717177

  3. SSH 远程控制

    本文以 Ubuntu 20.04(客户端) 控制 Kali Linux 2020.2(服务端)为例 1.安装SSH(secure Shell) SSH分为客户端oppenssh-client和服务端o ...

  4. nginx 的基础知识(一)

    Nginx  HTTP 和 反向代理web服务器 epoll 占用少的系统资源.支持更多的并发连接 负载均衡 安装简单.配置灵活 热部署.启动快.不间断服务情况下对软件配置进行升级 反向代理 反向代理 ...

  5. 使用 Flux+Flagger+Istio+Kubernetes 实战 GitOps 云原生渐进式(金丝雀)交付

    在这篇指南中,你将获得使用 Kubernetes 和 Istio 使用 GitOps 进行渐进式交付(Progressive Delivery)的实际经验. 介绍 gitops-istio GitOp ...

  6. 游刃于私有网络与公共网络之间的NAT

    网络地址转化技术NAT 1. 应用场景 2. NAT 2.1 静态NAT 2.2 动态NAT 2.3 NAPT 2.4 EASY IP 3. NAT配置 3.1 静态NAT 3.2 动态NAT 3.3 ...

  7. Python+Selenium自动化 模拟鼠标操作

    Python+Selenium自动化 模拟鼠标操作   在webdriver中,鼠标的一些操作如:双击.右击.悬停.拖动等都被封装在ActionChains类中,我们只用在需要使用的时候,导入这个类就 ...

  8. Docker学习(1) 初识

    Docker的使用场景 1 使用Docker容器开发,测试,部署服务 2 创建隔离的运行环境 3 搭建测试环境 4 构建多用户的平台及服务(PaaS)基础设施 5 提供软件即服务(SaaS)应用程序 ...

  9. 自监督学习(Self-Supervised Learning)多篇论文解读(下)

    自监督学习(Self-Supervised Learning)多篇论文解读(下) 之前的研究思路主要是设计各种各样的pretext任务,比如patch相对位置预测.旋转预测.灰度图片上色.视频帧排序等 ...

  10. OpenCV读写视频文件解析(二)

    OpenCV读写视频文件解析(二) VideoCapture::set 设置视频捕获中的属性. C++:bool VideoCapture::set(int propId, double value) ...