【问题标题】:Nginx: Redirect both http://example.com and http://*.example.com to https://example.comNginx:将 http://example.com 和 http://*.example.com 都重定向到 https://example.com
【发布时间】:2015-05-21 10:39:54
【问题描述】:

我只有 example.com 的 SSL 证书,并且想使用 nginx 将 http://example.com 和 http://*.example.com 都重定向到 https://example.com。我知道如果没有包含所有子域的证书,就不可能通过 SSL 重定向子域,但至少,我应该能够重定向正在输入 www.example.com 的用户(端口 80 ) 到 SSL 主页。

我当前的 nginx 配置如下:

server {
        # This should catch all non-HTTPS requests to example.com and *.example.com
        listen 80;
        server_name example.com *.example.com;
        access_log off;
        return 301 https://example.com$request_uri;
}

server {
        listen 443 ssl;
        # Actual server config starts here...

请求http://example.com 将被正确重定向到https://example.com,而http://www.example.com 导致https://www.example.com(当然,浏览器显示证书错误)。我认为这与 server_name 值的处理顺序有关,但我没有找到有关如何强制执行某个顺序的任何信息。

【问题讨论】:

    标签: redirect ssl nginx


    【解决方案1】:

    尝试添加另一台服务器{}

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

    【讨论】:

      【解决方案2】:

      试试:

      server_name  example.com  www.example.com  *.example.com;
      

      直接取自 Nginx docs

      【讨论】:

        猜你喜欢
        • 2020-09-24
        • 2016-01-16
        • 1970-01-01
        • 1970-01-01
        • 2012-02-20
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        • 1970-01-01
        相关资源
        最近更新 更多