https:// domain .com重定向到https:// www

时间:2019-07-05 10:27:09

标签: apache nginx configuration

我的nginx配置主要使用https,从http到https的所有重定向都可以使用,除了https://example.com需要重定向到https://www.example.com之外,有人可以告诉我我要去哪里了吗?

我尝试在服务器块中添加重定向,以侦听example.com的443以重定向到www.example.com,但是我遇到了同样的问题。

{{1}}

我希望将网址https://example.com重定向到https://www.example.com

1 个答案:

答案 0 :(得分:0)

在example.com括号中,不需要“ if($ host = example.com){” 因为您已经通过设置“ server_name example.com;”来分隔进入此括号的主机。同样,当您重定向到https:// $ host $ request_uri时; ,您将重定向到example.com,而不是www.example.com,从而创建重定向的无限循环。

希望对您有帮助,这是应该可以使用的example.com配置:

server {

 location / {

    return 301 https://www.example.com$request_uri;
  }

    server_name example.com;
    return 404; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # m$
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; #$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
相关问题