Nginx:将非www重定向到https上的www [英] Nginx: redirect non-www to www on https

查看:103
本文介绍了Nginx:将非www重定向到https上的www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在https上,Nginx无法将非www重定向到www:

https://domain.com https://www.domain.com

我在.conf中的当前设置是:

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

http://domain.com https://www.domain.com http://www.domain https://www.domain.com 有效,但是https上的非www到www无效. /p>

如果我在第二个server块上添加了IP_ADDRESS,我会在Chrome中看到一个错误(SSL错误),并且(www和非www)都停止工作.

更新:

多亏了斯特芬(答案如下),我将自签名证书更新为*.domain.com而不是domain.com.

.conf文件已更新为以下格式:

ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

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

解决方案

这可能是因为您没有domain.com的证书,而只有www.domain.com或* .domain.com的证书.参见 Nginx重定向http://www并以裸露的http/https到https://www https://domain.com to https://www.domain.com

My curent setup in .conf is:

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

http://domain.com to https://www.domain.com and http://www.domain to https://www.domain.com works, but non-www to www on https isn't working.

If I added the IP_ADDRESS on the second server block, I get an error in Chrome (SSL error) and both (www and non-www) stop working.

UPDATE:

Thanks to Steffen (below answer), I updated the self-signed certificate to be *.domain.com and not domain.com.

The .conf file was updated under this format:

ssl_certificate /etc/nginx/ssl/server.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;

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

解决方案

This is probably because you don't have a certificate for domain.com, but only for www.domain.com or *.domain.com. See Nginx redirect http://www and naked http/https to https://www or https://serverfault.com/questions/579916/nginx-redirect-https-www-to-https-non-www-without-untrusted-connection-warn/579917#579917 for details.

这篇关于Nginx:将非www重定向到https上的www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆