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. Swoole 中协程的使用注意事项及协程中的异常捕获

    协程使用注意事项 协程内部禁止使用全局变量,以免发生数据错乱: 协程使用 use 关键字引入外部变量到当前作用域禁止使用引用,以免发生数据错乱: 不能使用类静态变量 Class::$array / 全 ...

  2. css 基础 选择器的使用

    1.标签选择器解释:是针对一个标签做的样式,它会将匹配的所有标签都发生改变语法格式:标签名{ css样式代码 }2.类选择器特点:1.可以给某一个标签标记为一类,设定css样式,比较灵活 2.类目可以 ...

  3. 使用.NET 6开发TodoList应用(22)——实现缓存

    系列导航及源代码 使用.NET 6开发TodoList应用文章索引 需求 有的时候为了减少客户端请求相同资源的逻辑重复执行,我们会考虑使用一些缓存的方式,在.NET 6中,我们可以借助框架提供的中间件 ...

  4. nginx中请求大小的限制的设置

    Nginx对客户端请求缓冲区大小有个默认限制,如果超过了该值(比如在上传大文件时),会报500错误. 只需要设置三个值,就可以解决该问题: 1. client_body_buffer_size: 指定 ...

  5. vs2017 winform 组件 -- 总结

    1.ComboBox  [下拉框] (1) 添加选项 this.[控件名].Items.Add("内容") (2)设置下拉框 自动完成 模式 和 数据源 this.[控件名].Au ...

  6. Oracle 五种约束的创建和移除:

    1.主键约束: 创建表的时候就添加: create table table_name (categoryId varchar2(10), categoryName varchar2(30), prim ...

  7. c# - 接口的写法与基本调用

    1.前言 接口与Java基本一样 2.操作 (1)看路径结果 (2) 接口源码: namespace ConsoleApp1 { public interface ILogin { void Eat( ...

  8. jsp文件中文乱码解决

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

  9. SpringBoot学习笔记二之Spring整合Mybatis

    原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...

  10. POJ2891Strange Way to Express Integers

    http://poj.org/problem?id=2891 实际上就是一个一元线性同余方程组.按照合并的方式来解即可. 有一个注意点,调用函数是会慢的. #include<iostream&g ...