Nginx与SSL连接错误

我试图从HTTP迁移到HTTPS,但是,我的nginx(版本:1.10.3)configuration似乎没有工作。

以下行为是需要的:

  • http://www.example.com/path/to/content应该redirecthttps://example.com/path/to/content
  • http://example.com/path/to/content应redirect到https://example.com/path/to/content
  • https://www.example.com/path/to/content应redirect到https://example.com/path/to/content

与我目前的configuration浏览器不会连接到使用HTTPS的网站:

 server { listen 80; listen [::]:80; server_name www.example.com example.com; # redirects both www and non-www to https rewrite ^(.*) https://www.example.com$1 permanent; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name example.com; # redirects non-www to www rewrite ^(.*) https://www.example.com$1 permanent; } server { listen 443 ssl http2; listen [::]:443 ssl http2; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; charset utf-8; # rest of my config } 
  • 为了实现上述行为,我需要做些什么?
  • 是否有可能在第一步接受(并稍后redirect)HTTP请求,以保持页面“活”,让我testing它?
  • 我的网站有很好的SEO排名(索引为“ http://www.example.com ”),所以正确的redirect是必须的。

这个configuration使你所问:

 server { listen 80; listen [::]:80; server_name www.example.com example.com; # redirects both www and non-www to https return 301 https://example.com$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name www.example.com; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; # redirects www to non-www return 301 https://example.com$request_uri; } server { listen 443 ssl http2; listen [::]:443 ssl http2; server_name example.com; include snippets/ssl-example.com.conf; include snippets/ssl-params.conf; charset utf-8; # rest of my config } 

我改变了rewrite ,因为这样更有效率。 使用return ,必须使用$request_uri将请求path和参数放入redirectURL中。

然后我改变了server_name example.com; listen 443; 块提供网站的实际内容, server_name www.example.com; listen 443; redirect。

请尝试使用下面的方法,并更新其余的SSL信息:

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

并为ssl提供ssl密钥path:

 server { listen 443 ssl; ssl on; ssl_prefer_server_ciphers on; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_certificate /path of certificate; ssl_certificate_key /path of server.key; }