1、临时重定向

1.1使用redirect实现临时重定向

# cat /apps/nginx/conf/nginx.conf
...省略...
server {
listen 80;
server_name www.a.com;
location / {
root /data/nginx/html/a;
index index.html index.htm;
rewrite / http://www.b.com redirect;
}
} server {
listen 80;
server_name www.b.com;
location / {
root /data/nginx/html/b;
index index.html index.htm;
} }

1.2 测试

#curl www.a.com
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
#curl www.a.com -Ik
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.18.0
Date: Sun, 28 Nov 2021 07:47:55 GMT
Content-Type: text/html
Content-Length: 145
Connection: keep-alive
Location: http://www.b.com

win10端测试,添加www.a.com的本地域名解析

2、永久重定向

2.1 使用permanent实现临时重定向

# cat /apps/nginx/conf/nginx.conf
...省略...
server {
listen 80;
server_name www.a.com;
location / {
root /data/nginx/html/a;
index index.html index.htm;
rewrite / http://www.b.com permanent;
}
} server {
listen 80;
server_name www.b.com;
location / {
root /data/nginx/html/b;
index index.html index.htm;
} }

2.2 访问测试

#curl www.a.com -Ik
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0
Date: Sun, 28 Nov 2021 08:01:40 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: http://www.b.com #curl www.a.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

可以看出临时重定向的状态码是302,而永久重定向的状态码是301

用rewrite规则实现将所有到a域名的访问rewrite到b域名的更多相关文章

  1. Nginx Rewrite规则初探(转)

    Nginx  rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...

  2. HTTP POST请求的Apache Rewrite规则设置

    最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为:     ...

  3. Nginx Rewrite规则

    location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...

  4. Nginx配置location总结及rewrite规则写法

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #323333 } p. ...

  5. rewrite规则写法及nginx配置location总结

    rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...

  6. nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路

    前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...

  7. Nginx 常用全局变量 及Rewrite规则详解

    每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...

  8. 转:Nginx 配置 location 总结及 rewrite 规则写法

    转: http://www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名 ...

  9. 【转】nginx配置location总结及rewrite规则写法

    原文:http://seanlook.com/2015/05/17/nginx-location-rewrite/ 1. location正则写法 location = / { # 精确匹配 / ,主 ...

随机推荐

  1. Linux 安装并启用 PHP-FPM

    首先,在编译时带上 --enable-fpm 参数: [root@localhost local]# yum -y install libxml2 libxml2-devel gd gd-devel ...

  2. Docker 部署 ElasticSearch-Head 及其他插件

    拉取ElasticSearch-Head镜像 docker pull mobz/elasticsearch-head:5 运行ElasticSearch-Head容器 docker run -d -- ...

  3. 创建react开发环境

    准备工作 1.下载node.js(http://nodejs.cn/download/)推荐下载长期支持的版本 2.下载cnpm(https://jingyan.baidu.com/article/9 ...

  4. Python入门(上)

    Python入门(上) Python入门(上) 简介 Python 基础语法 行与缩进 注释 运算符 标准数据类型 变量 编程流程 顺序(略) 分支 if 循环 for while break 和 c ...

  5. h5跳转高德地图

    <a href="https://uri.amap.com/marker?position=经度,纬度&name=所在的位置名称">高德地图</a>

  6. Centos7安装erlang以及RabbitMQ Centos启动rabbitmq

    本文使用版本:  rabbitmq-server-3.8.3-1.el7.noarch.rpm   Centos7  erlang  22.3.1 在线安装 yum install esl-erlan ...

  7. CUDA 入门(转)

    CUDA(Compute Unified Device Architecture)的中文全称为计算统一设备架构.做图像视觉领域的同学多多少少都会接触到CUDA,毕竟要做性能速度优化,CUDA是个很重要 ...

  8. 【Android】安卓中的存储

    [Android]安卓中的存储 1.存储在App内部 最简单的一种.在尝试过程中发现,手机中很多文件夹都没有权限读写.我们可以将我们需要写的文件存放到App中的files文件夹中,当然我们有权限在整个 ...

  9. windows10双系统删除linux

    问题 在这里删除后会发现有残留一个引导区,几百m(下图已经删除完),而且启动会进linux引导,然后必须f12进入选择启动项才可以启动windows 解决方法 使用删除引导就可以了 再使用傲梅分区助手 ...

  10. ROS Kinetic 使用PocketSphinx进行语音识别报错

     已解决,重启电脑,装环境把声卡驱动那些似乎死机了 ROS Kinetic 使用PocketSphinx进行语音识别教程 https://www.ncnynl.com/archives/201701/ ...