使用rewrite规则实现将所有到a域名的访问rewrite到b域名

本文介绍了一个具体的 Nginx rewrite 规则案例,展示了如何将对 www.aaa.com 的所有请求重定向至 www.bbb.com,并进一步演示了如何通过 curl 命令验证重定向的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用rewrite规则实现将所有到a域名的访问rewrite到b域名

[root@centos8 ~]#vim /apps/nginx/conf.d/pc.conf
server {
	listen 80;
	server_name www.aaa.com;
	location / {
        root /data/nginx/html/pc;
        index index.html;
        rewrite / http://www.bbb.com redirect;
	}
}

server {
	listen 80;
	server_name www.bbb.com;
	location / {
        root /dara/nginx/html/mobile;
        index index.html;
	}
}

[root@centos8 ~]#echo aaa > /data/nginx/html/pc/index.html
[root@centos8 ~]#echo bbb > /data/nginx/html/mobile/index.html

[root@ubuntu18 ~]#vim /etc/hosts
10.0.0.58  www.aaa.com www.bbb.com

[root@ubuntu18 ~]#curl www.aaa.com
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>

[root@ubuntu18 ~]#curl -L www.aaa.com
bbb

[root@ubuntu18 ~]#curl www.aaa.com -Lv (-L跟随跳转-v显示细节)
* Rebuilt URL to: www.aaa.com/
*   Trying 10.0.0.58...
* TCP_NODELAY set
* Connected to www.aaa.com (10.0.0.58) port 80 (#0)
> GET / HTTP/1.1
> Host: www.aaa.com
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx
< Date: Fri, 01 Jul 2022 07:22:29 GMT
< Content-Type: text/html
< Content-Length: 162
< Connection: keep-alive
< Location: http://www.bbb.com
< 
* Ignoring the response-body
* Connection #0 to host www.aaa.com left intact
* Issue another request to this URL: 'http://www.bbb.com'
* Rebuilt URL to: http://www.bbb.com/
*   Trying 209.17.116.160...
* TCP_NODELAY set
* Connected to www.bbb.com (209.17.116.160) port 80 (#1)
> GET / HTTP/1.1
> Host: www.bbb.com
> User-Agent: curl/7.58.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: openresty/1.19.9.1
< Date: Fri, 01 Jul 2022 07:22:32 GMT
< Content-Type: text/html
< Content-Length: 175
< Connection: keep-alive
< Location: https://www.bbb.com/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值