实现域名跳转

一、www.a.com -> www.b.com

1、目标:当在网页中输入www.a.com时,自动跳转到www.b.com

2、操作步骤:    

(1) 安装nginx,保证nginx能够正常访问

(2) 修改nginx的配置文件

(3) 重启nginx服务

(4) 浏览器测试

3、操作流程

关闭防火墙以及linux安全机制

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

nginx正常访问的情况下,修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
if ($host = "www.b.com") {
        rewrite ^(.*)$ http://www.a.com/$1 permanent;
 }

测试语法,重启

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

  [root@localhost ~]# nginx
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
  nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

网页测试(访问www.b.com ,返回www.a.com)

 

二、www.a.com/a.html -> www.a.com/file/a.html

1、目标:当在网页中输入www.a.com/a.html时,自动跳转到www.a.com/file/a.html

2、操作步骤:

(1) 安装nginx,保证能正常访问

(2) 修改nginx的配置文件

(3) 重启nginx服务

(4) /usr/local/nginx/html下创建文件a.html,编写内容“1111111111

(5) /usr/local/nginx/html下创建文件夹file,在file下创建文件a.html,编写内容“22222222

(6) 浏览器测试

3、操作过程

关闭防火墙以及linux安全机制

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

nginx安装完了,修改配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location /a.html {
        rewrite ^(.*)$ http://www.a.com/file/a.html permanent;

}

重启nginx服务

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

创建文件a.html,编写内容“11111111”

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim a.html
1111111111

创建文件夹file,在file中创建文件a.html,编写内容“22222222”

[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# mkdir file
[root@localhost html]# ls
50x.html  file  index.html
[root@localhost html]# cd file/
[root@localhost file]# touch a.html
[root@localhost file]# vim a.html
22222222

 浏览器测试(访问www.a.com/a.html ,返回www.a.com/file/a.html)

 

 

原文地址:https://www.cnblogs.com/tanxiaojuncom/p/11549148.html