【问题标题】:Nginx - redirect correctly from http:// to https:// without wwwNginx - 从 http:// 正确重定向到 https:// 没有 www
【发布时间】:2016-04-27 19:17:27
【问题描述】:

按照我的配置文件:

server {
   listen [::]:443 ipv6only=off ssl;
   server_name www.example.com;
   // ssl stuff
   return 301 https://example.com$request_uri;
}

server {
   listen  [::]:80 ipv6only=off;
   return 301 https://example.com$request_uri;
}

server {
   listen [::]:443 ssl;  
   server_name example.com;
   // php and ssl stuff
}

我不明白为什么http://www.example.com 会重定向到https://www.example.com,然后再重定向到https://example.com。如何从http://www.example.com直接重定向到https://example.com

【问题讨论】:

  • 您启用了 HSTS 吗?如果是,那么第一次重定向是由您的浏览器直接完成的,无需任何网络交互,因此无需担心。
  • @Tom 是的!很高兴知道!非常感谢。
  • 所以我让你更新你的问题添加 HSTS 并验证我的答案:)

标签: redirect ssl nginx https


【解决方案1】:

启用 HSTS 后,第一次重定向由您的浏览器直接完成,无需任何网络交互。

【讨论】:

    【解决方案2】:

    NGINX 配置,用于在不使用 WWW 的情况下从 HTTP 重定向到 HTTPS:

    server {
       listen 80 default_server;
       listen  [::]:80 default_server;
       server_name example.com www.example.com;
       return 301 https://example.com$request_uri;
    }
    server {
       listen 443 default_server;
       listen [::]:443 ssl http2 default_server;
       server_name example.com www.example.com;
       ##here-ssl-settings##
       return 301 https://example.com$request_uri;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-23
      • 2014-05-08
      • 1970-01-01
      • 2018-02-02
      • 2020-10-01
      • 2016-01-07
      • 1970-01-01
      • 2017-09-03
      相关资源
      最近更新 更多