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

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

正如标题所述。

我发现另一个有用的问答,但它没有显示如何在 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

非常感谢!!!

答案1

因此,您希望将所有流量重定向到https://www.example.com

我觉得你错过了安全套接字指令监听 443 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;
    }

答案2

像这样

# 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;
}

相关内容