AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 770505
Accepted
dotslash
dotslash
Asked: 2016-04-15 22:37:52 +0800 CST2016-04-15 22:37:52 +0800 CST 2016-04-15 22:37:52 +0800 CST

如何使用 Nginx 从 http(s)://(www.)example.com 重定向到 https://example.com?

  • 772

正如标题所述。

我发现了另一个有用的 Q/A,但它没有显示如何在 Nginx 设置中正确执行,我的意思是parameters. 他可能的意思是https://exmaple.com在被重定向到 之前应该有一个有效的握手https://www.example.com。在我的场景中https://www.example.com效果很好,但https:example.com不能重定向到https://www.example.com.

server {
    listen 80;
    server_name example.com www.example.com;
    rewrite ^ https://example.com$request_uri? permanent;
}

server {
    listen 443;
    server_name example.com;

    ssl on;
    # some other settings, correctly

}

但是,它在浏览http://example.com时被重定向到,https://example.com但它以 chrome 显示

This site can’t be reached 
example.com unexpectedly closed the connection.
ERR_CONNECTION_CLOSED```

我想全部重定向到https://example.com,如何?

- 更新 -

/var/log/nginx/access.log节目_

"GET / HTTP/2.0" 301 216 "-" 

没有错误/var/log/nginx/error.log

非常感谢!!!

linux ssl nginx https ssl-certificate
  • 2 2 个回答
  • 881 Views

2 个回答

  • Voted
  1. Best Answer
    jarvis
    2016-04-16T10:15:17+08:002016-04-16T10:15:17+08:00

    所以你想将所有流量重定向到https://www.example.com?

    我认为您在listen 443 ssl中缺少ssl指令;

    请参阅下面的示例

    使用 HTTPS 的主要配置 - www.example.com

    server {
        listen 443 ssl;
        server_name www.example.com;
        ssl_certificate /path/ssl.crt
        other ssl config....
    }
    

    将 HTTPS 裸域重定向到 WWW

    server {
            listen 443 ssl;
            server_name example.com;
            return 301 https://www.example.com$request_uri;
        }
    

    重定向所有 HTTP

    server {
            listen 80;
            server_name example.com www.example.com;
            return 301 https://www.example.com$request_uri;
        }
    
    • 0
  2. Tim
    2016-04-16T01:13:12+08:002016-04-16T01:13:12+08:00

    像这样

    # Redirect all variations to https://www domain
    server {
      listen 80;
      access_log  /var/log/nginx/hr.access.log main buffer=128k flush=60;
      server_name example.com www.example.com;
      return 301 https://example.com$request_uri;
    }
    
    server {
      listen 443 ssl http2;
      server_name www.example.com;
    
      ssl_certificate /var/lib/acme/certs/x/fullchain;
      ssl_certificate_key /var/lib/acme/certs/x/privkey;
    
      access_log  /var/log/nginx/hr.access.log main buffer=128k flush=60;
    
      # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;
    
      # This is a cache for SSL connections
      ssl_session_cache shared:SSL:2m;
      ssl_session_timeout 60m;
    
      return 301 https://example.com$request_uri;
    }
    
    • -1

相关问题

  • 你最喜欢的 Linux 发行版是什么?[关闭]

  • 您最喜欢的 SSL 证书提供商是什么?[关闭]

  • 更改 PHP 的默认配置设置?

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve