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的六种查找命令

    http://www.ruanyifeng.com/blog/2009/10/5_ways_to_search_for_files_using_the_terminal.html 1. find fi ...

  2. Linux下配置GitHub

    一.注册GitHub账号 二.在linux命令行输入 git config --global user.name "YOUR NAME" #配置github账号 git confi ...

  3. react 网址导航

    项目搭建 使用webpack.babel.react.antdesign配置单页面应用开发环境

  4. 【Java】泛型

    文章目录 泛型 为什么要有泛型 在集合中使用泛型 如何自定义泛型结构 自定义泛型类.接口 泛型方法 泛型在继承方面的体现 通配符的使用 有限制条件的通配符的使用 泛型 为什么要有泛型 集合容器类在设计 ...

  5. Idea操作Maven详细使用:

    Idea操作Maven详细使用: Maven简介 什么是 Maven Maven 的正确发音是[ˈmevən] "卖v",而不是"马瘟"以及其他什么瘟. Mav ...

  6. Typora图床

    Typora图床 Typora+PicGo+Gitee(码云)实现高效Markdown图床 typora是我最早接触的markdown格式的轻文本编辑器,因为我是计算机专业,所以平常记笔记会有代码块, ...

  7. FFT 傅里叶万岁

    FFT --- Fast Foulier Transformation 以 $O(n \log n)$ 的速度计算 $\forall k=1,2,\dots,n, c[k]=\sum\limits_{ ...

  8. Linux命令(2)--cp拷贝、mv剪切、head、tail追踪、tar归档

    文章目录 一.知识回顾 ls cd 二.Linux基本操作(二) 1.cp 拷贝 2.mv 移动(剪切) 3.head 头部 4.tail 追踪(尾部) 5.tar 归档 查看 压缩 解压 总结 一. ...

  9. 【LeetCode】628. 三个数的最大乘积

    解题思路 如果数组中全是正数或者全是负数,最大乘积就是最大的三个数的乘积.如果数组中既有正数又有负数,最大乘积可能是三个最大正数乘积,也可能是两个最小负数和最大正数的乘积.遍历数组找到最大的三个数和最 ...

  10. Manacher算法求解回文字符串

    Manacher算法可以在\(O(N)\)时间内求解出一个字符串的所有回文子串(正反遍历相同的字串). 注:回文串显然有两种,一种是奇数长度,如abczcba,有一个中心字符z:另外一种是偶数个长度, ...