首页 > 解决方案 > 为什么网站不使用 nginx 重定向

问题描述

我正在尝试将域 example.com 重定向到另一个网站 example.blogspot.com (它不在我的服务器中)。

我一直在使用htaccessand cpanel,所以很容易重定向。但是为什么重定向在 nginx 中不起作用。

nginx/1.15.10在版本和最新版本中使用以下方式没有看到任何效果vesta control panel

仍然显示由vesta控制面板生成的默认页面(index.html)。我应该更改 public_html 吗?还是重启服务器?

我已经nginx.conf使用以下代码进行了编辑。

方法一

http{
    server{
        listen          80;
        server_name     example.com www.example.com;
        return          301 https://example.blogspot.com$request_uri;
    }
    .....
}

方法二

http{
    server{
        listen [::]:80;
        listen          80;
        server_name     example.com www.example.com;
        return          301 https://example.blogspot.com$request_uri;
    }
    .....
}

方法三

http{
    server { 
        server_name .example.com;
        return 301 $scheme://example.blogspot.com;
    }
    ....

}

保存后重启ngix

sudo service nginx restart
sudo systemctl restart nginx

标签: nginxredirecthttp-status-code-301vesta

解决方案


最后,添加Server Ip address监听端口重定向域

http{
    server{
        listen          my.server.ip.address:80;
        server_name     example.com www.example.com;
        return          301 https://example.blogspot.com$request_uri;
    }
    .....
}

推荐阅读