Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当域在Nginx中使用https时,如何对子域使用http_Nginx - Fatal编程技术网

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

当域在Nginx中使用https时,如何对子域使用http,nginx,Nginx,我在/etc/nginx/sites enabled/example.com文件中有一个基本配置,用于将HTTP流量重定向到站点的HTTPS版本 server { listen 80; server_name example.com www.example.com; rewrite ^ https://example.com$request_uri? permanent; } server { listen 443; server_name example.com; s

我在
/etc/nginx/sites enabled/example.com
文件中有一个基本配置,用于将HTTP流量重定向到站点的HTTPS版本

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
}
上述措施效果良好。当我访问时,我被重定向到

我需要example.com上的子域,但它只需要HTTP。我创建了一个
/etc/nginx/sites enabled/sub.example.com
文件,该文件以

server {
   listen 80;
   server_name sub.example.com;
起初,它似乎有效,但我注意到一个问题:

  • 清除浏览器缓存并访问。这个网站没有任何问题
  • 接下来,访问。浏览器被重定向到 这很好
  • 回去看看吧这一次,浏览器被重定向到,并且浏览器抱怨缺少证书。

  • 为什么会这样?对于子域使用HTTP而不是域上使用HTTPS的正确方法是什么?

    @alexey ten是正确的。在我的特殊情况下,我遵循了使用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
    并重新启动服务器,使事情按我预期的方式运行。

    您有HST吗?-我99%确定您的HSTS标题中有
    includeSubDomains
    指令