Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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 http重定向到https返回无法读取的响应_Http_Redirect_Nginx_Https - Fatal编程技术网

NginX http重定向到https返回无法读取的响应

NginX http重定向到https返回无法读取的响应,http,redirect,nginx,https,Http,Redirect,Nginx,Https,我想用NginX将所有http请求重定向到https,但我在这方面遇到了一些困难 这是我的vhost文件: server { gzip off; listen 80; listen [::]:80; server_name mydomain.fr www.mydomain.fr sub.otherdom.fr otherdom.fr; return 301 https://$host$request_uri; } server { lis

我想用NginX将所有http请求重定向到https,但我在这方面遇到了一些困难

这是我的vhost文件:

server {
    gzip off;
    listen 80;
    listen [::]:80;
    server_name mydomain.fr www.mydomain.fr sub.otherdom.fr otherdom.fr;
    return 301 https://$host$request_uri;
    }

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    root /usr/share/nginx/html;
    index index.html index.htm;
    ssl on;
    server_name mydomain.fr www.mydomain.fr sub.otherdom.fr otherdom.fr;
    ssl_certificate /root/tmp/live-ecdsa/mydomain.fr/0001_chain.pem;
    ssl_certificate_key /root/tmp/live-ecdsa/mydomain.fr/privkey-p384.pem;
    access_log /var/log/nginx/default.access.log;
    charset utf-8;
    location  / {
        try_files $uri $uri/ /index.html;
        }
        }
尝试使用不同浏览器通过普通http访问这些域会导致以下结果: Chrome/Firefox:下载一个包含字节数据的文件

Edge:显示带

A
curl-imydomain.fr
outputs
▒▒

直接通过https访问这些域是可行的。
我已经尝试过使用
return301 https://$host$request\u uri
返回301 https://$server\u name$request\u uri

我怀疑这与您在相当大的本地范围上下文中的“一个服务器名称”字段中声明的大量服务器名称有关。尽管如此,如果我诚实的话,这是一个基于我已经成为用户的习惯的毫无根据的断言

我建议做几件事,虽然通常这些都不能解决你的问题,但这可能会让你更容易弄清楚发生了什么:

  • 将配置拆分为指定的文件。例如,在另一个文件夹中创建一个ssl.conf,其中包含所有您的证书设置、密码套件等。然后在配置中添加include/path/to/ssl.conf
  • 不要使用$host,这个变量可以由用户设置,所以可能不是一个好主意
假设您已经从其他地方引用了所有其他相关的ssl/tls设置,那么下面的操作应该大致可行

server {
  listen 80 default_server;
  listen [::]:80 default_server;
  server_name mydomain.fr;
  return 301 https://$server_name$request_uri;
}

server {
  listen 443 ssl http2 default_server;
  listen [::]:443 ssl http2 default_server;
  root /usr/share/nginx/html;
  $server_name mydomain.fr
  location  / {
    try_files $uri $uri/ /index.html;
  }
}

虽然user6788523的响应帮助我进行了调试,但问题出在我这边

我还有几个其他vhost文件,其中包含与http端口80关联的http2指令(
listen[:]:80http2;
)。删除
http2
指令解决了问题

此设置只能与启用ssl的服务器块一起使用