【问题标题】:nginx HTTPS redirect is not triggered when IP address is used使用IP地址时不会触发nginx HTTPS重定向
【发布时间】:2016-06-27 17:39:43
【问题描述】:

我使用 nginx 作为我的公共 EC2 实例的反向代理。我有:

  • 一个漂亮、干净的公共领域
  • AWS 生成的公共域 (*.compute-1.amazonaws.com)
  • AWS 生成的公共 IP 地址

我希望所有流量都通过 HTTPS 传输到公共域。我试图通过创建一个配置为路由到我的应用程序的“主要”服务器块来实现这一点,并使用两个辅助服务器块来捕获所有其他流量并重定向到https://public.domain.com。这是我的配置的样子:

# "Primary" block
server {
    listen       443 ssl;
    server_name  public.domain.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
    # Other config; SSL config
}

# Catch-all redirects
server {
    listen      80;

    return 301 https://public.domain.com$request_uri;
}
server {
    listen      443;

    return 301 https://public.domain.com$request_uri;
}

在测试中,我得到以下结果:

为什么 nginx 没有将我的 HTTPS 流量重定向到我的公共域?网址匹配过程中没有使用server_name吗?

【问题讨论】:

    标签: nginx amazon-ec2


    【解决方案1】:

    你没有在 443 端口上设置默认服务器,所以 nginx 采用第一个定义的主机,即server_name public.domain.com;

    使用listen 443 ssl default_server,您还需要为您的重定向服务器提供通配符证书才能使此配置正常工作(自签名,如果主机不匹配,客户端仍会显示警告)

    https://serverfault.com/questions/578648/properly-setting-up-a-default-nginx-server-for-https

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2018-01-05
      • 2016-01-09
      • 2014-05-20
      • 2020-04-07
      • 2018-07-23
      • 2021-03-26
      • 1970-01-01
      相关资源
      最近更新 更多