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. 变量覆盖-高级篇(动态覆盖,extract综合)

    0x00 原理   变量覆盖漏洞可以让用户定义的变量值覆盖原有程序变量,可控制原程序逻辑. 0x01 代码 <?php highlight_file('index.php'); function ...

  2. shell中的 “.”和source export

    在shell脚本里面export的环境变量,在set里面竟然看不到.为什么在shell脚本里面用export设置环境变量之后,当shell执行完了,用set命令看不到呢?但是你如果直接在终端里expo ...

  3. CSS3 变形

    目录 Transform Transform与坐标系统 transform-origin transform-style 二维旋转 旋转 rotate 平移 translate translateX ...

  4. RHEL高级磁盘管理—Stratis

    2. Stratis 本地存储管理工具,通过Stratis可以便捷的使用Thin Provisioning.Snapshots.Pool-based的管理和监控等高级存储功能. Stratis 基于x ...

  5. PHP使用mail函数发送邮件

    yum install sendmail #需要使用到sendmail hostname jb51.net #建议修改主机名为域名格式,否则启动sendmail会很慢 systemctl start ...

  6. 013.Ansible Playbook include

    一 include 当项目越大,tasks越多的时候.如果将多有的task写入一个playbook中,可读性很差,就需要重新组织playbook 可以把一个playbook分成若干份晓得palyboo ...

  7. 按时按登录IP记录Linux所有用户操作日志的方法

    背景:Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了.以下方法可以实现通过记录登陆IP地址和所 ...

  8. 校准仪开发日志--2017-10-20 today's question

  9. Python for循环 - Python零基础入门教程

    目录 一.for 循环语法 二.for 循环实战 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python ...

  10. xss-代码角度理解与绕过filter

    0x00 原理   xss全称为cross site scripting,中文为跨站脚本攻击.它允许web用户将恶意代码植入到提供给用户使用的页面.代码包括HTML代码和客户端脚本. 0x01 危害 ...