当域在Nginx中使用https时,如何使用http作为子域

时间:2017-03-20 04:08:25

标签: nginx

我的<body ng-app="MyApp" ng-controller="MyController"> <div class="container"> <table class="table table-striped"> <thead> <tr > <th>ID</th> <th>Désignation</th> <th>Prix</th> <th>Quantité</‌​th> </tr> </thead> <tbody> <tr ng-repeat="p in pageProduits"> <td>{{p.id}}</td> <td>{{p.designation}}</td> <td>{{p.prix}}</td> <td>{{p.quantite}}</td> </tr> </tbody> </table> </div> <script type="text/javascript" src="node_modules/angular/angular.min.js"></script> <script type="text/javascript" src="js/app.js"></script> </body> 文件中有一个基本配置,可将HTTP流量重定向到网站的HTTPS版本。

/etc/nginx/sites-enabled/example.com

以上工作正常。当我访问http://example.com时,我被重定向到https://example.com

我需要example.com上的子域,但它只需要HTTP。我创建了一个以

开头的server { listen 80; server_name example.com www.example.com; rewrite ^ https://example.com$request_uri? permanent; } server { listen 443; server_name example.com; ssl on; ... rest of the site configuration } 文件
/etc/nginx/sites-enabled/sub.example.com

起初,它似乎有效,但我发现了一个问题:

  1. 清除浏览器缓存并访问http://sub.example.com。该网站没有任何问题。
  2. 接下来,访问http://example.com。浏览器被重定向到 https://example.com这很好。
  3. 返回并再次访问http://sub.example.com这次,浏览器被重定向到https://sub.example.com,浏览器会抱怨缺少证书。
  4. 为什么会这样?使用HTTP作为子域的正确方法是什么,但域上使用HTTPS?

1 个答案:

答案 0 :(得分:0)

@ alexey-ten是正确的。在我的特定情况下,我遵循https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04教程使用LetsEncrypt来获取证书。在本教程中,我创建了一个ssl-params.conf文件,并使用

将其加载到我的/etc/nginx/sites-enabled/example.com文件中
server {
  listen 443;
  server_name example.com;
  ssl on;
  include /etc/nginx/snippets/ssl-params.conf;
  ... rest of the site configuration
}

ssl-params.conf文件确实有以下行:

add_header Strict-Transport-Security "max-age=63072000; includeSubdomains";

删除includeSubdomains并重新启动服务器让事情按照我的预期运行。

相关问题