如何使用Nginx将http(s)://(www。)example.comredirect到https://example.com?

正如标题所述。

我发现另一个有用的Q / A ,但它并没有显示如何在Nginx设置中正确地做到这一点,我的意思是parameters 。 他可能意味着https://exmaple.com在redirect到https://www.example.com之前应该有一个有效的握手。 在我的场景中, https: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它被redirect到https://example.com但是它显示在chrome中

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

我想redirect到https://example.com ,怎么样?

– 更新 –

/var/log/nginx/access.log显示

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

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

非常感谢!!!

所以你想把所有的stream量redirect到https://www.example.com

我想你错过了侦听443 ssl中ssl指令;

见下面的示例

主要configurationHTTPS – http://www.example.com

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

将HTTPS裸域redirect到WWW

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

redirect所有的HTTP

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

喜欢这个

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