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. 《Python核心编程第3版中文版》(高清).PDF,免费无需任何解压密码

    链接:https://pan.baidu.com/s/18d3xinNX1oH5q8zpB10ABA 提取码:dx7h

  2. Django_模型类详解(七)

    # 定义书籍模型类 class BookInfo(models.Model): btitle = models.CharField(max_length=20) # 书籍名称 bpub_date = ...

  3. js 模块化 -- export 时 一个默认和多个默认的写法

    js文件 只有一个  food 类,一般写 export {food} 或者 export {food as default} 那么如果有多个呢? 这样 export {food, food2} 或者 ...

  4. 怎样查看Jenkins的版本

    where to check jenkins version To identify your current version of Jenkins, you can do one of two th ...

  5. jsp文件中文乱码解决

    文件顶加上 <%@ page contentType="text/html;charset=UTF-8" language="java" %>即可

  6. LINUX学习-Nginx+JDK+Tomcat+Mysql搭建JAVA WEB

    一.安装JDK环境 1.先到  https://www.oracle.com/java/technologies/javase-jdk8-downloads.html  下载linux jdk-8u2 ...

  7. rocketmq实现延迟队列精确到秒级实现方案1-代理实现

    简单的来说,就是rocketmq发送消息到broker的时候,判断是否定时消息, 如果是定时消息,将消息发送到代理服务(这个是一个独立的服务,需要自己开发,定时地把消息发送出去), 当然了消息用什么来 ...

  8. 图片不清晰?Graphics 高质量绘制

    Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; Graphics ...

  9. P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two

    // Problem: P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two // Contest: Luogu // URL: https://www.luogu.com ...

  10. 用Python实现一个Picgo图床工具

    PyPicGo PyPicGo 是一款图床工具,是PicGo是Python版实现,并支持各种插件自定义插件,目前PyPicGo自带了gitee.github.SM.MS和七牛云图传,以及rename. ...