用rewrite规则实现将所有到a域名的访问rewrite到b域名
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域名的更多相关文章
- Nginx Rewrite规则初探(转)
Nginx rewrite(nginx url地址重写)Rewrite 主要的功能就是实现URL的重写,Nginx的Rewrite规则采用Pcre,perl兼容正则表达式的语法规则匹配,如果需要Ng ...
- HTTP POST请求的Apache Rewrite规则设置
最近自测后端模块时有个业务需求需要利用WebServer(我用的是Apache)将HTTP POST请求转发至后端C模块,后端处理后返回2进制加密数据.http post请求的url格式为: ...
- Nginx Rewrite规则
location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这条规则将匹配 ...
- Nginx配置location总结及rewrite规则写法
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 32.0px "Helvetica Neue"; color: #323333 } p. ...
- rewrite规则写法及nginx配置location总结
rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用. 例如http://seanlook.com/a/we/index.php ...
- nginx+tomcat集群配置(4)--rewrite规则和多应用根目录设定思路
前言: nginx中有一块很重要的概念, 就是rewrite规则. 它会对URL进行修改, 然后进行内部的重定向. rewrite授予了nginx更多的自由, 使得后级服务的接入更加地方便. 本文将简 ...
- Nginx 常用全局变量 及Rewrite规则详解
每次都很容易忘记Nginx的变量,下面列出来了一些常用 $remote_addr //获取客户端ip $binary_remote_addr //客户端ip(二进制) $remote_port //客 ...
- 转:Nginx 配置 location 总结及 rewrite 规则写法
转: http://www.linuxidc.com/Linux/2015-06/119398.htm 1. location正则写法 一个示例: location =/{ # 精确匹配 / ,主机名 ...
- 【转】nginx配置location总结及rewrite规则写法
原文:http://seanlook.com/2015/05/17/nginx-location-rewrite/ 1. location正则写法 location = / { # 精确匹配 / ,主 ...
随机推荐
- CSS基础 盒子相关属性总结 padding+border
1.border当个属性: 作用 属性名 属性值 边框粗细 border-width 数字+px 边框样式 border-style solid实线.dashed虚线.dotted点线 边框颜色 bo ...
- httprunner2.0 概述及使用说明
一.概述 HttpRunner是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试.性能测试.线上监控.持续集成等多种测试需求. 二.系统流程 ...
- 详解Scrapy的命令行工具
接触过Scrapy的人都知道,我们很多操作是需要借助命令行来执行的,比如创建项目,运行爬虫等.所以了解和掌握这些命令对于scrapy的学习是很有帮助的! Scrapy 命令 首先,在scrapy命令中 ...
- STC8H开发(四): FwLib_STC8 封装库的介绍和注意事项
目录 STC8H开发(一): 在Keil5中配置和使用FwLib_STC8封装库(图文详解) STC8H开发(二): 在Linux VSCode中配置和使用FwLib_STC8封装库(图文详解) ST ...
- Linux基础之终端、控制台、tty、pty简介说明
最近在搞Linux提权的时候,有时候su 用户名 登录的时候发现登录不了,因为tty终端,交互不了,所以我也来总结一下 一. 基本概念 1. ttytty(终端设备的统称):tty一词源于telety ...
- Go - 如何编写 ProtoBuf 插件 (三) ?
目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...
- 【刷题-LeetCode】203. Remove Linked List Elements
Remove Linked List Elements Remove all elements from a linked list of integers that have value *val* ...
- 【解决了一个小问题】golang xorm中使用where id in (xxx),没办法使用参数替换
代码中使用XORM来从数据库查询数据,有类似如下的代码: session.Where("id in (?)", strings,Join(arr, ",")) ...
- after effects的xml格式工程文件aepx的格式分析(一)
表面上看,AE的工程文件可以另存为xml格式,我们可以通过直接编辑XML来做一些事情. 很可惜,通过aepx文件修改AE工程,能做到的非常有限.理由是: 1.重要的属性都放在了标签的bdata这个属性 ...
- Collection的使用
常用方法 Modifier and Type Method and Description boolean add(E e) 确保此集合包含指定的元素(可选操作). boolean addAll(Co ...