NGINX将http重定向到https,将非www重定向到ww [英] NGINX Redirect http to https and non-www to ww

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

问题描述

我正在设置一个带有SSL的nginx服务器。

I'm setting up an nginx server with an SSL.

带有ssl的域名是www.mydomain.com

The domain with the ssl is www.mydomain.com

我想重定向所有请求:

http://mydomain.com http://www.mydomain.com ,& https://mydomain.com

https://www.mydomain.com

我目前设置了以下服务器块:

I have the following server blocks setup currently:

server{
  listen 443 ssl;
  root /www/mydomain.com/;

  ssl_certificate /ssl/domain.crt;
  ssl_certificate /ssl/domain.key;
  .
  . 
  .
}

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

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

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

目前这不起作用,但我不明白为什么不行。我可以得到http - > https工作或没有www到 - > www工作的组合,但如上所述混合它们不起作用。

This currently does not work, but I don't understand why not. I can get a combination of either http -> https working or no-www to -> www working, but mixing them as above does not work.

推荐答案

如果您的ssl证书不支持非www域,则ssl重定向将不起作用。
配置正确,但可以简化为1个重定向服务器

The ssl redirect won't work if your ssl certificate doesn't support the non-www domain. The config is correct but can be reduced to just 1 redirect server

另外不要忘记重新加载nginx sudo service nginx reload

Also don't forget to reload nginx sudo service nginx reload

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

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

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