Redirect 如何在NginX中将www.example.com重定向到example.com?

Redirect 如何在NginX中将www.example.com重定向到example.com?,redirect,nginx,Redirect,Nginx,当我们打开example.com(不带HTTPS)时,站点加载得非常好,但当我们打开www.example.com(不带HTTPS)时,它会打开“欢迎使用nginX”,但我不需要它,我们想要打开example.com,即使URL是www.example.com 注意:我们的网站有“HTTPS”,当我们打开example.com时,它将被重定向到https://example.com 注意:当我们打开https://www.example.com,网站加载良好,但当URL为www.example.

当我们打开
example.com
(不带HTTPS)时,站点加载得非常好,但当我们打开
www.example.com
(不带HTTPS)时,它会打开“欢迎使用nginX”,但我不需要它,我们想要打开
example.com
,即使URL是
www.example.com

注意:我们的网站有“HTTPS”,当我们打开
example.com
时,它将被重定向到
https://example.com

注意:当我们打开
https://www.example.com
,网站加载良好,但当URL为
www.example.com
https://www.example.com
它没有被重定向


我们在AWS使用Ubuntu。

您是否为每个选项定义了单独的服务器? 请参阅下面的示例代码段。 你的问题看起来很相似,而且都有很多信息,我不会复制粘贴到这里

这个链接也可能有帮助

}


$scheme
是协议(HTTP或HTTPS),而
$request\u uri
是包含参数的完整uri。

为此,我建议编辑您的nginx配置文件(通常在/etc/nginx/sites enabled/you example config中),添加返回301,如图所示:

server {
    listen 80;
    listen 443;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
    #[...ssl settings go here, certs etc....]
}
server {
    listen 443;
    server_name example.com;
    # [...ssl and main settings go here...]
}
这将导致所有请求返回

server {
    listen 80;
    listen 443;
    server_name www.example.com example.com;
    return 301 https://example.com$request_uri;
    #[...ssl settings go here, certs etc....]
}
server {
    listen 443;
    server_name example.com;
    # [...ssl and main settings go here...]
}