基于域名的跳转
现在公司旧域名www.wt.com有业务需求变更,需要使用新域名www.dw.com代替,但是旧域名不能废除,需要跳转
到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.wt.com #域名修改
charset utf-8
access_log /var/log/nginx/www.wt.com-accsee.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.wt.com'){ #$host为rewrite全局变量。代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.dw.com /$1 permanent; #$1为正则匹配的内容,即域名后边的字符串
}
root html;
index index.html index.htm;
}
}
mkdir -p /var/log/ngingx
echo "192.168.153.10 www.wt.com www.dw.com" >> /etc/hosts
systemctl restart nginx
mkdir -p /usr/local/nginx/html/test/
echo 'this ih test web!' > /usr/local/nginx/html/test/1.html
systemctl restart nginx.service
浏览器输入模拟访问 http://www.wt.com/test/1.html
会跳转到www.dw.com/test/1.html,查看元素可以看到返回301.实现了永久重定向跳转,而且域名后的参数也正常跳转。